#!/bin/sh
#
# $Id: unlinkss 7431 2011-04-20 16:57:22Z NiLuJe $
#

# Pull some helper functions for logging
_FUNCTIONS=/etc/rc.d/functions
[ -f ${_FUNCTIONS} ] && . ${_FUNCTIONS}

LINKFONTS_BASEDIR="/mnt/us/linkfonts"

LINKSS_BASEDIR="/mnt/us/linkss"
WATCHDOG_PID="${LINKSS_BASEDIR}/run/usb-watchdog.pid"
WATCHDOG_DAEMON="${LINKSS_BASEDIR}/bin/usb-watchdog"
WATCHDOG_HELPER="${LINKSS_BASEDIR}/bin/usb-watchdog-helper"

K2_SCREEN_SIZE="600x800"
KDX_SCREEN_SIZE="824x1200"

LINKJAIL_BASEDIR="/mnt/us/linkjail"

# Trap official update files if we find any...
# Use the jailbreak's hack whitelist, if we have it
if [ -f "${LINKJAIL_BASEDIR}/etc/whitelist" ] ; then
    wl_hack_list="$( cat "${LINKJAIL_BASEDIR}/etc/whitelist" )"
else
    # If we don't, use a hard-coded list
    wl_hack_list="update_jailbreak update_ss update_fonts update_dummy update_usbnetwork update_sstimeout update_font_pkg Update_DK update_dk update_duokan update_unduokan update_adqdevkeys update_launchpad update_disable-autoupdates update_nolog update_freedownload update_keybru update_physkeyb update_updates-provider update_loc_ru update_loc_cz update_loc_cz2 update_loc_pl update_loc_ukr update_loc_bg update_backup update_reginfo update_checkmd5 update_emergency update_dev-key update_PublicKeys-dg update_k3-3.1-create_updater update_k3g-3.1-create_updater update_k3w-3.1-create_updater"
fi

# Trap updates by default
want_trap_update="true"

for wl_hackname in ${wl_hack_list} ; do
    for us_binfile in /mnt/us/*pdate*.bin ; do
        case "${us_binfile##*/}" in
            ${wl_hackname}*.bin )
                # Don't trap our own hacks' update files
                msg "skipping the trap of update binfile (${us_binfile##*/}) because it matches '${wl_hackname}'" I
                want_trap_update="false"
            ;;
        esac
    done
done

# Do we have something to trap?
if [ "${want_trap_update}" == "true" ] ; then
    # Found one! Make a copy of it :) Only if we don't already have one from unlinkfonts ;)
    if [ -f "${us_binfile}" -a ! -f "${LINKSS_BASEDIR}/${us_binfile##*/}" -a ! -f "${LINKFONTS_BASEDIR}/${us_binfile##*/}" ] ; then
        msg "trapping update binfile (${us_binfile##*/})" I
        cp -f "${us_binfile}" "${LINKSS_BASEDIR}/${us_binfile##*/}"
    fi
fi

# Kill our optionnal watchdog
if [ -f ${WATCHDOG_PID} ] ; then
    msg "start of USB watchdog kills" I
    # Can't use start-stop-daemon -K, we've got the bash loop, the watchdog daemon and its helper to kill.
    for pid in $( cat ${WATCHDOG_PID} ) ; do
        # Check that's it's really one of our own process before killing it mercilessly :p
        if ps -fp ${pid} | grep -e "${WATCHDOG_DAEMON}" -e "usbPlugOut" -e "${WATCHDOG_HELPER}" > /dev/null 2>&1 ; then
            msg "killing USB watchdog process (${pid})" I
            kill -KILL ${pid} 2> /dev/null
        fi
    done
    msg "end of USB watchdog kills" I
    # And remove the stale pidfile
    rm -f ${WATCHDOG_PID}
fi

# Do we have stuff to unmount?
# Handle both k2 & kDX
for kx_size in ${K2_SCREEN_SIZE} ${KDX_SCREEN_SIZE} ; do
    if [ -f ${LINKSS_BASEDIR}/mounted_${kx_size} ] ; then
        msg "unmounting custom screensavers (${kx_size})" I
        umount -l /opt/amazon/screen_saver/${kx_size}

        # And we're good!
        rm -f ${LINKSS_BASEDIR}/mounted_${kx_size}
    fi
done

# Do we have a custom version tag to unmount?
if [ -f ${LINKSS_BASEDIR}/ver_mounted ] ; then
    msg "unmounting custom version tag" I
    umount -l /etc/prettyversion.txt

    # And we're good!
    rm -f ${LINKSS_BASEDIR}/ver_mounted
fi
