#!/bin/sh
#(c) Copyright 2013 Barry Kauler, bkhome.org
#2013 Licence GPL3 (/usr/share/doc/legal)
#called from /usr/local/pup_event/pup_event_d
#called if no uevents have occurred for 4 seconds.
#perform background housekeeping.
#130609 first version.
#130729 dump the /tmp/pup_event_drvs_optical mechanism.
#130807 L18L: gettext declaration moved to frontend_funcs.

OLDLANG=$LANG
export LANG=C

ZDRV='';ZDRVINIT='no' #these usually set in PUPSTATE.
. /etc/rc.d/PUPSTATE
. /etc/eventmanager #has RAMSAVEINTERVAL, ICONDESK, HOTPLUGNOISY, ICONPARTITIONS, BACKENDON, POWERTIMEOUT

. /usr/local/pup_event/frontend_funcs

[ "$RAMSAVEINTERVAL" = "" ] && RAMSAVEINTERVAL=30
RAMSAVEINTERVAL=`expr $RAMSAVEINTERVAL \* 60` #convert minutes to seconds.
[ "$POWERTIMEOUT" = "" ] && POWERTIMEOUT=0
POWERTIMEOUT=`expr $POWERTIMEOUT \* 60` #convert minutes to seconds.

#used in free_flash_func...
SIZE_MODS_M=0
if [ "$ZDRVINIT" = "yes" ];then 
 #all mods were in initrd at bootup, then moved to main f.s.
 SIZE_MODS_M=`du -m -s /lib/modules | cut -f 1`
fi
PREVSIZETMPM=0
PREVSIZEFREEM=0


SAVECNT=0
[ -f /tmp/pup_event_frontend_timeout_savecnt ] && SAVECNT="`cat /tmp/pup_event_frontend_timeout_savecnt`"
SAVECNT=`expr $SAVECNT + 4`

if [ $RAMSAVEINTERVAL -ne 0 -a $SAVECNT -gt $RAMSAVEINTERVAL ];then
 touch /tmp/snapmergepuppyrequest #request to savepuppy_func.
 SAVECNT=0
fi
echo -n "$SAVECNT" > /tmp/pup_event_frontend_timeout_savecnt

MOUSECNT=0
[ -f /tmp/pup_event_frontend_timeout_mousecnt ] && MOUSECNT="`cat /tmp/pup_event_frontend_timeout_mousecnt`"
MINUTECNT=0
[ -f /tmp/pup_event_frontend_timeout_minutecnt ] && MINUTECNT="`cat /tmp/pup_event_frontend_timeout_minutecnt`"

CURPOS1=""
[ -f /tmp/pup_event_frontend_curpos1 ] && CURPOS1="`cat /tmp/pup_event_frontend_curpos1`"

 if [ $POWERTIMEOUT -ne 0 ];then #power-off computer after inactivity.
  MOUSECNT=`expr $MOUSECNT + 4`
  echo -n "$MOUSECNT" > /tmp/pup_event_frontend_timeout_mousecnt
  MINUTECNT=`expr $MINUTECNT + 4`
  echo -n "$MINUTECNT" > /tmp/pup_event_frontend_timeout_minutecnt
  if [ $MINUTECNT -gt 59 ];then #come in here every minute...
   MINUTECNT=0
   echo -n "$MINUTECNT" > /tmp/pup_event_frontend_timeout_minutecnt
   CURPOS2="`getcurpos`"
   echo "MOUSECNT=$MOUSECNT CURPOS2=$CURPOS2" >> /tmp/pup_event_frontend_d_curpos.log
   [ "$CURPOS1" = "" ] && CURPOS1="$CURPOS2"
   if [ "$CURPOS1" != "$CURPOS2" ];then
    MOUSECNT=0
    echo -n "$MOUSECNT" > /tmp/pup_event_frontend_timeout_mousecnt
   fi
   CURPOS1="$CURPOS2"
   echo -n "$CURPOS1" > /tmp/pup_event_frontend_curpos1
   [ $MOUSECNT -gt $POWERTIMEOUT ] && wmpoweroff &
  fi
 fi
 #some apps should not be disturbed by this background stuff...
 RUNPS="`ps`"
 #w482 add xorriso... 111228 added removepreview.sh (in /usr/local/petget) but probably test for "petget" is already enough...
 #120512 Karl Godt: do not save when compiling, add: make, cc, new2dir. 120513 add -w option, and gcc, imake, cmake, gxine, gmplayer...
 [ "`echo "$RUNPS" | grep -w -E 'make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit|installpkg\.sh|downloadpkgs\.sh|removepreview\.sh'`" != "" ] && exit 1

 [ "$AUTOUNMOUNT" = "true" ] && auto_unmount_func #v424 automatially unmount a partition if no longer being used (AUTOUNMOUNT set in eventmanager).
 
 #monitor free memory, periodic save of tmpfs top layer...
 case $PUPMODE in
  3|7|13)
   free_flash_func
   savepuppy_func
  ;;
  16|24|17|25) #unipup.
   free_initrd_func
  ;;
  *)   
   free_func
  ;;
 esac
 [ "$ICONDESK" = "false" ] && continue #see /etc/eventmanager
 
 SCRN_X="`cat /tmp/pup_event_frontend_scrn_x`" #written by frontend_startup.
 SCRN_Y="`cat /tmp/pup_event_frontend_scrn_y`" #  "
 PROBEDISK="`cat /tmp/pup_event_probedisk_startup`" #130610 written by frontend_startup.
. /etc/rc.d/functions4puppy4 #130610

 #unfortunately, we have to poll at regular intervals to see if a cd is inserted...
# DRVS_OPTICAL="`cat /tmp/pup_event_drvs_optical`" #created in frontend_startup.
# for ONEOPTICAL in $DRVS_OPTICAL
 for ONEOPTICAL in $(ls /sys/block|grep '^sr') #130729
 do
  xONEOPTICAL="$(echo -n "$ONEOPTICAL" | cut -f 4 -d '/')"
  [ "$xONEOPTICAL" = "" ] && continue #precaution
  probe_optical_func $xONEOPTICAL
 done

 #poll to see if a ls120 or zip floppy diskette inserted...
 DRVS_FLOPPY="`cat /tmp/pup_event_drvs_floppy`" #created in frontend_startup.
 for ONEFLOPPY in $DRVS_FLOPPY
 do
  [ "$ONEFLOPPY" = "" ] && continue #precaution
  probe_floppy_func $ONEFLOPPY
 done

###END###
