#!/bin/sh

sudo mv "$1/../MobileConnectDriver.pkg" "/private/tmp"
install_driver()
{
    echo $1

    declare -i i=1
    
    while ((i<=30))
    do
        if [ -f "/private/tmp/MobileConnectDriver.pkg" ]
        then
            sudo installer -pkg /private/tmp/MobileConnectDriver.pkg -target /
            res=$?  #install ok or failed??
            if [ "$res" == "0" ]
            then
                echo "MobileConnectDriver.pkg on /tmp/FlatPkg/ found,and install ok"
            else
               echo "MobileConnectDriver.pkg on /tmp/FlatPkg/ found,and install failed"
            fi 
            rm -Rf /private/tmp/MobileConnectDriver.pkg
            break
        else
            echo "MobileConnectDriver.pkg on /tmp/FlatPkg/ dir not found,sleep 1 second"
            sleep 1
        fi
    done 
    PID=`ps -axc | grep kextd | perl -e '($a,$r,$z)=split /  */, <>; print $r;'`
    kill -HUP $PID
    touch /System/Library/Extensions
}

install_driver "$1"



killall -c HWPortCfg_driver

sudo chown -R root:wheel /Library/StartupItems/HWPortDetect_driver
sudo chmod -Rf 755 /Library/StartupItems/HWPortDetect_driver

sudo cp -f "$1/Contents/Resources/com.huawei.hwportdetect_driver.plist" "/Library/LaunchDaemons/"
sudo cp -f "$1/Contents/Resources/com.huawei.hw_createnetwork.plist" "/Library/LaunchDaemons/"
sudo cp -f "$1/Contents/Resources/config" "/Library/StartupItems/HWPortDetect_driver/"
sudo cp -f "$1/Contents/SysConfig.dat" "/Library/StartupItems/HWPortDetect_driver/"

sudo chown -R root:wheel /Library/StartupItems/HWPortDetect_driver
sudo chmod -Rf 755 /Library/StartupItems/HWPortDetect_driver

sudo chmod -Rf 644 /Library/LaunchDaemons/com.huawei.hwportdetect_driver.plist
sudo chown -R root:wheel /Library/LaunchDaemons/com.huawei.hwportdetect_driver.plist
sudo chmod -Rf 644 /Library/LaunchDaemons/com.huawei.hw_createnetwork.plist
sudo chown -R root:wheel /Library/LaunchDaemons/com.huawei.hw_createnetwork.plist


