#!/bin/sh
# Begin $rc_base/init.d/tmpfs

. /etc/sysconfig/rc
. $rc_functions

eval $(/usr/local/bin/readhash /var/ipfire/main/settings)

if [ "$RRDLOG" = '' ]; then
	RRDLOG=/var/log/rrd
fi

case "$1" in
	start)
		$0 restore
		if [ ! -e $RRDLOG.bak/vnstat ]; then
			mkdir -p $RRDLOG.bak/vnstat
    		fi
		if [ ! -e $RRDLOG/vnstat ]; then
			mkdir -p $RRDLOG/vnstat
    		fi
		#
		# create some folders
		#
		if [ ! -e /var/lock/subsys ]; then
			mkdir -p /var/lock/subsys
		fi
		if [ ! -e /var/lock/time ]; then
			mkdir -p /var/lock/time
			chown nobody.root /var/lock/time
		fi
		if [ ! -e /var/run/clamav ]; then
			mkdir -p /var/run/clamav
			chown clamav:clamav /var/run/clamav
		fi
		if [ ! -e /var/run/cups ]; then
			mkdir -p /var/run/cups
		fi
		if [ ! -e /var/run/dbus ]; then
			mkdir -p /var/run/dbus
		fi
		if [ ! -e /var/run/mysql ]; then
			mkdir -p /var/run/mysql
			chown mysql:mysql /var/run/mysql
		fi
		if [ ! -e /var/run/pluto ]; then
			mkdir -p /var/run/pluto
			chmod 700 /var/run/pluto
		fi
		if [ ! -e /var/run/saslauthd ]; then
			mkdir -p /var/run/saslauthd
		fi

		#
		# Move /var/spool/cron to ramdisk and make a symlink
		#
		if [ ! -L /var/spool/cron ]; then
			cp -pR /var/spool/cron /var/log/rrd.bak/cron
			mv /var/spool/cron /var/log/rrd/cron
			ln -s /var/log/rrd/cron /var/spool/cron
		fi

		echo_ok
		;;
	stop)
		$0 backup
		;;

	backup)
		boot_mesg "Save ramdisk..."
		cp -pR $RRDLOG/* $RRDLOG.bak/
		evaluate_retval
		;;
	restore)
		if [ -e $RRDLOG.bak/cron/new.root ]; then
			if [ -e $RRDLOG.bak/cron/root ]; then
				rm -f $RRDLOG.bak/cron/new.root
			fi
		fi
		if [ -e $RRDLOG.bak ];then
			boot_mesg "Restore ramdisk..."
			cp -pR $RRDLOG.bak/* $RRDLOG/
		fi
		;;


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

# End $rc_base/init.d/tmpfs
