#!/bin/sh
#
# Script args:
# $0: full path to script
# $1: full path to top level package dir, no trailing slash
# $2: full path to installed location
# $3:  ????? Path to install Volume????
# $4:  ?????

update_extension_10_6()
{

    kextload -t /System/Library/Extensions/MBBFCDataCardACMData.kext

    sleep 5

    PID=`ps -axc | grep kextd | perl -e '($a,$r,$z)=split /  */, <>; print $r;'`
    kill -HUP $PID

    if sw_vers -productVersion |grep -c 10.6; then
    touch /System/Library/Extensions
    fi

}

update_extension()
{
    #BEGIN DTS2013110200564,Modified by y00173071, 2013/11/11
    echo "update_extension"
    HIGHER_SYSTEM=0

    OSMAJORVER=`(sw_vers -productVersion) | awk -F '.' '{print $1}'`
    if [ "$OSMAJORVER" -gt "10" ] #eg. 10.*.*  compare first number
    then
        HIGHER_SYSTEM=1
    else
        if [ "$OSMAJORVER" = "10" ] #eg. 10.*.*  compare first number
        then 
            OSMINIVER=`(sw_vers -productVersion) | awk -F '.' '{print $2}'`
            if [ $OSMINIVER -gt "8" ] #eg. 10.8.*  compare second number
            then
                HIGHER_SYSTEM=1
            else
                HIGHER_SYSTEM=0
            fi
        else  #eg. 9.*.* old system, compare first number
            HIGHER_SYSTEM=0
        fi
    fi

    if [ $HIGHER_SYSTEM = "1" ] #eg. 10.9.*  new system
    then
        echo "Mac 10.9.load MBBFCDataCardACMData_10_9(1)"
        kextload -t /Library/Extensions/MBBFCDataCardACMData_10_9.kext
    else
        kextload -t /System/Library/Extensions/MBBFCDataCardACMData.kext
    fi
    #END DTS2013110200564,Modified by y00173071, 2013/11/11

    PID=`ps -axc | grep kextd | perl -e '($a,$r,$z)=split /  */, <>; print $r;'`
    kill -HUP $PID



    if sw_vers -productVersion |grep -c 10.5; then
        touch /System/Library/Extensions
    fi
    if sw_vers -productVersion |grep -c 10.6; then
    touch /System/Library/Extensions
    fi

}

