#!/bin/ash
#(c) Copyright 2013 Barry Kauler bkhome.org
#2013 Licence GPL3 (/usr/share/doc/legal).
#this is startup code, called from /usr/local/pup_event/pup_event_frontend_d
# (which in turn is called from /root/.xinitrc when X starts).
#the code in this script was extracted from the retired script /sbin/pup_event_frontend_d.
#130609 first version.
#130807 L18L: gettext declaration moved to frontend_funcs.
#170310 support exfat. note, guessfs_type and blkid are busybox applets, do support exfat.
#170528 determine screen dimensions, moved to /root/.xinitrc

#export TEXTDOMAIN=pup_event_frontend_d
#export OUTPUT_CHARSET=UTF-8
#. gettext.sh

OLDLANG=$LANG #100613
export LANG=C #w004

sleep 3 #1 #let the dust settle after X has started. 120718 reduce 2 to 1. 121105 try 2, see also /usr/sbin/delayedrun. 121212
#w471 slow cpus need more delay (well, rox does)...
DELAYFACTOR=0
CPUMHZ=`grep -m 1 -i '^cpu MHz' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'` #my laptop: 933.000
if [ $CPUMHZ ];then
 [ $CPUMHZ -gt 100 ] && DELAYFACTOR=$((1600 / $CPUMHZ)) #120829 L18L: need 1 sec for my 1514 MHZ CPU.  #`expr 1100 \/ $CPUMHZ`
else #120718 raspi: 697.95  my laptop: 4789.47
 BOGOMIPS=`grep -m 1 -i '^bogomips' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'`
 [ $BOGOMIPS ] && [ $BOGOMIPS -gt 200 ] && DELAYFACTOR=`expr 2100 \/ $BOGOMIPS`
fi
[ $DELAYFACTOR -gt 0 ] && sleep $DELAYFACTOR

[ "$DISPLAY" = "" ] && exit 1 #X not running.

#v404 /usr/sbin/video-wizard (Xvesa) does create /var/local/pup_event_icon_change_flag (120213 changed path from /tmp)
#which /sbin/clean_desk_icons reads via .xinirc when X starts, to wipe all drive icons.
#however I think it would be good to test here also if X mode not yet settled on...
if [ -f /tmp/videomode ];then #only exists during X resolution setup.
 if [ "`readlink /usr/bin/X`" = "Xvesa" ];then
  while [ -f /tmp/videomode ];do
   sleep 1
  done
 fi
fi

#TOGGLE=1
echo -n "" > /tmp/pup_event_frontend_d_curpos.log #see frontend_timeout

ZDRV='';ZDRVINIT='no' #these usually set in PUPSTATE.
. /etc/rc.d/PUPSTATE
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/X11R7/bin"
#KERNVER="`uname -r`"
. /etc/eventmanager #has RAMSAVEINTERVAL, ICONDESK, HOTPLUGNOISY, ICONPARTITIONS, BACKENDON, POWERTIMEOUT
. /etc/rc.d/functions4puppy4

. /usr/local/pup_event/frontend_funcs #has create_icon_func

#170528 this code moved to /root/.xinitrc...
#SCRNXY="`xwininfo -root | grep -o '\-geometry .*' | tr -s ' ' | cut -f 2 -d ' ' | cut -f 1 -d '+'`"
#SCRN_X="`echo -n "$SCRNXY" | cut -f 1 -d 'x'`"
#SCRN_Y="`echo -n "$SCRNXY" | cut -f 2 -d 'x'`"
#echo -n "$SCRN_X" > /tmp/pup_event_frontend_scrn_x #read by frontend_change.
#echo -n "$SCRN_Y" > /tmp/pup_event_frontend_scrn_y #read by frontend_change.
SCRN_X="$(cat /tmp/pup_event_frontend_scrn_x)"
SCRN_Y="$(cat /tmp/pup_event_frontend_scrn_y)"

#RETVALm=1

######################################################################
#stuff to setup at entry...
mkdir -p /root/.pup_event

#build the desktop icons...
DRV_CATEGORY='drive'
PROBEDISK="`probedisk`"
echo "$PROBEDISK" > /tmp/pup_event_probedisk_startup #130610 read by frontend_timeout.
#v407 ls120/zip floppy drives are probed every 4 secs...
DRVS_FLOPPY="`echo "$PROBEDISK" | grep '|floppy|' | cut -f 1 -d '|' | cut -f 3 -d '/'`"
echo "$DRVS_FLOPPY" > /tmp/pup_event_drvs_floppy #also written by frontend_change, read by frontend_timeout.
#v407 same thing for all optical drives...
DRVS_OPTICAL="`echo "$PROBEDISK" | grep '|optical|' | cut -f 1 -d '|' | cut -f 3 -d '/'`"
echo "$DRVS_OPTICAL" > /tmp/pup_event_drvs_optical #130610 also written by frontend_change, read by frontend_timeout.
if [ "$ICONDESK" = "false" ];then
 #only show a single 'drives' icon on desktop...
 DRV_NAME='drives'
 DRV_CATEGORY='any' #see pmount.
 DRV_DESCRIPTION="all drives"
 create_icon_func #needs DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION.
else
 #show all drives on desktop... w476 add ext4... 130216 add f2fs... 170310 exfat...
 PROBEPART="`probepart -k | grep -E '\|exfat\||\|f2fs\||\|ext4\||\|ntfs\||\|msdos\||\|vfat\||\|ext2\||\|ext3\||\|iso9660\||\|udf\||\|audiocd\||\|xfs\||\|reiser'`"
 if [ "$FD0ICON" = "true" ];then #see /etc/eventmanager
  if [ -e /sys/block/fd0 ];then
   PROBEDISK="/dev/fd0|floppy|Legacy floppy drive
$PROBEDISK"
   PROBEPART="/dev/fd0|vfat|1440
$PROBEPART"
  fi
 fi
 for ONEDRV in `echo "$PROBEDISK" | cut -f 1,2,3 -d '|' | tr ' ' '_' | tr '\n' ' '`
 do
  DRV_NAME="`echo -n "$ONEDRV" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
  DRV_CATEGORY="`echo -n "$ONEDRV" | cut -f 2 -d '|'`"
  DRV_DESCRIPTION="`echo -n "$ONEDRV" | cut -f 3 -d '|' | tr '_' ' '`"
  [ "`echo "$PROBEPART" | grep "$DRV_NAME"`" = "" ] && continue #precaution (such as CD not inserted).
  create_icon_func startup #needs DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION, PROBEPART.
 done
fi

if [ "$BACKENDON" = "false" ];then #see /etc/eventmanager.
 killall udevd
 exit 2
fi
[ "$HOTPLUGON" = "false" ] && exit 3 #turn off all frontend hotplug support. see /etc/eventmanager.


exit 0
###END###
