:
# NAME:
#	sigs - start/stop/check SignServerPool
#
# DESCRIPTION:
#	This script start/stop/checks SignServerPool.py
#
# SEE ALSO:
#	
#
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: rc_sigs,v 1.6 2018/03/26 22:26:05 sjg Exp $
#
#	@(#) Copyright (c) 2013 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

# need to set Myname
Myname=sigs
mydir=`dirname $0`

SIGNSERVERPOOL=${SIGNSERVERPOOL:-$mydir/SignServerPool.py}
test -x $SIGNSERVERPOOL || exit 0	# nothing to do
NAME=`basename $SIGNSERVERPOOL`

# we expect everything relative to where SignServerPool.py is
cd `dirname $SIGNSERVERPOOL`
test -s $Myname.rc && . ./$Myname.rc

SIGNSERVERPOOL=./`basename $SIGNSERVERPOOL`
if test -d conf; then
	CONF_DIR=${CONF_DIR:-conf}
fi

Echo() {
	echo "$@"
}

Start() {
	for conf in "$@"
	do
		$PYTHON $SIGNSERVERPOOL -c $conf $SIGNSERVERPOOL_OPTS &
		Echo -n " $NAME $conf"
	done
}

Stop() {
	t=shutdown
	for conf in "$@"
	do
		$PYTHON ./signctl.py -c $conf shutdown > /dev/null 2>&1 || continue
		Echo "$t $NAME $conf"
		t=
	done
}

Check() {
	e=Echo
	for conf in "$@"
	do
		$PYTHON ./signctl.py -c $conf check > /dev/null 2>&1 && continue
		$e -n restarting
		Start $conf
		e=:
	done
}

Status() {
	rc=0
	for conf in "$@"
	do
		Echo -n "$NAME $conf "
		if $PYTHON ./signctl.py -c $conf check > /dev/null 2>&1; then
			Echo running
		else
			Echo down
			rc=1
		fi
	done
	return $rc
}

case "$0" in
*check*)
	cmd=check;;
*)	cmd=$1;;
esac
case "$1" in
check|shut*|*start|stop|status) shift;;
esac

list_cf() {
      for f in "$@"
      do
              test -s $f && echo $f
      done
}

conf_list=${@:-${CONF_LIST:-`list_cf ${CONF_DIR:-.}/*.cf`}}

case "$cmd" in
""|start)	# default is start
	Start $conf_list
	;;
check)
	Check $conf_list
	;;
status)
        Status $conf_list
        exit $?
        ;;
restart)
	Stop $conf_list
	Check $conf_list
	;;
stop|shutdown)
	Stop $conf_list
	;;	
esac
x=$?
echo
exit $x
