#!/bin/sh
#Barry Kauler 2008.
#Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html). 2007 www.puppylinux.com
#a gui to provide choices for "events" -- things that occur at a systems level,
#usually hardware related.
#v411 checkbox to redraw/realign icons.
#v412 workaround, someone exited with invalid settings.
#w007 mouse inactivity computer power-down.
#w464 /etc/puppyversion is now history.
#v424 automatic unmounting (see pup_event_frontend_d).

# i18n gettext, eval_gettext
export TEXTDOMAIN=pup_event
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
#. /usr/bin/gettext.sh
#

SHOWMODE=$1 #v405 optional to modify gui.

. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS
PUPPYVERSION="$DISTRO_VERSION"
KERNVER="`uname -r`"
. /etc/rc.d/MODULESCONFIG

[ -f /etc/eventmanager ] && . /etc/eventmanager
[ "$ICONDESK" != "true" -a "$ICONDESK" != "false" ] && ICONDESK="true"
[ ! $RAMSAVEINTERVAL ] && RAMSAVEINTERVAL=30
[ "$HOTPLUGNOISY" != "true" -a "$HOTPLUGNOISY" != "false" ] && HOTPLUGNOISY="false"
[ "$AUTOTARGET" != "true" -a "$AUTOTARGET" != "false" ] && AUTOTARGET="true"
[ "$ICONPARTITIONS" != "true" -a "$ICONPARTITIONS" != "false" ] && ICONPARTITIONS="true"
[ "$HOTPLUGON" != "true" -a "$HOTPLUGON" != "false" ] && HOTPLUGON="true"
[ "$BACKENDON" != "true" -a "$BACKENDON" != "false" ] && BACKENDON="true"
[ "$FD0ICON" != "true" -a "$FD0ICON" != "false" ] && FD0ICON="true"
[ ! $POWERTIMEOUT ] && POWERTIMEOUT=0 #w007 0=never.
[ ! $AUTOUNMOUNT ] && AUTOUNMOUNT="false" #v424

#w007 as have a optional cutdown gui, make sure these are all preset...
NEWICONDESK="$ICONDESK"
NEWRAMSAVEINTERVAL=$RAMSAVEINTERVAL
NEWHOTPLUGNOISY="$HOTPLUGNOISY"
NEWAUTOTARGET="$AUTOTARGET"
NEWICONPARTITIONS="$ICONPARTITIONS"
NEWHOTPLUGON="$HOTPLUGON"
NEWBACKENDON="$BACKENDON"
NEWFD0ICON="$FD0ICON"
NEWPOWERTIMEOUT=$POWERTIMEOUT
NEWAUTOUNMOUNT="$AUTOUNMOUNT"

T_Ac="`gettext \"Activate\"`"
T_Sa="`gettext \"Save Session\"`"
T_DI="`gettext \"Desktop Icons\"`"
T_IH="`gettext \"Icon Handler\"`"
T_Le="`gettext \"Legacy\"`"
T_Po="`gettext \"Power\"`"

T_notebook_labels_desk="${T_DI}|${T_IH}|${T_Le}"
T_notebook_labels="${T_Ac}|${T_Sa}|${T_notebook_labels_desk}|${T_Po}"

T_title_desk="`gettext \"Event Manager: Desktop drive icons\"`"
T_title="`gettext \"Puppy Event Manager\"`"


title="$T_title"
labels="$T_notebook_labels"
if [ "$SHOWMODE" = "desktop" ];then
 #cutdown, only show choices for settng the desktop icons...
 title="$T_title_desk"
 labels="$T_notebook_labels_desk"
 full_event="
   <button>
    <label>`gettext \"Show full Event Manager\"`</label>
    <action type=\"exit\">fullem</action>
   </button>
   "