update_driver()
{
    #BEGIN DTS2013110200564,Modified by y00173071, 2013/11/11
    echo "update_driver"
    HIGHER_SYSTEM=0

    OSMAJORVER=`(sw_vers -productVersion) | awk -F '.' '{print $1}'`
    if [ "$OSMAJORVER" -gt "10" ] #eg. 10.*.*  compare first number
    then
        HIGHER_SYSTEM=1
    else
        if [ "$OSMAJORVER" = "10" ] #eg. 10.*.*  compare first number
        then 
            OSMINIVER=`(sw_vers -productVersion) | awk -F '.' '{print $2}'`
            if [ $OSMINIVER -gt "8" ] #eg. 10.8.*  compare second number
            then
                HIGHER_SYSTEM=1
            else
                HIGHER_SYSTEM=0
            fi
        else  #eg. 9.*.* old system, compare first number
            HIGHER_SYSTEM=0
        fi
    fi
    #END DTS2013110200564,Modified by y00173071, 2013/11/11

    echo "path is :"
    echo $1

    #BEGIN DTS2013110200564,Modified by y00173071, 2013/11/11
    if [ "$HIGHER_SYSTEM" = "0" ]  #Lower than 10.9
    then
        echo "update_driver, HIGHER_SYSTEM is :$HIGHER_SYSTEM" `sw_vers -productVersion`
        rm -Rf /System/Library/Extensions/MBBFCDataCardACMData.kext
        mv -f /Library/MBBFCDataCardACMData.kext /System/Library/Extensions/MBBFCDataCardACMData.kext   

        chown -R root:wheel /System/Library/Extensions/MBBFCDataCardACMData.kext
        chmod -R 755 /System/Library/Extensions/MBBFCDataCardACMData.kext
	
	#BEGIN DTS2013111106133,Deleted by y00173071, 2013/11/12
        #chown -R root:wheel /System/Library/Extensions/USBExpressCardCantWake_Huawei.kext
        #chmod -R 755 /System/Library/Extensions/USBExpressCardCantWake_Huawei.kext
	#END DTS2013111106133,Deleted by y00173071, 2013/11/12

        rm -Rf /Library/MBBFCDataCardACMData_10_9.kext
    else                           #10.9 or Higher
        echo "update_driver, HIGHER_SYSTEM is :$HIGHER_SYSTEM" `sw_vers -productVersion`
        rm -Rf /System/Library/Extensions/MBBFCDataCardACMData.kext

        rm -Rf /Library/Extensions/MBBFCDataCardACMData_10_9.kext
        mv -f /Library/MBBFCDataCardACMData_10_9.kext /Library/Extensions/MBBFCDataCardACMData_10_9.kext

        chown -R root:wheel /Library/Extensions/MBBFCDataCardACMData_10_9.kext
        chmod -R 755 /Library/Extensions/MBBFCDataCardACMData_10_9.kext

        rm -Rf /Library/MBBFCDataCardACMData.kext
    fi
    #END PN:DTS2013110200564,Modified by y00173071, 2013/11/11

    rm /System/Library/Extensions.kextcache
    kextcache -k /System/Library/Extensions
    rm -Rf /System/Library/Caches
    touch /System/Library/Extensions
    if sw_vers -productVersion |grep -c 10.6; then
        update_extension_10_6
    else
        update_extension
    fi
}

