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

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

TMP="/tmp/checklist.tmp.$$"
ISSET="true"
ISOFF="false"
CONFIG="/root/.jwmrc-tray"
CONFIG2="/root/.jwmrc-tray-bak"
CONF="/root/.jwmrc-tray-temp"

#----Set defaults---->>
AUTO_OFF=ON
AUTO_SET=off

#----Are current settings non-default---->>
SET_AHD=`grep -c "autohide=\"$ISSET\"" $CONFIG`

if [ "$SET_AHD" -eq "1" ]; then
    AUTO_OFF=off
    AUTO_SET=ON
fi

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

SEDSET=s!autohide=\"$ISOFF\"!autohide=\"$ISSET\"!g
SEDOFF=s!autohide=\"$ISSET\"!autohide=\"$ISOFF\"!g


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

Xdialog --backtitle "JWMタスクバーの設定" \
	--title "タスクバー" \
        --radiolist "トレイを自動的に隠すオプションを選択" 13 46 2 \
"OFF"  "トレイを自動的に隠さない" $AUTO_OFF \
"ON"    "トレイを自動的に隠す" $AUTO_SET 2>$TMP

retval=$?

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

#---------save changes----->>

OPTN=`cat $TMP`

#-----If there new settings chosen then make the changes----->>
if [ "$OPTN" = "ON" ]; then
  if [ "$SET_AHD" -eq "0" ]; then
    sed -e "$SEDSET" $CONFIG > $CONF
    mv $CONF $CONFIG
  fi
else
  if [ "$SET_AHD" -eq "1" ]; then
    sed -e "$SEDOFF" $CONFIG > $CONF
    mv $CONF $CONFIG
  fi
fi

#------check new configuration----->>
jwm -p 2> $TMP

CHECKCONF=`cat $TMP`

if [ "$OPTN" = "ON" ]; then
  RES=`grep -c "autohide=\"$ISSET\"" $CONFIG`
else
  RES=`grep -c "autohide=\"$ISOFF\"" $CONFIG`
fi

#----notify of result----->>

if [ "$RES" -eq "1" ]; then
  if [ -z $CHECKCONF ]; then 
    RESTOP="変更を保存"
    RESMSG="トレイを自動的に隠すオプションは $OPTN"
    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