else
 frames_Activate_Save="
  <frame `gettext \"Activate Puppy Event Manager\"`>
  <text><label>`gettext \"The 'pup_event' Puppy Event Manager runs in the background and handles hotplugging of drives, including the desktop drive icons. This also includes automatic module and firmware loading if new hardware is detected. However, turn it all off, and Puppy will revert to the same behaviour as versions prior to 4.x, in which there is no hotplug support. This reduces resource usage, so try unticking these on very old slow PCs. Pmount can still be run manually to mount and unmount partitions.\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for hotplug module/firmware loading\"`</label>
   <default>${BACKENDON}</default>
   <variable>NEWBACKENDON</variable>
  </checkbox>

  <hbox>
  <text><label>\"  \"</label></text>
  <vbox>
  <text><label>`gettext \"If the above is ticked, then can also turn on these:\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for desktop hotplug support\"`</label>
   <default>${HOTPLUGON}</default>
   <variable>NEWHOTPLUGON</variable>
  </checkbox>
  <checkbox>
   <label>`gettext \"Tick box for auto unmounting of partitions\"`</label>
   <default>${AUTOUNMOUNT}</default>
   <variable>NEWAUTOUNMOUNT</variable>
  </checkbox>
  <text><label>`gettext \"(WARNING: auto unmounting is highly experimental)\"`</label></text>
  </vbox>
  </hbox>
 </frame>

 <frame `gettext \"Periodic save from RAM\"`>
 <vbox>
  <text><label>`eval_gettext \"Puppy runs in a particular 'state' that we call the PUPMODE. This depends on the type of installation and the type of hardware. Currently, PUPMODE=\\\${PUPMODE}. If it is an odd number, for example 13, then Puppy is doing everything in RAM and saving the current session to a drive periodically -- this is done in the case of Flash memory to make it last longer. When Puppy runs in an odd PUPMODE, there will be a 'save' icon on the desktop, also a save occurs at shutdown. You can also specify a periodic save here:\"`</label></text>
  <hbox>
   <text><label>`gettext \"Save interval (0=never):\"`</label></text>
   <entry max_length=\"4\">
    <default>${RAMSAVEINTERVAL}</default>
    <variable>NEWRAMSAVEINTERVAL</variable>
   </entry>
   <text><label>`gettext \"minutes\"`</label></text>
   <text><label>\"       \"</label></text>
  </hbox>
  <text><label>`gettext \"NOTE1: PUPMODE=77 (multisession DVD) excluded, only manual save with desktop icon and at shutdown.\"`</label></text>
  <text><label>`gettext \"NOTE2: PUPMODE=5 there are no saves, above value ignored.\"`</label></text>
 </vbox>
 </frame>
 "
 framePower="

 <frame `gettext \"Power\"`>
 <vbox>
  <text><label>`gettext \"Puppy is able to power-off your computer after a period of mouse inactivity. This will occur if the mouse cursor has not been moved for the designated interval:\"`</label></text>
  <hbox>
   <text><label>`gettext \"Inactivity interval (0=never):\"`</label></text>
   <entry max_length=\"4\">
    <default>${POWERTIMEOUT}</default>
    <variable>NEWPOWERTIMEOUT</variable>
   </entry>
   <text><label>`gettext \"minutes\"`</label></text>
   <text><label>\"       \"</label></text>
  </hbox>
 </vbox>
 </frame>
 
 " 
fi 

export MAIN_DIALOG="<window title=\"${title}\" icon-name=\"gtk-execute\">
<vbox>

  <text use-markup=\"true\">
  <label>\"<b>`gettext \"Restart X for changes to take effect\"`</b>\"</label></text>

<notebook labels=\"${labels}\">

