#!/bin/bash
# A simple settings manager for the Hotpup daemon
# part of the "hotpup" daemon scripts
## yet another dubious Dougal script for Puppy Linux, July 2007
## now have automount option
## Latest update: August 2nd (fixed bug of being stuck on a setting, moved back to normal checkboxes)

BLANK_IMAGE=/usr/share/pixmaps/net-setup_btnsize.png

APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"

## Get current mode and turn checkboxes on
# get the appropriate line from xinitrc (need later for sed)
CURRENTLINE="`grep -F "$APPDIR/HotPup.sh" $HOME/.xinitrc`"

# remove '&' from end of line -- gives problems with sed
CURRENTLINE="${CURRENTLINE%&*}"
#CURRENTLINE="`echo "$CURRENTLINE" | tr -d '&' | tr -s ' '`"

# get current mode of running
case "$CURRENTLINE" in
 '#'*) OLDMODE="OFF" ;;
 *) OLDMODE="${CURRENTLINE#* }" ;;
esac 
# turn off all
DEF_NO_PARAMS="false" ; DEF_PERMON="false" ; DEF_CDROMON="false"
DEF_FLOPPYON="false" ; DEF_ALLON="false" ; DEF_AUTOMOUNT="false"
DEF_OFF="false"
VIS_NO_PARAMS="enabled" ; VIS_PERMON="enabled" ; VIS_CDROMON="enabled"
VIS_FLOPPYON="enabled" ; VIS_ALLON="enabled"  ; VIS_AUTOMOUNT="enabled"
   
# turn on required
if [ "$OLDMODE" ] ; then 
  case "$OLDMODE" in *OFF*) DEF_OFF="true" 
   VIS_NO_PARAMS="disabled" ; VIS_PERMON="disabled" ; VIS_CDROMON="disabled"
   VIS_FLOPPYON="disabled" ; VIS_ALLON="disabled"  ; VIS_AUTOMOUNT="disabled"
   ;; 
  esac
  case "$OLDMODE" in *perm*) DEF_PERMON="true" ;; esac
  case "$OLDMODE" in *cdrom*) DEF_CDROMON="true" ;; esac
  case "$OLDMODE" in *floppy*) DEF_FLOPPYON="true" ;; esac
  case "$OLDMODE" in *all*) DEF_ALLON="true" 
   VIS_NO_PARAMS="disabled" ; VIS_PERMON="disabled" ; VIS_CDROMON="disabled"
   VIS_FLOPPYON="disabled"
   ;; 
  esac
  case "$OLDMODE" in *automount*) DEF_AUTOMOUNT="true" ;; esac
else
  DEF_NO_PARAMS="true"
  VIS_PERMON="disabled" ; VIS_CDROMON="disabled" 
  VIS_FLOPPYON="disabled" ; VIS_ALLON="disabled"
fi
## Get orientation and set the right radiobutton on
CURRENT_ORIENT=`readlink $APPDIR/add_icons_for_device`
DEF1="false" ; DEF2="false" ; DEF3="false" ; DEF4="false"
DEF5="false" ; DEF6="false" ; DEF7="false" ; DEF8="false"

case "$CURRENT_ORIENT" in
 *add_icons_for_device1) DEF1="true" ;;
 *add_icons_for_device2) DEF2="true" ;;
 *add_icons_for_device3) DEF3="true" ;;
 *add_icons_for_device4) DEF4="true" ;;
 *add_icons_for_device5) DEF5="true" ;;
 *add_icons_for_device6) DEF6="true" ;;
 *add_icons_for_device7) DEF7="true" ;;
 *add_icons_for_device8) DEF8="true" ;;
esac

export HOTPUP_SETTINGS="<window title=\"HotPup設定マネージャ\" icon-name=\"gtk-preferences\" window-position=\"1\">
<vbox>
 <notebook labels=\"実行モード|アイコン配置\">
  <vbox>
    <pixmap><input file>$BLANK_IMAGE</input></pixmap>
    <text>
      <label>HotPupをどのパラメータで実行するか、ここで選択できます。:</label>
    </text>
    <pixmap><input file>$BLANK_IMAGE</input></pixmap>
    <hbox>
      <vbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>
        <checkbox>
	      <label>\"(なし)\"</label>
	      <variable>OPTION_NO_PARAMS</variable>
	      <action>if true disable:OPTION_PERMON</action>
	      <action>if true disable:OPTION_CDROMON</action>
	      <action>if true disable:OPTION_FLOPPYON</action>
	      <action>if true disable:OPTION_ALLON</action>
	      <action>if false enable:OPTION_PERMON</action>
	      <action>if false enable:OPTION_CDROMON</action>
	      <action>if false enable:OPTION_FLOPPYON</action>
	      <action>if false enable:OPTION_ALLON</action>
	      <default>$DEF_NO_PARAMS</default>
	      <visible>$VIS_NO_PARAMS</visible>
        </checkbox>
      </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>60</height>
        <default>\" 検出された取り外し可能デバイスだけにアイコン
