#!/bin/bash
kernel_ver=$(uname -r)
kernel_machine=$(uname -m)
module_name="usb_pqlabs"

kernel_build_usb_path=/lib/modules/${kernel_ver}/build/drivers/usb/
kernel_usb_path=/lib/modules/${kernel_ver}/kernel/drivers/usb/

#default install path
target_dir="/opt/pqlabs/driver"

#get the install target from the arguments
if [ $# -gt 0 ]; then
	target_dir=$1
fi

#check ubuntu 14.04 for native touch
is_ubuntu=0
is_ubuntu_14_later=0
linux_release=$(cat /etc/*-release)
case "$linux_release" in
                *Ubuntu*) is_ubuntu=1;;
esac

if [ $is_ubuntu -ne 0 ]; then
    is_ubuntu_14_later=1
    ubuntu_14_version="14.04"
    ubuntu_release=$(lsb_release -sr)
    echo "Ubuntu release:"
    echo $ubuntu_release
    if [ $ubuntu_release \< $ubuntu_14_version ]; then
        is_ubuntu_14_later=0
    fi
fi 

cd $(dirname "$0")
cur_dir=$(pwd)

# test the sudo
echo "testing the \"sudo\" ..."
sudo echo "OK."
if [ $? -ne 0 ]; then
   echo "Failed, please retry later, or switch to \"root\" by \"su\" and retry."
   exit 1
fi
# begin
sudo killall -q pqmtpdaemon 
sudo killall -q MultiTouchPlatformHelper

#remove old module
if [ "$(lsmod | grep $module_name)" != "" ] ; then
	sudo rmmod $module_name
fi
 
if [ -e precompiled/${kernel_ver}_${kernel_machine}/${module_name}.ko ]; then
    cd precompiled/${kernel_ver}_${kernel_machine}
else
    is_aptitude_exist=0
    is_aptget_exist=0
    is_yum_exist=0
    type -a aptitude
    if [ $? -eq 0 ]; then
        is_aptitude_exist=1
    else
        type -a apt-get
        if [ $? -eq 0 ]; then
            is_aptget_exist=1
        else
            type -a yum
            if [ $? -eq 0 ]; then
                is_yum_exist=1
            fi
        fi
    fi 
    if [ ! -e /lib/modules/${kernel_ver}/build ]; then
        echo " kernel headers of ${kernel_ver} is necessary for building driver module; Install it?[y/n]"
        read input
        if [ "$input" != "y" ]; then
           echo "Abort."
           exit 1
        fi
        if [ 1 -eq ${is_aptitude_exist} ]; then
            #it should be Ubuntu with aptitude;
            sudo aptitude install linux-headers-${kernel_ver}
        else
            if [ 1 -eq ${is_yum_exist} ]; then
                sudo yum install kernel-devel-${kernel_ver}  
            else
                if [ 1 -eq ${is_aptget_exist} ]; then
                    sudo apt-get update
                    sudo apt-get install linux-headers-${kernel_ver}
                else
                    echo "  you can install \"kernel-devel\" or \"linux-headers\" of ${kernel_ver} and retry."
                    echo "-----------------------------------"
                    exit 1
                fi #apt_get exist?
            fi #yum exist?
        fi #aptitude exist?
    fi
    type -a gcc
    if [ $? -ne 0 ]; then
        echo " \"gcc\" is needed for build the driver module; Install it?[y/n]"
        read input
        if [ "$input" != "y" ]; then
            echo "Abort."
            exit 1
        fi
        if [ 1 -eq ${is_aptitude_exist} ]; then
            sudo aptitude install gcc
        else
            if [ 1 -eq ${is_yum_exist} ]; then
                sudo yum install gcc
            else
                if [ 1 -eq ${is_aptget_exist} ]; then
                   sudo apt-get update
                   sudo apt-get install gcc
                else
                    echo " you can install \"gcc\" and retry."
                    echo "-----------------------------------"
                    exit 1
                fi #apt-get exist?
            fi #yum exist?
        fi #aptitude exist?
    fi #gcc exist?
    if [ ! -e ${kernel_build_usb_path} -a ! -e ${kernel_usb_path} ]; then
        echo " there is no kernel usb path to copy the driver to."
        echo " please make sure this Linux version is our tested ones."
        echo " or please turn to technique support."
    fi

    target_kernel_path=${kernel_build_usb_path}
    if [ ! -e ${kernel_build_usb_path} ]; then
        target_kernel_path=${kernel_usb_path}
    fi
    driver_folder="pqlabs"
    if [ $is_ubuntu_14_later -ne 0 ]; then
        driver_folder="nativetouch"
    fi
    sudo cp -rf ${driver_folder} ${target_kernel_path}
    cd ${target_kernel_path}/${driver_folder}/
   
    type -a make 
    if [ $? -ne 0 ]; then
        echo " \"make\" is needed for build the driver module; Install it?[y/n]"
        read input
        if [ "$input" != "y" ]; then
            echo "Abort."
            exit 1
        fi
        if [ 1 -eq ${is_aptitude_exist} ]; then
            sudo aptitude install make 
        else
            if [ 1 -eq ${is_yum_exist} ]; then
                sudo yum install make 
            else
                if [ 1 -eq ${is_aptget_exist} ]; then
                   sudo apt-get update
                   sudo apt-get install make
                else
                    echo " you can install \"make\" and retry."
                    echo "-----------------------------------"
                    exit 1
                fi #apt-get exist?
            fi #yum exist?
        fi #aptitude exist?
    fi #make exist?
 
    sudo make
fi
echo " updating driver module..."
sudo cp -f usb_pqlabs.ko /lib/modules/${kernel_ver}/kernel/drivers/usb/
sudo depmod -a
sudo sh -c "echo usb_pqlabs >> /etc/modules"
if [ -d /etc/sysconfig/modules ]; then
    sudo sh -c "echo -e '#!/bin/sh\n exec /sbin/modprobe usb_pqlabs' > /etc/sysconfig/modules/usb_pqlabs.modules"
    sudo chmod +x /etc/sysconfig/modules/usb_pqlabs.modules
fi
sudo insmod /lib/modules/${kernel_ver}/kernel/drivers/usb/usb_pqlabs.ko
cd $cur_dir
#
if [ ! -d $target_dir ]; then
        sudo rm -rf $target_dir
	sudo mkdir -p $target_dir
fi
sudo cp uninstall.sh $target_dir
sudo chmod +x ${target_dir}/uninstall.sh
echo " complete"
echo "-------------------------------------------"
