#! /bin/sh

# Creates a minimal backup for a MUSH. Assumes default names.
# Run from the pennmush directory.
#
# Usage: ./makebackup.sh [backup file]

# If you have a different config file than game/mush.cnf, the script
# needs to be edited. See the CONF= line below.

# To restore with just the backup file:
#  Get the PennMUSH source tarball and extract it.
#  Run Configure as normal.
#  Extract the backup tarball from the pennmush directory.
#  Run make update and make and make install as normal.


# Written by Raevnos (raevnos@pennmush.org). No warrenty. 

# Bugs: Doesn't handle include directives in mush.cnf.
# It's slow.

#The main mush config file
CONF=game/mush.cnf

# News files. If you have your own in different places, add them here.
NEWS="game/txt/nws/*.nws"

#The name of the backup file. Either a command line argument or backup.tar.gz
BACKUP=${1:-"backup.tar.gz"}

# Figure out the compression suffix
CS=`egrep '^compress_suffix' $CONF | sed "s/ +/\t/" | cut -f2`

# Figure out the databases
OUTDB=`egrep '^output_database' $CONF | sed "s/ +/\t/" | cut -f2`
CHATDB=`egrep '^chat_database' $CONF | sed "s/ +/\t/" | cut -f2`
MAILDB=`egrep '^mail_database' $CONF | sed "s/ +/\t/" | cut -f2`
DATABASES="game/${OUTDB}${CS} game/${CHATDB}${CS} game/${MAILDB}${CS}"

CONFIG="options.h $CONF game/restrict.cnf"

# Message files. This isn't exactly effecient, but it works
TEXT=""
TEXTNAMES="access_file names_file connect_file connect_html_file motd_file motd_html_file wizmotd_file wizmotd_html_file quit_file quit_html_file newuser_file newuser_html_file down_file down_html_file register_create_file register_create_html_file guest_file guest_html_file full_file full_html_file"
for name in $TEXTNAMES; do
    file=`egrep "^${name}" $CONF | sed "s/ +/\t/" | cut -f2`
    if [ -f "game/$file" ]; then
        TEXT="${TEXT} game/$file"
     fi
done

echo Backing up files to ${BACKUP}... 
tar czvf $BACKUP $DATABASES $NEWS $CONFIG $TEXT
echo Done.