#! /bin/bash

napservers="-s 63.196.54.15:8888"
napservers="$napservers -s 63.196.54.248:8888"
napservers="$napservers -s 63.196.54.4:8888"
# etc

if [ $# -gt 0 ]; then
    action=$1
    shift
else
    action=start
fi

if [ $# -gt 0 ]; then
    target=$1
    shift
else
    target=all
fi

do_action () {
 local action target
 action=$1
 target=$2
   
 case $action in
  start )
    do_action stop $target

    if [ "$target" = all -o "$target" = gnut ]; then
	echo -n "Starting gnut "
	nohup gnut -d >/dev/null &
	echo $! > gnut.pid
	echo "(PID $!)"
    fi
    if [ "$target" = all -o "$target" = nap ]; then
	echo -n "Starting nap "
	nohup nap --daemon --reconnect --autorestart $napservers --log nap.log >/dev/null &
	echo $! > nap.pid
	echo "(PID $!)"
    fi
    ;;
  stop )
    if [ "$target" = all -o "$target" = gnut ] &&  [ -r gnut.pid ]; then
	pid=`cat gnut.pid`
	echo "Stopping gnut (PID $pid)"
	kill -KILL $pid
	rm gnut.pid
    fi
    if [ "$target" = all -o "$target" = nap ] &&  [ -r nap.pid ]; then
	pid=`cat nap.pid`
	echo "Stopping nap (PID $pid)"
	kill -KILL $pid
	rm nap.pid
    fi
    ;;
 esac
}

do_action $action $target
