#!/bin/sh
#
# ipbx This shell script takes care of the RAM disk for the iPBX
#               
#
# chkconfig: 2345 90 60
# description: iPBX RAM Disk
# processname: none

# Source function library.
. /etc/rc.d/init.d/functions
. /usr/local/astium/etc/product

RETVAL=0

# See how we were called.
case "$1" in
  start)
        # Start daemons.
	#set -x
        echo -n "Starting $prod_name RAM Disk: "
	# ensure we have a mount point for the ramdisk
	if [ ! -d /ipbxrd ]
	then
		mkdir -p /ipbxrd
		chmod 755 /ipbxrd
	fi
	# ensure there's a line in /etc/fstab
	HAVERD=`grep /ipbxrd /etc/fstab | grep -v grep | wc -l`
	if [ "$HAVERD" = "0" ]
	then
		RAM=`free | grep Mem | awk '{ print $2; }'`
		# This will end up being about 5% (50M per 1G)
		RDSIZE=`expr $RAM / 20000`
		printf "tmpfs\t\t/ipbxrd\t\ttmpfs\tsize=%dM 0 0\n" $RDSIZE >> /etc/fstab
	fi
	# now do the mount
	mounted=`mount|grep /ipbxrd`
	if [ -z "$mounted" ]
	then
		mount /ipbxrd
	fi
	# handling for the asterisk database. Ensure we have a dir to link to
	if [ ! -d /ipbxrd/var/lib/asterisk ]
	then
		mkdir -p /ipbxrd/var/lib/asterisk
		chmod 755 /ipbxrd/var/lib/asterisk
		chown asterisk.asterisk /ipbxrd/var/lib/asterisk
	fi
	pushd /var/lib/asterisk > /dev/null
	# if there is no persistent file, then it's our first run
	if [ ! -f astdb.persistent -a -f astdb ]
	then
		mv astdb astdb.persistent
	fi
	# copy the persistent file (which holds registrations from before the reboot) to the ramdisk
	cp -a astdb.persistent /ipbxrd/var/lib/asterisk/astdb
	# ensure the soft link exists
	ln -sf /ipbxrd/var/lib/asterisk/astdb astdb
	popd > /dev/null

	# handling for the recordings. Ensure we have a dir to link to
	if [ ! -d /ipbxrd/var/spool/asterisk/monitor/ramdisk ]
	then
		mkdir -p /ipbxrd/var/spool/asterisk/monitor/ramdisk
		chmod 755 /ipbxrd/var/spool/asterisk/monitor/ramdisk
		chown asterisk.asterisk /ipbxrd/var/spool/asterisk/monitor/ramdisk
	fi
	pushd /var/spool/asterisk/monitor/ > /dev/null
	if [ -d ramdisk ]
	then
		/usr/local/astium/sbin/clean-recordings-ramdisk
		rmdir ramdisk
	fi
	if [ ! -d ramdisk -a ! -L ramdisk ]
	then
		ln -sf /ipbxrd/var/spool/asterisk/monitor/ramdisk # the man pages say -f will remove the files and replace with links,
	fi                                                        # BUT, that does not apply to directories!
	popd > /dev/null

	daemon /bin/true
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ipbxrd
        ;;
  stop)
        # Stop daemons.
	if [ -d /ipbxrd/var/spool/asterisk/monitor/ramdisk ]
	then
		cd /ipbxrd/var/spool/asterisk/monitor/ramdisk/
		for i in *.WAV *.wav
		do
			if [ -f $i ]
			then
				cp $i /var/spool/asterisk/monitor
			fi
		done
	fi
        echo -n "Stopping $prod_name RAM Disk: "
	mounted=`mount|grep /ipbxrd`
	if [ ! -z "$mounted" ]
	then
		umount /ipbxrd
	fi
	if [ -L /var/spool/asterisk/monitor/ramdisk ]
	then
		rm -f /var/spool/asterisk/monitor/ramdisk # this should remove the link!
		mkdir /var/spool/asterisk/monitor/ramdisk # we must leave a ramdisk dir if we remove the link
	fi
	daemon /bin/true
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ipbxrd
        ;;
  status)
	isrunning=`mount | grep /ipbxrd`
        if [ -n "$isrunning" ]
	then
		echo "$prod_name RAM Disk is active"
	fi
        if [ -z "$isrunning" ]
	then
		echo "$prod_name RAM Disk is not active"
	fi
	RETVAL=$?
	;;
  reload)
	RETVAL=$?
	;;
  restart)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  *)
        echo "Usage: ipbxrd {start|stop|restart|reload|status}"
        exit 1
esac

exit $RETVAL
