#!/bin/bash
#
# minecraft backup script
#
# chmod 755 minecraft-backup.sh
# crontab -e
# ----
# 5 1 * * * /usr/bin/nice /home/minecraft/minecraft/minecraft-backup.sh "world1" "world2" > /home/minecraft/minecraft/minecraft-backup.log 2>&1
# ----

readonly MCPATH='/home/minecraft/minecraft'
readonly BACKUPPATH='/media/remote.share/minecraft.backup'
readonly HISTORY=$(date '+%w')
readonly CMDNAME=`basename $0`

usage_exit() {
	echo "Usage: $CMDNAME <world1> <world2> ..." 1>&2
	exit 1
}

main(){
	if [ ! -d "${BACKUPPATH}" ]; then
		echo -e "ERROR\tpath not found. [${BACKUPPATH}]" 1>&2
		usage_exit
	fi

	local world
	for world in "$@"; do
		if [ ! -d "${MCPATH}/${world}" ]; then
			echo -e "ERROR\tworld not found. [${world}]" 1>&2
			continue
		fi
		rm -f "${BACKUPPATH}/${world}.${HISTORY}.zip"
		cd "${MCPATH}";/usr/bin/zip -qr9 "${BACKUPPATH}/${world}.${HISTORY}.zip" "${world}" "${world}_nether" "${world}_the_end"
	done
}

# check command args.
if [ $# -lt 1 ]; then
    usage_exit
fi

main "$@"

# vim: ts=4 sw=4
