/
#!/bin/tcsh

#Creates a backup and emails it, by Igor van den Hoven.

#No restrictions on use. 12/19/2008

set tar_flags = "-zcf"
set player    = "no"
set area      = "no"
set doc       = "no"
set src       = "no"
set all       = "no"
set version   = "MyMud-1.0"
set email     = "name@whatever.com"

#alternative retrieve the version number in the src/VERSION file.
#set version=`head -n 1 src/VERSION`

#poll arguments

if ( "$1" == "-h" ) then
	echo "syntax   : backup [-h] [-player|-area|-doc|-src|-all]"
	echo ""

	echo "-all     : makes a backup of everything (quietly)"
	echo "-area    : makes a backup of the areafiles"
	echo "-doc     : makes a backup of the doc directory"
	echo "-player  : makes a backup of the playerfiles"
	echo "-src     : makes a backup of the source code"
	echo ""
	echo "If you give no arguments, I will create all backups"
	echo ""
	exit
endif

if ( "$1" == "-all" ) then
	set all = "yes"
endif
if ( "$1" == "-area" ) then
	set area = "yes"
endif
if ( "$1" == "-doc" ) then
	set doc = "yes"
endif
if ( "$1" == "-player" ) then
	set player = "yes"
endif
if ( "$1" == "-src" ) then
	set src	= "yes"
endif
if ( "$1" == "" ) then
	set all = "yes"
endif

if ( "$all" == "yes" ) then
	set area   = "yes"
	set doc    = "yes"
	set player = "yes"
	set src    = "yes"
endif

if (! -d backups) then
	mkdir backups
endif

#backup files.

if ( "$area" == "yes" ) then
	echo -n "backing up area files."
	echo -n "..."
	tar $tar_flags area.tar.gz area_current/
	echo -n "..."
	mv -f area.tar.gz backups/
	echo ". [done]"
endif

if ( "$doc" == "yes" ) then
	echo -n "backing up doc files."
	tar $tar_flags doc.tar.gz doc/
	echo -n "...."
	mv -f doc.tar.gz backups/
	echo ".... [done]"
endif

if ( "$player" == "yes" ) then
	echo -n "backing up player files."
	tar $tar_flags player.tar.gz player/
	echo -n "."
	mv -f player.tar.gz backups/
	echo -n "."
	set CURDATE = `date "+%Y-%m-%W"`
	echo -n "."
	if (! -d backups/usrbkup) then
		mkdir backups/usrbkup
	endif
	cp -f backups/player.tar.gz backups/usrbkup/player_${CURDATE}.tar.gz
	echo -n "."
	echo ". [done]"
endif

if ( "$src" == "yes" ) then
	echo -n "backing up source files."
	mkdir tmpsrc
	mv -f src/*.o tmpsrc/
	echo -n "."
	tar $tar_flags src.tar.gz src/
	echo -n "."
	mv src.tar.gz backups/
	echo -n "."
	if (! -d backups/old-sources) then
		mkdir backups/old-sources
	endif
	cp backups/src.tar.gz backups/old-sources/src-${version}.tar.gz
	echo -n "."
	mv -f tmpsrc/*.o src/
	rm -rf tmpsrc
	echo ". [done]"
endif

#email the backups

if ( "$all" == "yes" ) then
	echo -n "emailing the backups."
	tar $tar_flags $version.tar.gz backups/area.tar.gz backups/doc.tar.gz backups/src.tar.gz backups/player.tar.gz
	echo -n "..."
	echo "" | mailx -a $version.tar.gz -s "$version backup" $email
	echo -n "...."
	rm -f $version.tar.gz
	echo ". [done]"
endif