#!/bin/bash
#
# Code adapted for Puppy 1.0.9, JWM 1.7
# by Kwiller on 6-May-2006
#
###################
# 23aug2010 shinobar: remove warnings at parsing new config
# 7apr2011 rodin.s: i18n
# 2013-12-13 01micko, add cmdline support, call from a script

export TEXTDOMAIN=puppy
. gettext.sh

##-----taskbarPlace----->>

TMP="/tmp/checklist.tmp.$$"
TOP="0"
BTM="-1"
CONFIG="/root/.jwmrc-tray"
CONFIG2="/root/.jwmrc-tray-bak"
CONF="/root/.jwmrc-tray-temp"

#----Set defaults---->>
AT_BTM=ON
AT_TOP=off

#----Are current settings non-default---->>
SET_TOP=`grep -c "y=\"$TOP\"" $CONFIG`

if [ "$SET_TOP" -eq "1" ]; then
    AT_BTM=off
    AT_TOP=ON
fi

#-----Backup current settings----->>
cp $CONFIG $CONFIG2

SEDTOP=s!y=\"$BTM\"!y=\"$TOP\"!g
SEDBTM=s!y=\"$TOP\"!y=\"$BTM\"!g

#-----accept commandline param--->>
if [ "$1" ];then 
 case $1 in
  top)echo TOP > $TMP ;;
  bottom)echo BOTTOM > $TMP ;;
  *)exit ;;
 esac
else # big if

#-------------gui----------->>

 Xdialog --backtitle "$(gettext 'JWM Taskbar Configuration')" \
	--title "$(gettext 'Taskbar')" \
        --radiolist "$(gettext 'Choose a tray placement option')" 13 46 2 \
"BOTTOM"  "$(gettext 'Tray at Bottom of Screen.')" $AT_BTM \
"TOP"  "$(gettext 'Tray at Top of Screen')" $AT_TOP 2>$TMP

 retval=$?

#--------cancel pressed----->>
 case $retval in
  1 | 255) exit 0;;
 esac

#---------save changes----->>
fi #end big if
   
POSN=`cat $TMP`

#-----If there new settings chosen then make the changes----->>
HIDE=$(grep autohide /root/.jwmrc-tray | cut -f 2 -d '"')
if [ "$POSN" = "TOP" ]; then
  if [ "$SET_TOP" -eq "0" ]; then
	[ "$HIDE" != "off" ] && sed -i "s/$HIDE/top/" $CONFIG
    sed -e "$SEDTOP" $CONFIG > $CONF
    mv $CONF $CONFIG
  fi
else
  if [ "$SET_TOP" -eq "1" ]; then
	[ "$HIDE" != "off" ] && sed -i "s/$HIDE/bottom/" $CONFIG
    sed -e "$SEDBTM" $CONFIG > $CONF
    mv $CONF $CONFIG
  fi
fi

#------check new configuration----->>
jwm -p 2>&1 | grep -v 'warning' > $TMP

CHECKCONF=`cat $TMP`
if [ "$POSN" = "TOP" ]; then
  RES=`grep -c "y=\"$TOP\"" $CONFIG`
else
  RES=`grep -c "y=\"$BTM\"" $CONFIG`
fi

#----notify of result----->>
if [ "$1" ];then
 [ "`find /tmp -name 'touchscreen*'`" ] || jwm -restart
 rm -f $TMP
 exit 0
 if [ "$RES" -eq "1" ]; then
  if [ -z $CHECKCONF ]; then 
    RESTOP="$(gettext 'Change Saved')"
    RESMSG="`eval_gettext \"The tray is at the \\\$POSN of the screen\"`"
    rm -f $CONFIG2
  else
    RESTOP="$(gettext 'Change Reversed')"
    RESMSG="$(gettext 'New config corrupt. Keeping original')"
    mv $CONFIG2 $CONFIG
  fi
 else
  RESTOP="$(gettext 'Change Failed')"
  RESMSG="$(gettext 'Configuration has not been altered')"
  mv $CONFIG2 $CONFIG
 fi

 Xdialog --title "$RESTOP" --msgbox "$RESMSG" 0 0
fi

#--------clean exit------->>
rm -f $TMP
jwm -restart
exit 0
