#!/system/bin/sh

echo "updating pdndevice start." >> update.log

PDN_DEVICE_EXECUTE_PATH="/data/PdnDevice"
PDN_DEVICE_EXECUTABLE_PATH="$PDN_DEVICE_EXECUTE_PATH/pdndevice"
PDN_WGET_EXECUTABLE_PATH="$PDN_DEVICE_EXECUTE_PATH/pdnwget"
PDN_DEVICE_UPDATE_PATH="$PDN_DEVICE_EXECUTE_PATH/update"

PDN_WGET_PID=$(ps -ef | grep pdnwget | grep -v grep | awk '{print $2}')
if [ -n "$PDN_WGET_PID" ]; then
    kill -9  $PDN_WGET_PID
fi


echo "updating pdndevice..." >> update.log

\cp -rf "$PDN_DEVICE_UPDATE_PATH"/* "$PDN_DEVICE_EXECUTE_PATH"

chown root:root "$PDN_DEVICE_EXECUTABLE_PATH"

chown root:root "$PDN_WGET_EXECUTABLE_PATH"

chmod +x "$PDN_DEVICE_EXECUTABLE_PATH"

chmod +x "$PDN_WGET_EXECUTABLE_PATH"

#cd $PDN_DEVICE_EXECUTE_PATH
#nohup "$PDN_DEVICE_EXECUTABLE_PATH" > /dev/null &
#nohup sh -c "sleep 5 && $PDN_DEVICE_EXECUTABLE_PATH > /dev/null" &

nohup $PDN_WGET_EXECUTABLE_PATH killmain &

# PDN_DEVICE_PID=$(cat $PDN_DEVICE_EXECUTE_PATH/pdndevice.pid)
PDN_DEVICE_PID=$(ps -ef | grep pdndevice | grep -v grep | awk '{print $2}')

if [ -n "$PDN_DEVICE_PID" ]; then
    echo "pdndevice is running, process id: $PDN_DEVICE_PID" >> update.log
    kill -9  $PDN_DEVICE_PID
    if [ $? -eq 0 ]; then
        echo "pdndevice killed." >> update.log
    else
        echo "pdndevice kill failure, update give up." >> update.log
        exit -1
    fi
fi

echo "update pdndevice done" >> update.log

exit 0