#BEGIN PN:DTS2013110200564,Add by y00173071, 2013/11/11
chech_driver_install()
{
    NEED_UPDATE=0
    VERSIONVOL=3

    echo "chech_driver_install enter, NEED_UPDATE:$NEED_UPDATE"
    OLD_VERSIONLINE=$[ $(cat -n $1/Contents/Info.plist |grep "CFBundleShortVersionString" |cut -d \< -f1)+1 ]
    NEW_VERSIONLINE=$[ $(cat -n $2 |grep "CFBundleShortVersionString" |cut -d \< -f1)+1 ]

    EXT_VERSION=$(cat -n $1/Contents/Info.plist |head -n 20 |grep $OLD_VERSIONLINE |cut -f $VERSIONVOL |cut -d \< -f2 |cut -d \> -f2)
    VERSION=$(cat -n $2 |head -n 20 |grep $NEW_VERSIONLINE |cut -f $VERSIONVOL |cut -d \< -f2 |cut -d \> -f2)

    EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $4}'`

    echo "(1)EXT_TEMP:$EXT_TEMP, EXT_VERSION:$EXT_VERSION, VERSION:$VERSION"
    if [ "$(echo $EXT_TEMP)" != "" ]
    then
	TEMP=`echo $VERSION |awk -F '.' '{print $4}'`
	COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`

	echo "(2)TEMP:$TEMP, COMPARE:$COMPARE, VERSION:$VERSION"
	if [ $COMPARE = "0" ] 
	then
	    EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $1}'`
            TEMP=`echo $VERSION |awk -F '.' '{print $1}'`
            COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`

            echo "(3)TEMP:$TEMP, COMPARE:$COMPARE, EXT_TEMP:$EXT_TEMP"
            if [ $COMPARE -lt "0" ]
            then
                echo "COMPARE < 0" #packet is old
                NEED_UPDATE=0
            else
                if [ $COMPARE = "0" ]
                then
                    EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $2}'`
                    TEMP=`echo $VERSION |awk -F '.' '{print $2}'`
                    COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`
                    echo "(4)TEMP:$TEMP, COMPARE:$COMPARE, EXT_TEMP:$EXT_TEMP"
                    if [ $COMPARE -lt "0" ]
                    then
                        NEED_UPDATE=0 #packet is old
                    else
                        if [ $COMPARE = "0" ]
                        then
                            EXT_TEMP=`echo $EXT_VERSION |awk -F '.' '{print $3}'`
                            TEMP=`echo $VERSION |awk -F '.' '{print $3}'`
                            COMPARE=`echo "$TEMP-$EXT_TEMP" |bc`
                            echo "(5)TEMP:$TEMP, COMPARE:$COMPARE, EXT_TEMP:$EXT_TEMP"
                            if [ $COMPARE -lt "0" ]
                            then
                                NEED_UPDATE=0 #packet is old
                            else
                                NEED_UPDATE=1
                            fi
                        else
                            NEED_UPDATE=1
                        fi
                    fi
                else
                    NEED_UPDATE=1
                fi
            fi
	else
	    NEED_UPDATE=1
	fi
    else
        NEED_UPDATE=1
    fi
    echo "chech_driver_install return, NEED_UPDATE:$NEED_UPDATE"
    return NEED_UPDATE;
}
#END PN:DTS2013110200564,Add by y00173071, 2013/11/11
#BEGIN PN:DTS2013110200564,Add by y00173071, 2013/11/11
install_driver()
{
    echo "install_driver"
    HIGHER_SYSTEM=0

    OSMAJORVER=`(sw_vers -productVersion) | awk -F '.' '{print $1}'`
    if [ "$OSMAJORVER" -gt "10" ] #eg. 10.*.*  compare first number
    then
        HIGHER_SYSTEM=1
    else
        if [ "$OSMAJORVER" = "10" ] #eg. 10.*.*  compare first number
        then 
            OSMINIVER=`(sw_vers -productVersion) | awk -F '.' '{print $2}'`
            if [ $OSMINIVER -gt "8" ] #eg. 10.8.*  compare second number
            then
                HIGHER_SYSTEM=1
            else
                HIGHER_SYSTEM=0
            fi
        else  #eg. 9.*.* old system, compare first number
            HIGHER_SYSTEM=0
        fi
    fi

    if [ "$HIGHER_SYSTEM" = "0" ]  #Lower than 10.9
    then
        OLD_FILE="/System/Library/Extensions/MBBFCDataCardACMData.kext"
        NEW_FILE="/Library/MBBFCDataCardACMData.kext/Contents/Info.plist"

        if [ "$(ls /System/Library/Extensions/ |grep MBBFCDataCardACMData)" != "" ]
        then
            NEED_UPDATE=$(chech_driver_install "$OLD_FILE" "$NEW_FILE")
            if [ $NEED_UPDATE = "0" ]
            then
                #rm -Rf /System/Library/MBBFCDataCardACMData.kext
                rm -Rf /Library/MBBFCDataCardACMData.kext
                rm -Rf /Library/MBBFCDataCardACMData_10_9.kext
		#BEGIN DTS2013111106133,Deleted by y00173071, 2013/11/12
		#rm -Rf /System/Library/USBExpressCardCantWake_Huawei.kext
		#END DTS2013111106133,Deleted by y00173071, 2013/11/12

            else
                update_driver "$1"
            fi
        else
            update_driver "$1"
        fi
    else                             #10.9 or Higher
        OLD_FILE="/Library/Extensions/MBBFCDataCardACMData_10_9.kext"
        NEW_FILE="/Library/MBBFCDataCardACMData_10_9.kext/Contents/Info.plist"
        
        if [ "$(ls /Library/Extensions/ |grep MBBFCDataCardACMData_10_9)" != "" ]
        then

            NEED_UPDATE=$(chech_driver_install "$OLD_FILE" "$NEW_FILE")
            if [ $NEED_UPDATE = "0" ];
            then
                rm -Rf /Library/MBBFCDataCardACMData.kext
                rm -Rf /Library/MBBFCDataCardACMData_10_9.kext
            else
                update_driver "$1"
            fi

        else
            update_driver "$1"
        fi
    fi
}
#END DTS2013110200564,Modified by y00173071, 2013/11/11

install_driver "$1"
exit 0
