#!/bin/sh
#
# $Id: install.sh 7177 2010-11-14 21:12:34Z 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

# Remove our deprecated content
# From v0.4.N
logmsg "I" "update" "removing deprecated symlinks (v0.4.N)"
[ -h /etc/rcS.d/S90${HACKNAME} ] && rm -f /etc/rcS.d/S90${HACKNAME}

update_progressbar 8

# From v0.5.N
logmsg "I" "update" "removing deprecated symlinks (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 16

# From v0.13.N
logmsg "I" "update" "removing deprecated config files (v0.13.N)"
HACK_CFGLIST="prettyversion.txt"
for hack_cfg in ${HACK_CFGLIST} ; do
    [ -f /mnt/us/${HACKNAME}/bin/${hack_cfg} ] && rm -f /mnt/us/${HACKNAME}/bin/${hack_cfg}
done

update_progressbar 24

# Install our hack's custom content
# But while trying to keep the user's custom content...
if [ -d /mnt/us/${HACKNAME} ] ; then
    logmsg "I" "update" "our custom directory already exists, checking if we have custom content to preserve"
    # Custom Screensavers
    if [ "x$( ls -A /mnt/us/${HACKNAME}/screensavers 2> /dev/null )" != x ] ; then
        # If we already have a non-empty custom ss dir, exclude the default custom ss from our archive
        HACK_EXCLUDE="${HACKNAME}/screensavers/00_you_can_delete_me.png"
        logmsg "I" "update" "found custom screensavers, excluding default custom screensaver"
    fi
fi

update_progressbar 32

# Okay, now we can extract it. Since busybox's tar is very limited, we have to use a tmp directory to perform our filtering
logmsg "I" "update" "installing custom directory"
tar -xvzf ${HACKNAME}.tar.gz

# That's very much inspired from official update scripts ;)
cd src
# And now we filter the content to preserve user's custom content
for custom_file in ${HACK_EXCLUDE} ; do
    if [ -f "./${custom_file}" ] ; then
        logmsg "I" "update" "preserving custom screensavers"
        rm -f "./${custom_file}"
    fi
done
# Finally, re-tape our filtered dir and unleash it on the live userstore
tar cf - . | (cd /mnt/us ; tar xvf -)
_RET=$?
if [ ${_RET} -ne 0 ] ; then
    logmsg "C" "update" "code=${_RET}" "failure to update userstore with custom directory"
    return 1
fi
cd - >/dev/null
rm -rf src

update_progressbar 40

# Install our hack's init script
logmsg "I" "update" "installing init script"
cp -f ${HACKNAME}-init /etc/init.d/${HACKNAME}

update_progressbar 48

# Make it executable
logmsg "I" "update" "chmoding init script"
[ -x /etc/init.d/${HACKNAME} ] || chmod +x /etc/init.d/${HACKNAME}

update_progressbar 56

# Make it start at boot (rc5), after dbus and before the framework
logmsg "I" "update" "creating boot runlevel symlink"
[ -h /etc/rc5.d/S${SLEVEL}${HACKNAME} ] || ln -fs /etc/init.d/${HACKNAME} /etc/rc5.d/S${SLEVEL}${HACKNAME}

update_progressbar 64

# Make it stop at reboot (rc6), after the framework and before userstore
logmsg "I" "update" "creating reboot runlevel symlink"
[ -h /etc/rc6.d/K${KLEVEL}${HACKNAME} ] || ln -fs /etc/init.d/${HACKNAME} /etc/rc6.d/K${KLEVEL}${HACKNAME}

update_progressbar 72

# Make it stop at shutdown (rc0), after the framework and before userstore
logmsg "I" "update" "creating shutdown runlevel symlink"
[ -h /etc/rc0.d/K${KLEVEL}${HACKNAME} ] || ln -fs /etc/init.d/${HACKNAME} /etc/rc0.d/K${KLEVEL}${HACKNAME}

update_progressbar 80

# Make it stop when updating (rc3), after the framework and before the updater
logmsg "I" "update" "creating update runlevel symlink"
[ -h /etc/rc3.d/K${KLEVEL}${HACKNAME} ] || ln -fs /etc/init.d/${HACKNAME} /etc/rc3.d/K${KLEVEL}${HACKNAME}

update_progressbar 88

# Cleanup
logmsg "I" "update" "cleaning up"
rm -f ${HACKNAME}-init ${HACKNAME}.tar.gz

update_progressbar 96

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

return 0
