#!/bin/sh
#
# Copyright (C) 2004 by Oleg I. Vdovikin <oleg@cs.msu.su>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

VERSION="$(cat /.version)"

case "$1" in
	status)
		if [ "$(nvram get boot_local)" = "$VERSION" ]; then
			echo "boot_local $VERSION"
		else 
			echo "boot_local disabled"
		fi
		;;
	start)
		if [ "$(nvram get boot_local)" = "$VERSION" ]; then
			tar -C / -xzf /dev/mtdblock/4
		fi
		;;
	enable)
		nvram set boot_local="$VERSION"
		nvram commit
		;;
	disable)
		nvram set boot_local
		nvram commit
		;;
	clear)
		erase /dev/mtd/4
		;;
	load)
		tar -C / -xzvf /dev/mtdblock/4
		;;
	list)
		tar -C / -tzf /dev/mtdblock/4
		;;
	save)
		[ -f /usr/local/.files ] && FILES=$(cat /usr/local/.files)
		tar -C / -czvf /tmp/flash.tar.gz /tmp/local $FILES && 
		ls -l /tmp/flash.tar.gz &&
		echo "Check saved image and type \"$0 commit\" to commit changes"
		;;
	commit)
		flash /tmp/flash.tar.gz /dev/mtd/4 &&
		echo "Commited."
		;;
	*)
		echo "Usage: $0 status|enable|disable|clear|load|list|save|commit"
		;;
esac
