#!/extra/busybox ash

COMMAND=$1

# Startup sanity checks

if [ ! -x /extra/busybox ]; then
	echo "Cannot execute /extra/busybox... Is this a geniune Zefie release?"
	exit
fi

if [ ! -x /system/bin/getprop ]; then
	echo "Cannot execute /system/bin/getprop... /system not mounted?"
	exit
fi

if [ $(/extra/busybox mount | /extra/busybox grep /sdcard | /extra/busybox wc -l) = 0 ]; then
	echo "/sdcard not mounted. Please mount /sdcard and run this program again."
	exit
fi

if [ $(/system/bin/getprop | /extra/busybox grep svc.recovery | /extra/busybox wc -l) = 0 ]; then
	echo "You must execute this script in recovery mode."
	exit
fi

if [ $(/extra/busybox mount | /extra/busybox grep /data | /extra/busybox wc -l) = 0 ]; then
	if [ "$COMMAND" = "unmount" ]; then
		echo "/data already unmounted..."
		exit
	fi
	
	if [ "$COMMAND" = "umount" ]; then
		echo "/data already unmounted..."
		exit
	fi

	/extra/busybox mount -t rfs -o nosuid,nodev,xattr,check=no /dev/stl6 /data

	if [ "$COMMAND" = "mount" ]; then
		echo "/data mounted"
		exit
	fi
fi

if [ "$COMMAND" = "mount" ]; then
	echo "/data already mounted..."
	exit
fi



if [ $(df /sdcard | cut -d K -f 3 | sed -e "s/[ ]*/\:/g" | cut -d ":" -f 5 | tail -n 1) -lt $(df /data | cut -d K -f 3 | sed -e "s/[ ]*/\:/g" | cut -d ":" -f 4 | tail -n 1) ]
then
	echo "Insufficient space on SD Card to hold backup of /data"
	exit
fi

if [ $(/extra/busybox mount | /extra/busybox grep /data | /extra/busybox wc -l) = 1 ]; then
	# Umount /data before we begin	
	if [ $(/extra/busybox umount /data 2>&1 | grep "Device or resource busy" | /extra/busybox wc -l) = 1 ]; then
		echo "Error unmounting /data. Please verify nothing is using /data. Cannot Continue. ($?)"
	fi
fi

if [ "$COMMAND" = "unmount" ]; then
	echo "/data unmounted"
	exit
fi

if [ "$COMMAND" = "umount" ]; then
	echo "/data unmounted"
	exit
fi

# Okay, now we can resume..

case $COMMAND in
	wipe )
		/extra/fformat /dev/stl6;;

        backup )
		/extra/busybox mount -t rfs -o nosuid,nodev,xattr,check=no /dev/stl6 /data
		/extra/clean-tombstones
		/extra/busybox tar -cvf /sdcard/moment_data_partition_backup_$(date +%Y%m%d%H%M%S).tar /data | /extra/busybox grep -v "error exit delayed"
		/extra/busybox umount /data
		echo "Backed up /data to /sdcard/moment_data_partition_backup_$(date +%Y%m%d%H%M%S).tar...";;
	
        restore )
		FILE=$(/extra/busybox ls /sdcard/moment_data_* -r -1 | /extra/busybox head -n 1)

		if [ "$2" ]; then
			if [ ! -f "$2" ]; then
				echo "Error: $2 not found. Cannot restore."
				exit
			fi
			FILE=$2
		fi			

		if [ ! -f "$FILE" ]; then
			echo "No backups found. Cannot restore."
			exit;
		fi

		if [ "$FILE" = "/sdcard/moment_data_partition_backup.tar" ]; then
			echo "Old depreciated file found."
			echo "We will restore from this file, but we will remove it."
			echo "We will then create a new backup using the new format."
			echo "We will begin in 3 seconds."
			sleep 3
			echo "Starting restore process..."
			/extra/fformat /dev/stl6
			/extra/busybox mount -t rfs -o nosuid,nodev,xattr,check=no /dev/stl6 /data
			/extra/busybox tar -xvf /sdcard/moment_data_partition_backup.tar -C /
			echo "Restored /sdcard/moment_data_partition_backup.tar to /data..."
			sleep 3
			rm /sdcard/moment_data_partition_backup.tar
			echo "Removed depreciated file."
			echo "Beginning creation of new backup...";
			sleep 3
			/extra/clean-tombstones
			/extra/busybox tar -cvf /sdcard/moment_data_partition_backup_$(date +%Y%m%d%H%M%S).tar /data | /extra/busybox grep -v "error exit delayed"
			/extra/busybox umount /data
			echo "Backed up /data to /sdcard/moment_data_partition_backup_$(date +%Y%m%d%H%M%S).tar..."
			exit
		fi			

		echo "Starting restore process..."
		/extra/fformat /dev/stl6
		/extra/busybox mount -t rfs -o nosuid,nodev,xattr,check=no /dev/stl6 /data
		/extra/busybox tar -xvf $FILE -C /
		/extra/busybox umount /data
		echo "Restored $FILE to /data..."
		echo "Please note: We automatically unmount /data, so do not panic when you see it is empty :)";;

        clear-dalvik-cache )
		echo "Clearing dalvik-cache..."
		/extra/busybox mount -t rfs -o nosuid,nodev,xattr,check=no /dev/stl6 /data
		/extra/busybox rm /data/dalvik-cache/*
		/extra/busybox umount /data
		echo "dalvik-cache cleared.";;
        * )
		echo "Usage: $0 backup|restore|clear-dalvik-cache|mount|unmount|wipe [filename-for-restore]";;
esac