$frames_Activate_Save

 <frame `gettext \"Drive icons on desktop\"`>
 <vbox>
  <text><label>`gettext \"When this box is ticked, there will be an icon for each drive. If you plugin a USB pen drive for example, an icon will appear. Unplug and it will disappear. If you don't want these drive icons on the desktop, untick this box (but there will still remain just one icon that will launch Pmount when clicked on). The individual drive icons are purely a convenience and Puppy works fine without them.\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for drive icons on desktop\"`</label>
   <default>${ICONDESK}</default>
   <variable>NEWICONDESK</variable>
  </checkbox>


  <hbox>
  <text><label>\"  \"</label></text>
  <vbox>
  <text><label>`gettext \"If the above is ticked, then you can choose to have an icon for each partition, rather than one icon for the entire drive:\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for an icon for each partition\"`</label>
   <default>${ICONPARTITIONS}</default>
   <variable>NEWICONPARTITIONS</variable>
  </checkbox>
  </vbox>
  </hbox>

  <checkbox>
   <label>`gettext \"Tick box to erase then redraw and realign existing icons\"`</label>
   <default>false</default>
   <variable>NEWICONWIPE</variable>
  </checkbox>

 </vbox>
 </frame>

 <frame `gettext \"Drive 'handler'\"`>
 
 <vbox>
  <text><label>`gettext \"The 'handler' decides what to do when you click on a desktop drive icon. Normally, this just starts Pmount if you click on a drive icon, or mounts it and starts ROX-Filer if a partition icon, but this behavior can be extended by these checkboxes.\"`</label></text>
  
  <text><label>`gettext \"This checkbox extends the default behavior, and will launch a more appropriate application. Currently, it adds detection of a video DVD or audio CD and launches the media player:\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for auto-detection of target application\"`</label>
   <default>${AUTOTARGET}</default>
   <variable>NEWAUTOTARGET</variable>
  </checkbox>
 </vbox>
  
 <vbox>
  <text><label>`gettext \"Normally you have to click on a drive icon to run the 'handler', but this checkbox makes the 'handler' run as soon as a media is plugged in. For example, plug in a DVD and it will immediately either get mounted, or if the above checkbox is ticked the media player will run:\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for auto-launch 'handler' when media plugged in\"`</label>
   <default>${HOTPLUGNOISY}</default>
   <variable>NEWHOTPLUGNOISY</variable>
  </checkbox>
 </vbox>

 </frame>
 
 <frame `gettext \"Legacy\"`>
  <text><label>`gettext \"Linux does not support hotplug detection of when a legacy floppy diskette is inserted or removed. Periodic probing requires starting the drive motor, which is very slow -- and do you want the motor to startup every 4 seconds? Therefore, if your PC has a floppy drive, tick this box to have a permanent icon on desktop (regardless whether a diskette is actually inserted, or even a floppy drive!).\"`</label></text>
  <checkbox>
   <label>`gettext \"Tick box for floppy drive icon on desktop\"`</label>
   <default>${FD0ICON}</default>
   <variable>NEWFD0ICON</variable>
  </checkbox>
 </frame>

$framePower

 </notebook>

 <hbox>
   $full_event
   <button ok></button>
   <button cancel></button>
 </hbox>
</vbox>
</window>

"

RETSTRING="`gtkdialog3 --program=MAIN_DIALOG --center`"
[ $? -ne 0 ] && exit

eval "$RETSTRING"

#echo "$RETSTRING" #TEST

[ "$EXIT" = "abort" ] && exit
[ "$EXIT" = "Cancel" ] && exit
[ "$EXIT" = "" ] && exit

#v405 it is running with 'desktop' param, now rerun with all tabs...
[ "$EXIT" = "fullem" ] && exec eventmanager

#make sure that RAMSAVEINTERVAL is an integer only...
[ "$NEWRAMSAVEINTERVAL" = "" ] && NEWRAMSAVEINTERVAL=30 #412
NEWRAMSAVEINTERVAL=`echo -n "$NEWRAMSAVEINTERVAL" | sed -e 's/[^0-9]//g'`
#do not allow too-frequent saves...
[ $NEWRAMSAVEINTERVAL -gt 0 -a $NEWRAMSAVEINTERVAL -lt 5 ] && NEWRAMSAVEINTERVAL=5

