#! /bin/sh

# this starts/stops DidiWiki server
# ASSUMES A USER NAMED spot HAS BEEN SETUP
# GuestToo - Feb 7, 2005. Hacked a bit by BK -- 2005,2006.
# Designed for use with Puppy Linux, www.puppylinux.com

DIDI="/root/spot/didiwiki"

if ! MSG=`which gxmessage`
then
 MSG='xmessage'
fi

if ! grep spot /etc/group
then
 $MSG -center -bg "#ffc0c0" -title "DidiWiki" 'ユーザ「spot」はいません'
 exit 1
fi

if [ ! -r $DIDI ];then
 DIDI=`which didiwiki`
fi

if [ ! -r $DIDI ];then
 $MSG -center -bg "#ffc0c0" -title "DidiWiki" 'DidiWikiプログラムが見つかりません'
 exit 1
fi

#BK
PSSTATUS="`ps`"
if [ "`echo -n "$PSSTATUS" | grep 'didiwiki' | grep -v 'didiwiki\-gui'`" = "" ];then
 BUTTONS1="Start:21,Cancel:10"
 MSG1=" DidiWikiサーバを開始してブラウザを立ち上
げるにはStartボタンをクリックして下さい。
注意。DidiWikiはユーザ「spot」で実行します。"
 DIDIGO="no"
else
 BUTTONS1="Start:21,Stop:22,Cancel:10"
 MSG1="DidiWikiサーバはすでにユーザ「spot」で実行されています。
ブラウザを開いてDidiWikiを使うにはStartボタンをクリック。
あるいはサーバを停止するにはStopボタンをクリック。"
 DIDIGO="yes"
fi

$MSG -center -buttons "$BUTTONS1" -title "DidiWiki" "$MSG1"
case $? in

21)
RETVAL1=0
if [ "$DIDIGO" = "no" ];then
 su -c $DIDI - spot &
 #note, .didiwiki folder is created in /root/spot/.
 RETVAL1=$?
fi
if [ $RETVAL1 -eq 0 ];then
 ##hv3 seems to work fine...
 #if [ "`which hv3`" = "" ];then
 # defaultbrowser http://localhost:8000/
 #else
 # hv3 http://localhost:8000/
 #fi
 defaulthtmlviewer http://localhost:8000/
else
 $MSG -center -bg "#ffc0c0" -title "DidiWiki" 'エラー: DidiWikiは開始していません'
fi
;;

22)
killall -3 didiwiki
sleep 1
killall -9 didiwiki
ps | grep didiwiki
if [ $? -eq 0 ];then
 $MSG -center -bg "#d0ffd0" -title "DidiWiki" 'DidiWikiは停止しました'
else
 $MSG -center -bg "#ffc0c0" -title "DidiWiki" 'DidiWikiはまだ実行中です'
fi
;;

*) exit ;;

esac
