#!/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
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}

##-----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

#-------------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----->>

POSN=`cat $TMP`

#-----If there new settings chosen then make the changes----->>
if [ "$POSN" = "TOP" ]; then
  if [ "$SET_TOP" -eq "0" ]; then
    sed -e "$SEDTOP" $CONFIG > $CONF
    mv $CONF $CONFIG
  fi
else
  if [ "$SET_TOP" -eq "1" ]; then
    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 [ "$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

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