#!/bin/csh -f
#
# Change the following preferences to determine what takes place
# when your site is automatically backed up. This script is 
# called by the MUCK code according to the @tune archive_interval
# setting, but you can also run it manually. 

# NOTES:
# In order to use as little disk space as possible with the
# autoarchiving, turn off ARCHIVE_LOGS, turn on REMOVE_DUMPS, 
# and CLEAR_HOSTCACHE, keep the ARCHIVE_MUF number low, and 
# make sure to remove all the *.o files from the src/ directory 
# ahead of time. 
# If you decide to use ARCHIVE_LOGS, remember to clean out the oldlogs
# directory on occasion, since those can really build up!

# MUCK_NAME       - The name to archive the site under. 
# DUMP_NAME       - The name you use for your data base dumps.
# ARCHIVE_LOGS    - Set this to 1 to archive logs, otherwise they 
#                   are deleted.
# LOGS_TO_ARCHIVE - A space seperated list of the logs to archive.
# ARCHIVE_MUF     - The number of MUF archives to keep, or 0 to not
#                   archive MUF.
# CLEAR_HOSTCACHE - Clears the nethost.cache file in proto/game/
# REMOVE_DUMPS    - To clear out the backup data base dumps.
# ARCHIVE_SITE    - Set to 1 to make a tar.gz of the whole site.

set MUCK_NAME =       protomuck 
set DUMP_NAME =       proto.new
set ARCHIVE_LOGS =    0 
set LOGS_TO_ARCHIVE = 'status connects'
set ARCHIVE_MUF =     3 
set CLEAR_HOSTCACHE = 1
set REMOVE_DUMPS =    1
set ARCHIVE_SITE =    1

# The following determine the location of where things end up.
# Make sure they match your local setup.

set HOME =        $HOME
set PROTODIR =    $HOME/proto
set GAMEDIR =     $HOME/proto/game
set MUFDIR =      $HOME/proto/game/muf
set LOGDIR =      $HOME/proto/game/logs
set DUMPDIR =     $HOME/proto/game/backup/data
set MUFARCHIVE =  $HOME/proto/game/backup/muf
set LOGARCHIVE =  $HOME/proto/game/logs/oldlogs
set SITEARCHIVE = $HOME

###################################################################
# You should not normally need to change anything after this point.
# Unless you want to make it so that some of the logs do not get
# cleared. See below

set timestamp = `date +'%y.%m.%d.%H.%M'`

# Archive logs. 
if ( $ARCHIVE_LOGS >= 1 ) then
echo "Archiving logs to $LOGARCHIVE/logs.$timestamp.tgz"
# We need to tar up the logs and timestamp them.
    if (! -d $LOGDIR ) then
        mkdir $LOGDIR
    endif 
    cd $LOGDIR
    tar -zcvf logs.$timestamp.tgz $LOGS_TO_ARCHIVE
# Check that the logarchive directory exists and make it if needed.
    if (! -d $LOGARCHIVE ) then
        mkdir $LOGARCHIVE
    endif 
    mv -f logs.$timestamp.tgz $LOGARCHIVE  
    cd $LOGDIR
endif 
# Now clear the old logs
echo "Removing old log files."
cd $LOGDIR

# Just comment out the ones you don't want getting cleared.
rm -f ttyerr.log 
rm -f status
rm -f commands
rm -f connects
rm -f programs
rm -f files
rm -f sockets
rm -f help
rm -f restarts
rm -f muf-errors
touch $LOGDIR/ttyerr.log
touch $LOGDIR/status

set mcount = $ARCHIVE_MUF 
set ncount = $ARCHIVE_MUF  
@ ncount -= 1
# Archiving MUF.
if ( $ARCHIVE_MUF >= 1 ) then
    echo "Archiving MUF to $MUFARCHIVE/muf1.tgz"
    if ( ! -d $MUFARCHIVE ) then
        mkdir $MUFARCHIVE
    endif 
    cd $MUFARCHIVE
# Moving the old archives out of place.
    while ( $mcount >= 1 ) 
        if ( -r muf$ncount.tgz ) then
            mv -f muf$ncount.tgz muf$mcount.tgz
        endif
        @ mcount = $mcount - 1
        @ ncount = $ncount - 1
    end 
# Now remove the extra one
    @ ncount = $ARCHIVE_MUF + 1
    if ( -r muf$ncount.tgz ) then
        rm -f muf$ncount.tgz
    endif
# Now to make the new archive!
    cd $MUFDIR
    tar -zcf muf1.tgz *.m macros
    mv -f muf1.tgz $MUFARCHIVE
endif

# Clear that pesky nethost.cache
if ( $CLEAR_HOSTCACHE >= 1 ) then
    echo "Removing nethost.cache"
    cd $GAMEDIR
    rm nethost.cache
endif

# Clear out the past dumps in game/backup/data/
if ( $REMOVE_DUMPS >= 1 ) then
    if ( -d $DUMPDIR ) then
echo "Clearing out stored up data base dumps." 
        cd $DUMPDIR
        rm -f $DUMP_NAME.* 
    endif 
endif 

# Archive the entire site
if ( $ARCHIVE_SITE >= 1 ) then
    echo "Archiving entire site to $SITEARCHIVE/$MUCK_NAME.tgz"
    cd $SITEARCHIVE
    tar -zcf $MUCK_NAME.tgz $PROTODIR 
endif

echo "`date` - $MUCK_NAME ARCHIVED." >> $LOGDIR/status
