#!/bin/sh

KILL=kill
PORT=
PIDFILE=${SOCKS5_PIDFILE-/tmp/socks5.pid}

if [ "$#" != "0" ] ; then
	if [ "$1" = "-p" ] ; then
		if [ "$#" != "1" ] ; then
			shift
			PORT=$1
			shift
		fi
	fi
fi


if [ -z "$PORT" ] ; then 
	FILE=${PIDFILE}-1080
else
	FILE=${PIDFILE}-$PORT
fi

if [ ! -f "$FILE" -a "$#" != "2" ] ; then
	echo "PID file $FILE does not exist"
	exit
fi

case $# in
0)
	SIGNAL=-HUP
	PID=`cat $FILE`
	;;
1)  	SIGNAL=$1
	PID=`cat $FILE`
	;;
2)	SIGNAL=$1
	PID=$2
	;;
*)	
	echo "usage: $0 [-p port] [signal] [pid]"
	exit 1;
	;;
esac

case "$SIGNAL" in 
'-HUP')
	${KILL} -HUP ${PID}
	;;
'-1')
	${KILL} -1 ${PID}
	;;
*)
	${KILL} ${SIGNAL} -${PID}
	rm $FILE
	;;
esac

