#!/bin/sh
# Copyright 1992 NEC Corporation, Tokyo, Japan.
#
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without
# fee, provided that the above copyright notice appear in all copies
# and that both that copyright notice and this permission notice
# appear in supporting documentation, and that the name of NEC
# Corporation not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.  NEC Corporation makes no representations about the
# suitability of this software for any purpose.  It is provided "as
# is" without express or implied warranty.
#
# NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN 
# NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 
# OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
# PERFORMANCE OF THIS SOFTWARE. 

# $Id: kill.cpp,v 5.3 1994/04/20 03:46:51 kon Exp $























CANNALOCKFILE="/var/spool/canna/lock/.CANNALOCK";
MAXLOOP=20;

# main()
{
    if [ $# -gt 1 ]; then
	echo 'Usage: cannakill [ServerNumber|-all|-help]';
	exit 2;
    fi;

    if [ "$1" = "" -o "$1" = "0" ]; then {
	if [ ! -r $CANNALOCKFILE ]; then
	    echo "Error: cannaserver is not running.";
	    exit 1;
	fi;
	PID=`cat $CANNALOCKFILE 2> /dev/null`;
	if [ "$PID" = "" ]; then {
	    echo "Error: cannaserver is not running.";
	    rm -f $CANNALOCKFILE;
	    exit 1;
	} fi;
	kill $PID;
	if [ $? != 0 ]; then {
	    echo "Error: cannaserver is not running.";
	    exit 1;
	} fi
	LOOPN=0;
	while : ; do {
	    if [ ! -r "$CANNALOCKFILE" ]; then
		break;
	    fi;
	    sleep 1;
	    LOOPN=`expr $LOOPN + 1`;
	    if [ $LOOPN -gt $MAXLOOP ]; then {
		echo "give up to terminate cannaserver !!";
		exit 1;
	    } fi;





	} done;
	exit 0;
    } fi;
    case $1 in
	-h*)
		echo 'Usage: cannakill [ServerNumber|-all|-help]';
		exit 0;
		;;
	-a*)
		for LOCKF in $CANNALOCKFILE*; do {
		    if [ ! -r "$LOCKF" ]; then
			continue;
		    fi
		    PID=`cat -s $LOCKF`;
		    if [ "$PID" = "" ]; then {
			rm -f $LOCKF;
			continue;
		    } fi;
		    kill $PID;
		    if [ $? = 0 ]; then {
			LOOPN=0;
			while : ; do {
			    if [ ! -r "$LOCKF" ]; then
				break;
			    fi;
			    sleep 1;
			    LOOPN=`expr $LOOPN + 1`;
			    if [ $LOOPN -gt $MAXLOOP ]; then {
				echo "give up to terminate cannaserver !!";
				break;
			    } fi;





			} done;
			continue;
		    } fi;
		    echo "Error: cannaserver($2) is not running.";
		    continue;
		} done;
		exit 0;
		;;
	*)
		if [ ! -r "$CANNALOCKFILE:$1" ]; then
		    echo "Error: cannaserver($1) is not running.";
		    exit 1;
	        fi;
		PID=`cat $CANNALOCKFILE:$1 2> /dev/null`;
		if [ "$PID" = "" ]; then {
		    echo "Error: cannaserver($1) is not running.";
		    rm -f $CANNALOCKFILE:$1;
		    exit 1;
		} fi;
		kill $PID;
		if [ $? = 0 ]; then {
		    LOOPN=0;
		    while : ; do {
			if [ ! -f "$CANNALOCKFILE:$1" ]; then
			    break;
			fi;
			sleep 1;
			LOOPN=`expr $LOOPN + 1`;
			if [ $LOOPN -gt $MAXLOOP ]; then {
			    echo "give up to terminate cannaserver !!";
			    break;
			} fi;





		    } done;
		    exit 0;
		} fi;
		echo "Error: cannaserver($2) is not running.";
		exit 1;
		;;
    esac;
}
