#!/bin/bash

echo "[$0]"
set -e

if [ -f /etc/xdg/autostart/driver-notify.desktop  ]
then
    rm /etc/xdg/autostart/driver-notify.desktop
fi

is_productname_exists() {
    local productname="$(cat /sys/class/dmi/id/product_name |grep "${1}" 2>/dev/null)"
    if [ "${productname}" ]; then
        return 0
    fi
    return 1
}

is_pci_exists() {
    local pci_info="$(lspci -d "${1}" 2>/dev/null)"
    if [ "${pci_info}" ]; then
        return 0
    fi
    return 1
}

is_usb_exists() {
    local usb_info="$(lsusb -d "${1}" 2>/dev/null)"
    if [ "${usb_info}" ]; then
        return 0
    fi
    return 1
}

# add X550VC wlan software control
fix_productname_x550vc() {
    if is_productname_exists "X550VC"; then
        echo "fix X550VC --------------------------------"

       # add wapf = 4 to enable software control
        echo "options asus_nb_wmi wapf=4" | tee -a /etc/modprobe.d/asus_nb_wmi.conf
    fi
}

# bcm4313 [14e4:4727] wireless driver
fix_pci_bcm4313() {
    if is_pci_exists "14e4:4727"; then
        echo "fix bcm4313 --------------------------------"

        echo "blacklist b43" | tee -a /etc/modprobe.d/bcm.conf
        echo "blacklist wl" | tee -a /etc/modprobe.d/bcm.conf

        # enable brcmsmac
        echo "brcmsmac" | tee -a /etc/modules
    fi
}

# bcm4350 [14e4:43a3] wl is not usable
fix_pci_bcm4350() {
    if is_pci_exists "14e4:43a3"; then
        echo "fix bcm4350 ------------------------------"
        # blacklist wl driver
        echo "blacklist wl" | tee -a /etc/modprobe.d/bcm.conf
    fi
}

# ar9485 [168C:0032] wireless driver
fix_pci_ar9485_nohwcrypt() {
    # fix ar9485 issue that could not connect to wifi with password protect
    if is_pci_exists "168C:0032"; then
        echo "fix ar9485 --------------------------------"
        echo "options ath9k nohwcrypt=1" | tee -a /etc/modprobe.d/ath9k.conf
    fi
}

# rtl8723be [10ec:b723] wireless driver
fix_pci_rtl8723be_fwlps() {
    # fix rtl8723be issue that could not re-connect to wifi after a
    # period of time
    if is_pci_exists "10ec:b723"; then
        echo "fix rtl8723be --------------------------------"
        echo "options rtl8723be fwlps=0" | tee -a /etc/modprobe.d/rtl8723be.conf
    fi
}

# rtl8723au [0bda:1724] wireless driver
# TODO: do not need any more after updating kernel to 3.15
fix_usb_rtl8723au() {
    if  is_usb_exists "0bda:1724"; then
        echo "fix rtl8723au --------------------------------"
        apt-get install --allow-unauthenticated --force-yes -y rtl8723au-dkms
    fi
}

# fix device driver problems
#_kernel_version=$(uname -r)
#case $_kernel_version in
#	4.*)
#		echo "[INFO] Kernel version >> 4.0, skip fix network.."
#		exit 0
#	;;
#esac

fix_pci_rtl8723ae_fwlps() {
     if is_pci_exists "10ec:8723"; then
         echo "fix rtl8723ae ---------------------------------"
         echo 'options rtl8723ae fwlps=0' | tee -a /etc/modprobe.d/rtl8723ae.conf
     fi
}


fix_pci_bcm4313 || true
fix_pci_bcm4350 || true
fix_pci_rtl8723be_fwlps || true
#fix_usb_rtl8723au || true
fix_pci_rtl8723ae_fwlps || true
fix_productname_x550vc || true
if cat /etc/modules |grep "bbswitch"
then
    echo "bbswitch already in /etc/modules"
else
    echo "bbswitch" | tee -a /etc/modules
fi