を追加する。(CD/DVDと取り外し可能USBデバイ
スが当てはまります)。\"</default>
      </edit>
    </hbox>
    <hbox>
      <vbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>  
        <checkbox>
	      <label>永久</label>
	      <variable>OPTION_PERMON</variable>
	      <default>$DEF_PERMON</default> 
	      <visible>$VIS_PERMON</visible>
        </checkbox>
      </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>60</height>
        <default>\" 永久デバイス（=起動したハードディスクと
USBドライブ）のアイコンも追加する。\"</default>
      </edit>
    </hbox>
    <hbox>
      <vbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>  
        <checkbox>
	      <label>CDROM</label>
	      <variable>OPTION_CDROMON</variable>
	      <default>$DEF_CDROMON</default>
	      <visible>$VIS_CDROMON</visible>
        </checkbox>
      </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>40</height>
        <default>\" CD/DVDドライブが空いていても、常にデスク
トップアイコンを表示する。\"</default>
      </edit>
    </hbox> 
    <hbox>
      <vbox>
        <checkbox>
	      <label>フロッピ</label>
	      <variable>OPTION_FLOPPYON</variable>
	      <default>$DEF_FLOPPYON</default>
	      <visible>$VIS_FLOPPYON</visible>
        </checkbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>      
      </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>25</height>
        <default>\" フロッピディスクのアイコンを常に表示する。\"</default>
      </edit>
    </hbox> 
    <hbox>
      <vbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>    
        <checkbox>
	      <label>全て</label>
	      <variable>OPTION_ALLON</variable>
	      <action>if true disable:OPTION_NO_PARAMS</action>
	      <action>if true disable:OPTION_PERMON</action>
	      <action>if true disable:OPTION_CDROMON</action>
	      <action>if true disable:OPTION_FLOPPYON</action>
	      <action>if false enable:OPTION_NO_PARAMS</action>
	      <action>if false enable:OPTION_PERMON</action>
	      <action>if false enable:OPTION_CDROMON</action>
	      <action>if false enable:OPTION_FLOPPYON</action>
	      <default>$DEF_ALLON</default>
	      <visible>$VIS_ALLON</visible>
        </checkbox>
       </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>60</height>
        <default>\" 上記の全て。
すべての現在のデバイスにアイコンを表示し、
検出されたどんな新しいデバイスにも追加する。\"</default>
      </edit>
    </hbox> 
    <hbox>
      <vbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>   
        <checkbox>
	      <label>自動</label>
	      <variable>OPTION_AUTOMOUNT</variable>
	      <default>$DEF_AUTOMOUNT</default>
	      <visible>$VIS_AUTOMOUNT</visible>
        </checkbox>
      </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>40</height>
        <default>\" 各パーティションにアイコンが追加されたら、
自動的にマウントする。\"</default>
      </edit>
    </hbox> 
    <hbox>
      <vbox>
        <checkbox>
	      <label>オフ</label>
	      <variable>OPTION_OFF</variable>
	      <action>if true disable:OPTION_NO_PARAMS</action>
	      <action>if true disable:OPTION_PERMON</action>
	      <action>if true disable:OPTION_CDROMON</action>
	      <action>if true disable:OPTION_FLOPPYON</action>
	      <action>if true disable:OPTION_ALLON</action>
	      <action>if true disable:OPTION_AUTOMOUNT</action>
	      <action>if false enable:OPTION_NO_PARAMS</action>
	      <action>if false enable:OPTION_PERMON</action>
	      <action>if false enable:OPTION_CDROMON</action>
	      <action>if false enable:OPTION_FLOPPYON</action>
	      <action>if false enable:OPTION_ALLON</action>
	      <action>if false enable:OPTION_AUTOMOUNT</action>
	      <default>$DEF_OFF</default>
        </checkbox>
        <pixmap><input file>$BLANK_IMAGE</input></pixmap>    
       </vbox>
      <edit editable=\"false\" cursor-visible=\"false\">
        <variable>EDITOR2</variable>
        <width>300</width><height>25</height>
        <default>\" デーモンを停止する。\"</default>
      </edit>
    </hbox> 
  </vbox>
  <vbox>
    <text>
      <label>\"  \"</label>
    </text>
    <text>
