#!/bin/sh
# crossloop -- restart crossfire-server in case of a crash

# Full path to server binary.
CF_BIN="/usr/pkg/bin/crossfire-server"

# Additional flags to pass to the server (comment out to disable).
CF_FLAGS="-disable-plugin cfrhg -disable-plugin cflogger -d"

# Directory to use for storing temporary runtime files. It is created if it
# does not exist. It should be dedicated to Crossfire, because it is saved
# in the event of a server crash.
CF_TMP="/tmp/crossfire"

# Directory for storing server logs (default $CF_TMP).
CF_LOGDIR="${CF_TMP}"

# Maximum number of restarts (set to zero to loop infinitely, default 100).
MAX_RESTART=100

#### END OF CONFIGURATION ####

# The current generation of the server.
generation=1

while [ ${MAX_RESTART} -ne ${generation} ]; do
    # Create folder to store temporary files if it doesn't exist.
    if [ ! -d ${CF_TMP} ]; then
        mkdir ${CF_TMP}
        chmod 700 ${CF_TMP}
    fi

    # Change to the ${CF_TMP} directory.
    cd ${CF_TMP}

    # Execute server with appropriate flags.
    echo "===>>> Starting server (generation ${generation})..."

    eval "${CF_BIN}" "${CF_FLAGS}" "-tmpdir ${CF_TMP}" \
        "-log ${CF_LOGDIR}/crossfire-`date +%Y-%M-%d-%H:%M`.log"

    # If a server crash occurred, save ${CF_TMP}.
    dump=`ls *core* 2> /dev/null`

    if [ ${?} -eq 0 ]; then
        echo "===>>> Server crashed; saving crash info and restarting..."
        cd ..
        mv ${CF_TMP} "crossfire-`date +%Y-%M-%d-%H:%M`.crash"
    else
        echo "===>>> Restarting server (press CTRL-C again to quit)..."
    fi

    # Increment server generation.
    generation=`expr ${generation} + 1`

    # Wait for 10 seconds before restarting again.
    sleep 10
done
