#!/bin/sh
#
#	MakeSmoke - Kick off the automated build of smoke database.
#
PATH=/usr/ucb:/bin:/usr/bin:.; export PATH
#
#	Verify that file dependencies are in a pristine state.
#
GAMENAME=smoke
PIDFILE=$GAMENAME.pid
BIN=../mux/game/bin
TEXT=./text
DATA=./$GAMENAME.d
LOGDIR=$DATA
NEW_DB=$GAMENAME.db.new
INPUT_DB=$GAMENAME.db
GDBM_DB=$GAMENAME
CRASH_DB=$GAMENAME.db.CRASH
SAVE_DB=$GAMENAME.db.old
TMP=`mktemp`
#
#	Verify that temporary game directory does not already exist.
#
if [ -r $DATA ]; then
	echo "$DATA directory already exists."
	exit 1
fi
if [ -r $BIN/NETMUX ]; then
	echo "Build the server first."
	exit 1
fi
#
#	Make sure the smoke test isn't already running.
#
if [ -r "$PIDFILE" ]; then
    oldpid=`cat $PIDFILE`
    if [ "$oldpid" -gt 1 ]; then
        nmux=`ps | grep $oldpid | grep netmux | wc -l`
        if [ "$nmux" -gt 0 ]; then
            echo "A smoke test already seems to be running."
            exit 0
        fi
    fi
fi
perl ./tools/unformat.pl *.mux > $TMP
#
#	Create necessary environment.
#
mkdir $DATA
if [ ! -r text ]; then
    echo "Creating text directory."
    mkdir text
fi
cp ../mux/game/alias.conf .
cp ../mux/game/compat.conf .
cat > smoke.conf <<\_EOF
# smoke.conf - TinyMUX configuration file for smoke testing.
#
input_database	smoke.d/smoke.db
output_database	smoke.d/smoke.db.new
crash_database	smoke.d/smoke.db.CRASH
#
# Mail, comsystem, and macro databases.
#
mail_database   smoke.d/mail.db
comsys_database smoke.d/comsys.db
#
port 2860
mud_name SmokeMUX
#
command_quota_increment 1000
command_quota_max 1000
player_queue_limit 10000
#
include alias.conf
include compat.conf
_EOF
#
#	Kick off MUX
#

# Linux/Solaris
LD_LIBRARY_PATH=$BIN
export LD_LIBRARY_PATH

# Mac OS X / NeXTStep / Mach
DYLD_LIBRARY_PATH=$BIN
export DYLD_LIBRARY_PATH

# AIX
LIBPATH=$BIN
export LIBPATH

# HP-UX
SHLIB_PATH=$BIN
export SHLIB_PATH

(nohup $BIN/netmux -c $GAMENAME.conf -p $PIDFILE -e $LOGDIR >/dev/null 2>&1 &)

sleep 1
expect ./tools/upload.tcl $TMP
if [ $? -ne 0 ]; then
    echo "Upload failed."
    exit 1
fi
#
#	Wait for MUX to finish shutdown.
#
waited=0
while [ -r "$PIDFILE" ] && kill -0 `cat $PIDFILE 2>/dev/null` 2>/dev/null; do
    sleep 1
    waited=`expr $waited + 1`
    if [ "$waited" -ge 10 ]; then
        echo "MUX process still running after ${waited}s."
        exit 1
    fi
done
sleep 1

#
#	Convert the dump to a flatfile for the Smoke test.
#	With SQLite storage, there may be no smoke.db.new (the dump is
#	a WAL checkpoint, not a flatfile).  In that case, we use dbconvert
#	to load from the .pag/.dir (or .sqlite) and write a flatfile.
#
if [ -r "$DATA/$NEW_DB" ]; then
    $BIN/netmux -d "$DATA/$GAMENAME" -i "$DATA/$NEW_DB" -o smoke.flat -u
    if [ $? -ne 0 ]; then
        echo "Unflatten failed."
        exit 1
    fi
else
    # SQLite backend: no flatfile was produced.  Use dbconvert to
    # load from the existing database and write a flatfile.
    #
    $BIN/netmux -d "$DATA/$GAMENAME" -o smoke.flat -u
    if [ $? -ne 0 ]; then
        echo "Export from database failed."
        exit 1
    fi
fi
if [ ! -r smoke.flat ]; then
    echo "smoke.flat was not produced."
    exit 1
fi
./tools/scrubflat.sed < smoke.flat > $TMP
mv $TMP smoke.flat

#
#	Grab resulting log file and clean up.
#
rm smoke.conf $PIDFILE shutdown.status alias.conf compat.conf
rm -rf $DATA text