<label>ここでは、マウントポイントアイコンをデスクトップのどの位置に置くか選択できます（現在のモードが選択されています）。</label>
    </text>
    <text>
      <label>\"  \"</label>
    </text>
    <radiobutton active=\"$DEF1\">
      <label>水平。画面下に沿って左から右へ</label>
      <variable>RADIOBUTTON1</variable>
    </radiobutton>
    <radiobutton active=\"$DEF2\">
      <label>水平。画面下に沿って右から左へ</label>
      <variable>RADIOBUTTON2</variable>
    </radiobutton>
    <radiobutton active=\"$DEF3\">
      <label>水平。画面上に沿って左から右へ</label>
      <variable>RADIOBUTTON3</variable>
    </radiobutton>
    <radiobutton active=\"$DEF4\">
      <label>水平。画面上に沿って右から左へ</label>
      <variable>RADIOBUTTON4</variable>
    </radiobutton>
    <radiobutton active=\"$DEF5\">
      <label>垂直。画面左に沿って上から下へ</label>
      <variable>RADIOBUTTON5</variable>
    </radiobutton>
    <radiobutton active=\"$DEF6\">
      <label>垂直。画面左に沿って下から上へ</label>
      <variable>RADIOBUTTON6</variable>
    </radiobutton>
    <radiobutton active=\"$DEF7\">
      <label>垂直。画面右に沿って上から下へ</label>
      <variable>RADIOBUTTON7</variable>
    </radiobutton>
    <radiobutton active=\"$DEF8\">
      <label>垂直。画面右に沿って下から上へ</label>
      <variable>RADIOBUTTON8</variable>
    </radiobutton>
  </vbox>
 </notebook>
 <hbox>
   <button>
		<label>適用</label>
		<input file stock=\"gtk-apply\"></input>
		<action>EXIT:apply</action>
	</button>
   <button cancel></button>
  </hbox>
</vbox>
</window>"

# run dialog
I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog3 --program HOTPUP_SETTINGS); do
	eval $STATEMENTS
done
IFS=$I
unset HOTPUP_SETTINGS

#gtkdialog3 --program=HOTPUP_SETTINGS
#exit

if [ "$EXIT" = "apply" ] ; then # apply new settings
  if [ "$OPTION_OFF" = "true" ] ; then # turn off
    NEWLINE="#$CURRENTLINE "
    #might need to do something about linking
  else
    if [ "$OPTION_ALLON" = "true" ] ; then # use "all"
      NEWLINE="$APPDIR/HotPup.sh all " # note space at end: & is untouched by sed
    elif [ "$OPTION_NO_PARAMS" = "true" ] ; then # don't use any params  
      NEWLINE="$APPDIR/HotPup.sh "
    else # need to add each individual case
      NEWPARAMS=""
      [ "$OPTION_PERMON" = "true" ] && NEWPARAMS="$NEWPARAMS perm"
      [ "$OPTION_CDROMON" = "true" ] && NEWPARAMS="$NEWPARAMS cdrom"
      [ "$OPTION_FLOPPYON" = "true" ] && NEWPARAMS="$NEWPARAMS floppy"
      # create the new line
      NEWLINE="$APPDIR/HotPup.sh $NEWPARAMS "
    fi
    # add automount, if needed
    [ "$OPTION_AUTOMOUNT" = "true" ] && NEWLINE="${NEWLINE}automount "
  fi
  
  ## done with creating NEWLINE, no look at linking (only link if changed)
  if [ "$RADIOBUTTON1" = "true" ] && [ "$DEF1" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device1 $APPDIR/add_icons_for_device
  elif [ "$RADIOBUTTON2" = "true" ] && [ "$DEF2" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device2 $APPDIR/add_icons_for_device
  elif [ "$RADIOBUTTON3" = "true" ] && [ "$DEF3" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device3 $APPDIR/add_icons_for_device
  elif [ "$RADIOBUTTON4" = "true" ] && [ "$DEF4" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device4 $APPDIR/add_icons_for_device
  elif [ "$RADIOBUTTON5" = "true" ] && [ "$DEF5" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device5 $APPDIR/add_icons_for_device
  elif [ "$RADIOBUTTON6" = "true" ] && [ "$DEF6" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device6 $APPDIR/add_icons_for_device
  elif [ "$RADIOBUTTON7" = "true" ] && [ "$DEF7" != "true" ] ; then
    ln -sf $APPDIR/add_icons_for_device7 $APPDIR/add_icons_for_device  
  elif [ "$RADIOBUTTON8" = "true" ] && [ "$DEF8" != "true" ] ; then  
    ln -sf $APPDIR/add_icons_for_device8 $APPDIR/add_icons_for_device  
  fi
  
  ## replace the old line with the new
  cp $HOME/.xinitrc $HOME/.xinitrc.old
  sed "s%$CURRENTLINE%$NEWLINE%" $HOME/.xinitrc > $HOME/.xinitrc.tmp
  # make sure it worked (didn't mess up xinitrc, that is...), else leave old
  if grep -F -q "$NEWLINE" $HOME/.xinitrc.tmp ; then
    mv -f $HOME/.xinitrc.tmp $HOME/.xinitrc
    chmod +x $HOME/.xinitrc
    Xdialog --title "完了" --msgbox "新しい設定は正しく適用されました。
適用を反映するにはXをリスタートして下さい" 0 0
  else
    Xdialog --title "失敗" --msgbox "残念、新しいモードの適用に失敗しました。
元に戻します。" 0 0 
  fi
fi

exit
