#!/bin/bash
#
# Code adapted for Puppy 1.0.9, JWM 1.7
# by Kwiller on 6-May-2006
#
###################

##-----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 "JWMタスクバーの設定" \
	--title "タスクバー" \
        --radiolist "トレイの場所オプションを選択して下さい" 13 46 2 \
"下"  "画面の下部にトレイ" $AT_BTM \
"上"  "画面の上部にトレイ" $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> $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="変更を保存"
    RESMSG="トレイは画面の $POSN"
    rm -f $CONFIG2
  else
    RESTOP="変更を保留"
    RESMSG="新しい設定は違っています。元のままにします。"
    mv $CONFIG2 $CONFIG
  fi
else
  RESTOP="変更の失敗"
  RESMSG="設定は修正されませんでした。"
  mv $CONFIG2 $CONFIG
fi

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

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