#w007 make sure that POWERTIMEOUT is integer only...
[ "$NEWPOWERTIMEOUT" = "" ] && NEWPOWERTIMEOUT=0
NEWPOWERTIMEOUT=`echo -n "$NEWPOWERTIMEOUT" | sed -e 's/[^0-9]//g'`
#do not allow too-frequent power-down...
[ $NEWPOWERTIMEOUT -gt 0 -a $NEWPOWERTIMEOUT -lt 5 ] && NEWPOWERTIMEOUT=5

#v403 if major-switch has turned off hotplug support, only show one icon...
[ "$NEWBACKENDON" = "" ] && NEWBACKENDON="true" #v412
[ "$NEWHOTPLUGON" = "" ] && NEWHOTPLUGON="true" #v412
[ "$NEWBACKENDON" = "false" ] && NEWHOTPLUGON="false"
if [ "$NEWHOTPLUGON" = "false" ];then
 NEWICONDESK="false"
 NEWICONPARTITIONS="false"
 NEWHOTPLUGNOISY="false"
 NEWAUTOTARGET="false"
 NEWAUTOUNMOUNT="false" #v424
fi

#v403 /sbin/clean_desk_icons can read this, then wipe all current icons...
[ "$ICONPARTITIONS" != "$NEWICONPARTITIONS" ] && echo "ICONPARTITIONS" > /tmp/pup_event_icon_change_flag
[ "$ICONDESK" != "$NEWICONDESK" ] && echo "ICONDESK" > /tmp/pup_event_icon_change_flag
[ "$NEWICONWIPE" = "true" ] && echo "ICONWIPE" > /tmp/pup_event_icon_change_flag #v411

CONFIG="`cat /etc/eventmanager`"

rsPATTERN="s/^RAMSAVEINTERVAL=.*/RAMSAVEINTERVAL=${NEWRAMSAVEINTERVAL}/"
CONFIG="`echo "$CONFIG" | sed -e "$rsPATTERN"`"

#w007...
ptPATTERN="s/^POWERTIMEOUT=.*/POWERTIMEOUT=${NEWPOWERTIMEOUT}/"
CONFIG="`echo "$CONFIG" | sed -e "$ptPATTERN"`"

idPATTERN="s/^ICONDESK=.*/ICONDESK=${NEWICONDESK}/"
CONFIG="`echo "$CONFIG" | sed -e "$idPATTERN"`"

ipPATTERN="s/^ICONPARTITIONS=.*/ICONPARTITIONS=${NEWICONPARTITIONS}/"
CONFIG="`echo "$CONFIG" | sed -e "$ipPATTERN"`"

hnPATTERN="s/^HOTPLUGNOISY=.*/HOTPLUGNOISY=${NEWHOTPLUGNOISY}/"
CONFIG="`echo "$CONFIG" | sed -e "$hnPATTERN"`"

atPATTERN="s/^AUTOTARGET=.*/AUTOTARGET=${NEWAUTOTARGET}/"
CONFIG="`echo "$CONFIG" | sed -e "$atPATTERN"`"

hoPATTERN="s/^HOTPLUGON=.*/HOTPLUGON=${NEWHOTPLUGON}/"
CONFIG="`echo "$CONFIG" | sed -e "$hoPATTERN"`"

boPATTERN="s/^BACKENDON=.*/BACKENDON=${NEWBACKENDON}/"
CONFIG="`echo "$CONFIG" | sed -e "$boPATTERN"`"

fiPATTERN="s/^FD0ICON=.*/FD0ICON=${NEWFD0ICON}/"
CONFIG="`echo "$CONFIG" | sed -e "$fiPATTERN"`"

auPATTERN="s/^AUTOUNMOUNT=.*/AUTOUNMOUNT=${NEWAUTOUNMOUNT}/" #v424
CONFIG="`echo "$CONFIG" | sed -e "$auPATTERN"`"

echo "$CONFIG" > /etc/eventmanager

###END###
