2007-11-13 04:50:41 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2011-06-26 06:32:18 +02:00
|
|
|
set -e -u
|
|
|
|
|
2010-10-08 04:41:02 +02:00
|
|
|
DIR=$(pwd)
|
2008-12-31 05:26:37 +01:00
|
|
|
XWRELAY=${DIR}/xwrelay
|
|
|
|
PIDFILE=${DIR}/xwrelay.pid
|
|
|
|
CONFFILE=${DIR}/xwrelay.conf
|
|
|
|
IDFILE=${DIR}/nextid.txt
|
2009-07-31 14:58:47 +02:00
|
|
|
CSSFILE=${DIR}/xwrelay.css
|
2011-06-26 06:32:18 +02:00
|
|
|
CONFFILE=${DIR}/xwrelay.conf
|
2009-07-31 14:58:47 +02:00
|
|
|
|
2010-10-08 04:48:38 +02:00
|
|
|
LOGFILE=/tmp/xwrelay_log_$$.txt
|
2009-07-30 14:46:40 +02:00
|
|
|
#LOGFILE=/dev/null
|
|
|
|
|
|
|
|
date > $LOGFILE
|
2007-11-13 04:50:41 +01:00
|
|
|
|
2010-09-15 06:18:04 +02:00
|
|
|
usage() {
|
|
|
|
echo "usage: $0 start | stop | restart | mkdb"
|
|
|
|
}
|
|
|
|
|
|
|
|
make_db() {
|
2011-06-26 06:32:18 +02:00
|
|
|
if [ ! -e $CONFFILE ]; then
|
|
|
|
echo "unable to find $CONFFILE"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
DBNAME=$(grep '^DB_NAME' $CONFFILE | sed 's,^.*=,,')
|
|
|
|
if [ -z "$DBNAME" ]; then
|
|
|
|
echo "DB_NAME keyword not found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
createdb $DBNAME
|
|
|
|
cat | psql $DBNAME --file - <<EOF
|
2010-09-29 03:06:50 +02:00
|
|
|
create or replace function sum_array( DECIMAL [] )
|
|
|
|
returns decimal
|
|
|
|
as \$\$
|
|
|
|
select sum(\$1[i])
|
|
|
|
from generate_series(
|
|
|
|
array_lower(\$1,1),
|
|
|
|
array_upper(\$1,1)
|
|
|
|
) g(i);
|
|
|
|
\$\$ language sql immutable;
|
|
|
|
EOF
|
|
|
|
|
2011-06-26 06:32:18 +02:00
|
|
|
cat | psql $DBNAME --file - <<EOF
|
2010-09-15 06:18:04 +02:00
|
|
|
CREATE TABLE games (
|
2010-10-07 06:45:05 +02:00
|
|
|
cid integer
|
|
|
|
,room VARCHAR(32)
|
2010-11-11 15:29:21 +01:00
|
|
|
,dead BOOLEAN DEFAULT FALSE
|
2010-10-07 06:45:05 +02:00
|
|
|
,lang INTEGER
|
|
|
|
,pub BOOLEAN
|
|
|
|
,connName VARCHAR(64) UNIQUE PRIMARY KEY
|
|
|
|
,nTotal INTEGER
|
|
|
|
,nPerDevice INTEGER[]
|
|
|
|
,seeds INTEGER[]
|
2011-06-21 03:10:07 +02:00
|
|
|
,ack VARCHAR(1)[]
|
2010-10-07 06:45:05 +02:00
|
|
|
,nSent INTEGER DEFAULT 0
|
2010-10-15 07:15:41 +02:00
|
|
|
,ctime TIMESTAMP (0) DEFAULT CURRENT_TIMESTAMP
|
2010-12-02 06:09:25 +01:00
|
|
|
,mtimes TIMESTAMP(0)[]
|
2010-09-15 06:18:04 +02:00
|
|
|
);
|
2010-09-23 15:31:42 +02:00
|
|
|
EOF
|
|
|
|
|
2011-06-26 06:32:18 +02:00
|
|
|
cat | psql $DBNAME --file - <<EOF
|
2010-09-23 15:31:42 +02:00
|
|
|
CREATE TABLE msgs (
|
2010-09-24 14:46:30 +02:00
|
|
|
id SERIAL
|
|
|
|
,connName VARCHAR(64)
|
|
|
|
,hid INTEGER
|
2010-10-19 05:19:09 +02:00
|
|
|
,ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
2010-09-24 14:46:30 +02:00
|
|
|
,msg BYTEA
|
2011-01-21 15:46:49 +01:00
|
|
|
,msglen INTEGER
|
2010-11-04 02:14:19 +01:00
|
|
|
,UNIQUE ( connName, hid, msg )
|
2010-09-23 15:31:42 +02:00
|
|
|
);
|
2010-09-15 06:18:04 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2008-12-31 05:26:37 +01:00
|
|
|
do_start() {
|
|
|
|
if [ -f $PIDFILE ] && [ -f /proc/$(cat $PIDFILE)/exe ]; then
|
2009-07-30 14:46:40 +02:00
|
|
|
echo "already running: pid=$(cat $PIDFILE)" | tee -a $LOGFILE
|
2009-11-10 15:37:42 +01:00
|
|
|
elif pidof $XWRELAY >/dev/null; then
|
2010-10-08 04:41:02 +02:00
|
|
|
echo "already running: pid($XWRELAY)=>$(pidof $XWRELAY)" | tee -a $LOGFILE
|
2008-12-31 05:26:37 +01:00
|
|
|
else
|
2009-07-30 14:46:40 +02:00
|
|
|
echo "starting..." | tee -a $LOGFILE
|
2009-07-31 14:58:47 +02:00
|
|
|
echo "running $XWRELAY $@ -f $CONFFILE -s $CSSFILE" | tee -a $LOGFILE
|
|
|
|
$XWRELAY $@ -f $CONFFILE -i $IDFILE -s $CSSFILE &
|
2008-12-31 05:26:37 +01:00
|
|
|
NEWPID=$!
|
2009-12-04 08:54:14 +01:00
|
|
|
echo -n $NEWPID > $PIDFILE
|
2008-12-31 05:26:37 +01:00
|
|
|
sleep 1
|
2009-07-30 14:46:40 +02:00
|
|
|
echo "running with pid=$(cat $PIDFILE)" | tee -a $LOGFILE
|
2008-12-31 05:26:37 +01:00
|
|
|
fi
|
|
|
|
}
|
2007-11-13 04:50:41 +01:00
|
|
|
|
2008-12-31 05:26:37 +01:00
|
|
|
case $1 in
|
2007-11-13 04:50:41 +01:00
|
|
|
|
|
|
|
stop)
|
2008-12-31 05:26:37 +01:00
|
|
|
shift
|
2007-11-22 05:59:11 +01:00
|
|
|
if [ -f $PIDFILE ] && [ -f /proc/$(cat $PIDFILE)/exe ]; then
|
2007-11-13 04:50:41 +01:00
|
|
|
sync
|
2009-07-30 14:46:40 +02:00
|
|
|
echo "killing pid=$(cat $PIDFILE)" | tee -a $LOGFILE
|
2007-11-13 04:50:41 +01:00
|
|
|
kill $(cat $PIDFILE)
|
|
|
|
else
|
2009-07-30 14:46:40 +02:00
|
|
|
echo "not running or $PIDFILE not found" | tee -a $LOGFILE
|
2009-11-10 15:37:42 +01:00
|
|
|
PID=$(pidof $XWRELAY || true)
|
2009-12-04 08:54:14 +01:00
|
|
|
if [ -n "$PID" ]; then
|
2009-07-30 14:46:40 +02:00
|
|
|
echo "maybe it's $PID; killing them" | tee -a $LOGFILE
|
2008-12-31 05:26:37 +01:00
|
|
|
kill $PID
|
|
|
|
fi
|
2007-11-13 04:50:41 +01:00
|
|
|
fi
|
2008-12-31 05:26:37 +01:00
|
|
|
rm -f $PIDFILE
|
2007-11-13 04:50:41 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
restart)
|
2008-12-31 05:26:37 +01:00
|
|
|
shift
|
2007-11-13 04:50:41 +01:00
|
|
|
$0 stop
|
|
|
|
sleep 1
|
2007-11-22 05:59:11 +01:00
|
|
|
$0 start $@
|
2007-11-13 04:50:41 +01:00
|
|
|
;;
|
|
|
|
|
2008-12-31 05:26:37 +01:00
|
|
|
start)
|
|
|
|
shift
|
|
|
|
do_start $@
|
|
|
|
;;
|
2010-09-15 06:18:04 +02:00
|
|
|
|
|
|
|
mkdb)
|
|
|
|
make_db
|
|
|
|
;;
|
|
|
|
|
2008-12-31 05:26:37 +01:00
|
|
|
*)
|
2010-09-15 06:18:04 +02:00
|
|
|
usage
|
|
|
|
exit 0
|
2007-11-13 04:50:41 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|