#!/bin/sh
#
# chkconfig: - 02 98
# description: save/restore volatile files we keep
# in tmpfs note: this script is called by rc.sysinit
# & halt, don't run on it's own.

. /etc/init.d/functions

SAVEFILE=/var/saved-state.tgz
TEMPDIR=/var/impermanent

start() {
    echo -n "Restoring saved state files: "

    if ! mount -t tmpfs | grep -q $TEMPDIR; then
	# TEMPDIR is not mounted as a tmpfs, so
	# skip.
	echo "$TEMPDIR not mounted as tmpfs!"
	failure; echo; return 1
    fi
    if [ ! -r $SAVEFILE ]; then
        # $SAVEFILE tarball should have been created
	# by this script when system was shut down
	# last.
	echo "Tarball $SAVEFILE not found!"
	failure; echo; return 1
    fi

    if tar -C $TEMPDIR -z -x -f $SAVEFILE ; then
        success; echo
    else
        failure; echo; return 1
    fi
}

stop() {
    echo -n "Saving state files: "

    if ! mount -t tmpfs | grep -q $TEMPDIR; then
        # TEMPDIR is not mounted as a tmpfs, so skip.
	echo "$TEMPDIR not mounted as tmpfs!"
	failure; echo; return 1
    fi

    if tar -C $TEMPDIR -z -c -f $SAVEFILE . ; then
        success; echo
    else
        failure; echo; return 1
    fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac
