1#!/bin/sh
2#
3# keepitup
4# This is a hacked version of "isempireup" by Dave Nye
5# Modified for use with the Empire2 server by Ken Stevens 1995
6#
7# Usage:
8# keepitup -kill <game>
9# keepitup -start <game> <dir>
10# keepitup -check <game> <dir> <port> <coun> <rep>
11#
12# Portability:
13# You may need to put a '-' in front of the ps flags below
14
15_verbose=true
16tbase=/tmp/empcheck.$$
17
18trap 'rm -f $FNAMES ; exit' 0 1 2 3 15
19
20putmsg() {
21    if [ "$_verbose" = "true" ]; then
22	echo "`date` $*"
23    fi
24}
25
26ERR0="Server EOF"
27ERR1="connect: Connection refused"
28ERR2="Expecting 2, got unix socket connect: No such file or directory"
29MDOWN="The game is down"
30FLAGS="-d"
31
32killit() {
33    FK=${tbase}.kill
34    FNAMES="$FNAMES ${FK}.L ${FK}.P"
35    rm -f ${FK}.L ${FK}.P
36
37    ps auxw >${FK}.L
38    egrep "emp_server $FLAGS $1" ${FK}.L | egrep "$USER" >${FK}.P
39
40    SZ="`wc -l ${FK}.P | awk '{print $1}' -`"
41    if [ $SZ -gt 0 ]; then
42	PIDS=`awk '{rec = rec " " $2} END {print rec}' ${FK}.P`
43	putmsg "Found pids $PIDS to kill for emp_server $FLAGS"
44	kill $PIDS
45    fi
46    rm -f ${FK}.L ${FK}.P
47}
48
49kill9it() {
50    PROC=$1
51
52    FK=${tbase}.kill
53    FNAMES="$FNAMES ${FK}.L ${FK}.P"
54    rm -f ${FK}.L ${FK}.P
55
56    ps auxw >${FK}.L
57    egrep "$PROC" ${FK}.L | egrep "$USER" >${FK}.P
58
59    SZ="`wc -l ${FK}.P | awk '{print $1}' -`"
60    if [ $SZ -gt 0 ]; then
61	PIDS=`awk '{rec = rec " " $2} END {print rec}' ${FK}.P`
62	putmsg "Found pids " $PIDS " to kill for" $PROC
63	kill -9 $PIDS
64    fi
65    rm -f ${FK}.L ${FK}.P
66}
67
68restartgame() {
69    DIR=$1
70    UNIQ=$2
71
72    killit $UNIQ
73    sleep 1
74    cd $DIR/bin
75    ./emp_server $FLAGS $UNIQ &
76    echo "restarted $UNIQ"
77    cd $HOME
78}
79
80checkgame() {
81    DIR=$1
82    PORT=$2
83    COUN=$3
84    PASS=$4
85
86    EMPIREHOST=`/bin/hostname`
87    EMPIREPORT=$PORT
88    COUNTRY=$COUN
89    PLAYER=$PASS
90    export EMPIREHOST EMPIREPORT COUNTRY PLAYER
91
92    FN=${tbase}.client
93    rm -f ${FN}.I  ${FN}.1 ${FN}.2
94    FNAMES="$FNAMES ${FN}.I ${FN}.1 ${FN}.2"
95    echo "quit" >${FN}.I
96    echo "quit" >${FN}.I
97
98    kill9it emp_client
99    $DIR/bin/emp_client >${FN}.1 2>${FN}.2  <${FN}.I
100    STAT=$?
101    ER=`head -1 ${FN}.2`
102    NORM=`head -1 ${FN}.1`
103
104    if [ "$STAT" = "0" ]; then
105	ALLOK=true
106#	putmsg "All Ok"
107    elif [ "$ER" = "$ERR0" ]; then
108	ALLOK=true
109#	putmsg "All Ok, but bad return status"
110    elif [ "$ER" = "$ERR1" ]; then
111	ALLOK=false
112	putmsg "emp_server is down"
113    elif [ "$ER" = "$ERR2" ]; then
114	ALLOK=false
115	putmsg "emp_server is down"
116    else
117	ALLOK=false
118	putmsg "Something is wrong"
119    fi
120    rm -f ${FN}.I  ${FN}.1 ${FN}.2
121}
122
123while [ $# -gt 0 ]; do
124    case "$1" in
125	# -kill game
126	"-kill") shift
127	    if [ $# -lt 1 ]; then
128		echo "Expecting -kill <game>"
129		exit 1
130	    fi
131	    killit $1
132	    shift ;;
133	# -start game dir
134	"-start") shift
135	    if [ $# -lt 2 ]; then
136		echo "Expecting -start <game> <dir>"
137		exit 1
138	    fi
139	    restartgame $2 $1
140	    shift ;shift;;
141	# -check game dir port country user
142	"-check") shift
143	    if [ $# -lt 5 ]; then
144		echo "Expecting -check <game> <dir> <port> <tcoun> <tuser>"
145		exit 1
146	    fi
147	    checkgame $2 $3 $4 $5
148	    if [ "$ALLOK" != "true" ]; then
149		restartgame $2 $1
150	    fi
151	    shift ; shift ; shift ; shift ; shift ;;
152	*) echo "Unknown parameter found in: $*."
153	    echo "Aborting."
154	    exit 1 ;;
155    esac
156done
157