#!/bin/sh

# roxapp to install and run Monkey Web Server
# monkey.conf is configured to run as user nobody
# web files are in /root/monkey/public_html
# config file is /root/monkey/conf/monkey.conf
# GuestToo - Nov 17, 2006

APPDIR=`dirname "$0"`
cd "$APPDIR"
APPDIR="`pwd`"
MSG=`which gxmessage` || MSG=xmessage

# newer versions of Puppy have cp-FULL
[ -d /root/monkey ] || mkdir /root/monkey
[ -d /root/monkey/bin ] || cp -a $APPDIR/bin/ /root/monkey/
[ -d /root/monkey/conf ] || cp -a $APPDIR/conf/ /root/monkey/
[ -d /root/monkey/logs ] || mkdir /root/monkey/logs
[ -d /root/monkey/public_html ] || cp -a $APPDIR/public_html/ /root/monkey/
ln -sfn "$APPDIR/bin/banana" /root/my-applications/bin/banana

chown -R nobody:nobody /root/monkey/

if pidof monkey > /dev/null
then

$MSG -bg "#d0ffd0" -buttons "Stop,Kill,Show,Cancel" -default Cancel -center -title "Monkey" "Monkey Web Server is running"
case $? in
  101)
    killall -q -2 monkey
    sleep 1
    exec "$APPDIR/AppRun"
    exit ;;
  102)
    killall -q -9 monkey
    sleep 1
    exec "$APPDIR/AppRun"
    exit ;;
  103)
    $MSG -timeout 2 -default okay -center -title "Monkey" "starting browser" &
	PORT=`grep ^Port /root/monkey/conf/monkey.conf | awk '{print $2}'`
	exec defaultbrowser "http://127.0.0.1:$PORT/"
	exit ;;
  *) exit ;;
esac

else

$MSG -bg "#ffc0c0" -buttons "Start,Kill,Cancel" -default Cancel -center -title "Monkey" "Monkey Web Server is NOT running"
case $? in
  101)
    killall -q -9 monkey
    sleep 1
    /root/monkey/bin/monkey -Dc /root/monkey/conf &
    sleep 1
    exec "$APPDIR/AppRun"
    exit ;;
  102)
    killall -q -9 monkey
    sleep 1
    exec "$APPDIR/AppRun"
    exit ;;
  *) exit ;;
esac

fi
