#!/bin/sh
#
# $Id: cover-watchdog-helper 15011 2018-06-02 16:58:21Z NiLuJe $
#

# Get hackname from the script's path (NOTE: Will only work for scripts called from /mnt/us/${KH_HACKNAME}/bin)
KH_HACKNAME="${0##/mnt/us/}"
KH_HACKNAME="${KH_HACKNAME%%/bin/*}"

# Try to pull our custom helper lib
_KH_FUNCS="/mnt/us/${KH_HACKNAME}/bin/libkh5"
if [ -f ${_KH_FUNCS} ] ; then
    . ${_KH_FUNCS}
else
    # Pull default helper functions for logging
    _FUNCTIONS=/etc/upstart/functions
    [ -f ${_FUNCTIONS} ] && . ${_FUNCTIONS}
    # We couldn't get our custom lib, abort
    f_log W linkss script "" "couldn't source libkh5 from '${KH_HACKNAME}'"
    exit 0
fi

# Make sure the cover extraction script is exec'able (check if it exists first)
if [ -f ${LINKSS_COVER_EXTRACT} ] ; then
    [ -x ${LINKSS_COVER_EXTRACT} ] || chmod +x ${LINKSS_COVER_EXTRACT}
fi
# Make sure shlock is exec'able
[ -x ${COVERWD_LOCK_BIN} ] || chmod +x ${COVERWD_LOCK_BIN}
# Make sure our lockfile has somewhere to live
[ -d ${COVERWD_LOCK_DIR} ] || mkdir -p ${COVERWD_LOCK_DIR}

# Add the PID of the lipc-wait-event(s) to the list of running daemons to kill (FIXME: Make sure we only add *ours*...)
echo "$( pidof lipc-wait-event )" >> ${COVER_WATCHDOG_PID}
# Add our PID to the list of running daemons to kill
echo "$$" >> ${COVER_WATCHDOG_PID}

while read line ; do
    if echo ${line} | grep -q 'appActivating 1 "com.lab126.booklet.reader"' ; then
        # Yep, we just switched to the reader! Let's do our stuff in a locked session to avoid killing the CPU...
        if ${COVERWD_LOCK_BIN} -p $$ -f ${COVERWD_LOCK_FILE} ; then
            # Log our attempt at extracting a cover
            kh_msg "switched to the reader, trying to extract the cover" I q
            # Give us some time to settle...
            sleep 10
            # Try to extract the cover
            if [ -f "${LINKSS_COVER_EXTRACT}" ] ; then
                # Low priority, the convert step is somewhat CPU hungry...
                nice -n 15 ${LINKSS_COVER_EXTRACT}
                if [ $? -ne 0 ] ; then
                    # It failed, screensaver might be blank, or innacurate (last successful extract)
                    kh_msg "failed to extract or convert cover, screensaver will be blank or innacurate" W a "failed to process cover"
                fi
            fi
            # And then remove our lock file
            rm -rf ${COVERWD_LOCK_FILE}
        else
            # Meep! We're already locked doing an extract, something went wrong (or we switched to another book pretty fast)...
            # NOTE: I'm not sure we can even hit this codepath anymore...
            kh_msg "we're already extracting a cover via the watchdog, go away" W a "cover extract already in progress"
        fi
    elif [ -f "${LINKSS_BASEDIR}/periodicals" ] && echo ${line} | grep -q 'appActivating 1 "com.lab126.booklet.periodicals"' ; then
        # Yep, we just switched to the periodical reader! Let's do our stuff in a locked session to avoid killing the CPU...
        if ${COVERWD_LOCK_BIN} -p $$ -f ${COVERWD_LOCK_FILE} ; then
            # Log our attempt at extracting a cover
            kh_msg "switched to the periodical reader, trying to extract the cover" I q
            # Give us a lot of time to settle, the periodical reader doesn't automatically update the cc database...
            sleep 15
            # Try to extract the cover
            if [ -f "${LINKSS_COVER_EXTRACT}" ] ; then
                # Low priority, the convert step is somewhat CPU hungry...
                nice -n 15 ${LINKSS_COVER_EXTRACT}
                if [ $? -ne 0 ] ; then
                    # It failed, screensaver might be blank, or innacurate (last successful extract)
                    kh_msg "failed to extract or convert cover from a periodical, screensaver will be blank or innacurate" W a "failed to process periodical cover"
                fi
            fi
            # And then remove our lock file
            rm -rf ${COVERWD_LOCK_FILE}
        else
            # Meep! We're already locked doing an extract, something went wrong (or we switched to another book pretty fast)...
            # NOTE: I'm not sure we can even hit this codepath anymore...
            kh_msg "we're already extracting a periodical cover via the watchdog, go away" W a "periodical cover extract already in progress"
        fi
    fi
done
