#!/bin/sh
#
# $Id: uninstall.sh 6591 2010-08-06 20:50:57Z NiLuJe $
#
# diff OTA patch script

_FUNCTIONS=/etc/rc.d/functions
[ -f ${_FUNCTIONS} ] && . ${_FUNCTIONS}


MSG_SLLVL_D="debug"
MSG_SLLVL_I="info"
MSG_SLLVL_W="warn"
MSG_SLLVL_E="err"
MSG_SLLVL_C="crit"
MSG_SLNUM_D=0
MSG_SLNUM_I=1
MSG_SLNUM_W=2
MSG_SLNUM_E=3
MSG_SLNUM_C=4
MSG_CUR_LVL=/var/local/system/syslog_level

logmsg()
{
    local _NVPAIRS
    local _FREETEXT
    local _MSG_SLLVL
    local _MSG_SLNUM

    _MSG_LEVEL=$1
    _MSG_COMP=$2

    { [ $# -ge 4 ] && _NVPAIRS=$3 && shift ; }

    _FREETEXT=$3

    eval _MSG_SLLVL=\${MSG_SLLVL_$_MSG_LEVEL}
    eval _MSG_SLNUM=\${MSG_SLNUM_$_MSG_LEVEL}

    local _CURLVL

    { [ -f $MSG_CUR_LVL ] && _CURLVL=`cat $MSG_CUR_LVL` ; } || _CURLVL=1

    if [ $_MSG_SLNUM -ge $_CURLVL ]; then
        /usr/bin/logger -p local4.$_MSG_SLLVL -t "ota_install" "$_MSG_LEVEL def:$_MSG_COMP:$_NVPAIRS:$_FREETEXT"
    fi

    [ "$_MSG_LEVEL" != "D" ] && echo "ota_install: $_MSG_LEVEL def:$_MSG_COMP:$_NVPAIRS:$_FREETEXT"
}

if [ -z "${_PERCENT_COMPLETE}" ]; then
    export _PERCENT_COMPLETE=0
fi

update_percent_complete()
{
    _PERCENT_COMPLETE=$((${_PERCENT_COMPLETE} + $1))
    update_progressbar ${_PERCENT_COMPLETE}
}

# Hack specific config (name and when to start/stop)
HACKNAME="linkss"
SLEVEL="73"
KLEVEL="08"

update_percent_complete 2

# From v0.4.N
# Boot symlink
logmsg "I" "update" "removing boot symlink from v0.4.N"
[ -h /etc/rcS.d/S90${HACKNAME} ] && rm -f /etc/rcS.d/S90${HACKNAME}

update_progressbar 12

# From v0.5.N
logmsg "I" "update" "removing messed up symlinks from v0.5.N"
[ -h /etc/rc6.d/K8${HACKNAME} ] && rm -f /etc/rc6.d/K8${HACKNAME}
[ -h /etc/rc0.d/K8${HACKNAME} ] && rm -f /etc/rc0.d/K8${HACKNAME}
[ -h /etc/rc3.d/K8${HACKNAME} ] && rm -f /etc/rc3.d/K8${HACKNAME}

update_progressbar 24

# From v0.5.N
# Boot symlink
logmsg "I" "update" "removing boot symlink"
[ -h /etc/rc5.d/S${SLEVEL}${HACKNAME} ] && rm -f /etc/rc5.d/S${SLEVEL}${HACKNAME}

update_progressbar 36

# Reboot symlink
logmsg "I" "update" "removing reboot symlink"
[ -h /etc/rc6.d/K${KLEVEL}${HACKNAME} ] && rm -f /etc/rc6.d/K${KLEVEL}${HACKNAME}

update_progressbar 48

# Shutdown symlink
logmsg "I" "update" "removing shutdown symlink"
[ -h /etc/rc0.d/K${KLEVEL}${HACKNAME} ] && rm -f /etc/rc0.d/K${KLEVEL}${HACKNAME}

update_progressbar 60

# Updater symlink
logmsg "I" "update" "removing update symlink"
[ -h /etc/rc3.d/K${KLEVEL}${HACKNAME} ] && rm -f /etc/rc3.d/K${KLEVEL}${HACKNAME}

update_progressbar 72

# Remove our hack's init script
logmsg "I" "update" "removing init script"
[ -f /etc/init.d/${HACKNAME} ] && rm -f /etc/init.d/${HACKNAME}

update_progressbar 85

# From v0.10.N
# Remove custom directory in userstore?
logmsg "I" "update" "removing custom directory (only if /mnt/us/${HACKNAME}/uninstall exists)"
[ -d /mnt/us/${HACKNAME} -a -f /mnt/us/${HACKNAME}/uninstall ] && rm -rf /mnt/us/${HACKNAME}

logmsg "I" "update" "done"
update_progressbar 100

return 0
