#!/tmp/busybox sh
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# License GPLv2+: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>
#

#
# tar data backup and restore
# based on bonsai data backup/restore scripts
# by toadlife , SenseiSimple and the Epic 4G XDA community
#
# Call from your updater script like so...
#
# Backup: 	run_program("/tmp/tar-backup", "backup");
# Restore: 	run_program("/tmp/tar-backup", "restore"[, "delete"]);
#
# optional "delete" switch will cause the backup file to be deleted after 
# restore
#
#

romname=CleanGB
build=EI22
datadir=/data
PATH=/tmp:/sbin:$PATH
BB=/tmp/busybox
backfile=/sdcard/${build}_ROM_Backup/$romname.tgz

if [ "$1" = "backup" ]; then
	$BB rm /data/dalvik-cache/*
	$BB rm /data/lost+found/*
	$BB rm $backfile

	if $BB test -d $datadir; then
		$BB test ! -d /sdcard/${build}_ROM_Backup && $BB mkdir /sdcard/${build}_ROM_Backup
		$BB tar pczf $backfile $datadir
		if $BB test $? -ne 0; then
			echo "Backup failed!"
			exit 1
		else
			echo "Backup completed"
			exit 0
		fi
	fi
fi

if [ "$1" = "restore" ]; then
	if $BB test -f $backfile; then
		$BB tar pxzf $backfile
		echo Restore Completed!
		if [ "$2" = "delete" ]; then
			rm $backfile
		else
			DATE=`date +%Y.%m.%d.%I%M`;
			newromname=$romname.$DATE
			newbackfile=/sdcard/${build}_ROM_Backup/$newromname.tgz
			mv $backfile $newbackfile
		fi
		exit 0
	else
		echo Restore failed!
		exit 1
	fi
fi
	
