#!/bin/sh
#
# startaes - enables|disables|check AES autostart status
# /etc/init.d/startaes
#

# Source function library.
. /etc/rc.d/init.d/functions

# Just to look nicer
echo

# check for naes
if [ -f /etc/rc.d/rc.aes ]; then
 	word="NAES"
  	exists=$(grep -c $word /etc/rc.d/rc.aes)
	if [ $exists -gt 0 ]; then
    		#echo "Word found"
    		aes=$word
    		#echo $aes
    	fi
fi

# check for xaaes
if [ -f /etc/rc.d/rc.aes ]; then
  	word="XaAES"
  	exists=$(grep -c $word /etc/rc.d/rc.aes)
	if [ $exists -gt 0 ]; then
    		#echo "Word found"
    		aes=$word
    		#echo $aes
    	fi
fi
  
# check for myaes
if [ -f /etc/rc.d/rc.aes ]; then
  	word="MyAES"
  	exists=$(grep -c $word /etc/rc.d/rc.aes)
	if [ $exists -gt 0 ]; then
    		#echo "Word found"
    		aes=$word
    		#echo $aes
    	fi
fi
    	
# check for multitos
if [ -f /etc/rc.d/rc.aes ]; then
  	word="MultiTOS"
  	exists=$(grep -c $word /etc/rc.d/rc.aes)
	if [ $exists -gt 0 ]; then
    		#echo "Word found"
    		aes=$word
    		#echo $aes
    	fi
    	
fi

# check for startaes
if [ -f /etc/rc.d/rc.aes ]; then
  	word="selectaes"
  	exists=$(grep -c $word /etc/rc.d/rc.aes)
	if [ $exists -gt 0 ]; then
    		#echo "Word found"
    		aes="SelectAES"
    		#echo $aes
    	fi
fi

#echo $aes


# See how we were called.
case "$1" in
  enable)
  	if [ -f /etc/rc.d/rc.aes ]; then
  	echo " AES autostart: "$aes" enabled."
  	else
		if [ -f /etc/startaes/rc.aes ]; then
			#echo
			cp /etc/startaes/rc.aes /etc/rc.d/
			echo " AES autostart: enabled."$word
			#echo
        	fi
        fi
	;;
  disable) 	  	
	if [ -f /etc/rc.d/rc.aes ]; then
		#echo
		mv /etc/rc.d/rc.aes /etc/startaes/
		echo " AES autostart: disabled." 
		#echo		
	else
		echo " AES autostart: disabled."
	fi
	;;
  naes)
	if [ -f /etc/startaes/naes ]; then
		#echo
		cp /etc/startaes/naes /etc/rc.d/rc.aes
		echo " AES autostart: NAES enabled."
		#echo		
        fi
	;;
  xaaes)
	if [ -f /etc/startaes/xaaes ]; then
		#echo
		cp /etc/startaes/xaaes /etc/rc.d/rc.aes
		echo " AES autostart: XaAES enabled."
		#echo
        fi
	;;
  myaes)
	if [ -f /etc/startaes/myaes ]; then
		#echo
		cp /etc/startaes/myaes /etc/rc.d/rc.aes
		echo " AES autostart: MyAES enabled."
		#echo
        fi
	;;
  multitos)
	if [ -f /etc/startaes/multitos ]; then
		#echo
		cp /etc/startaes/multitos /etc/rc.d/rc.aes
		echo " AES autostart: MultiTOS enabled."
		#echo
        fi
	;;
  select)
	if [ -f /etc/startaes/selectaes ]; then
		#echo
		cp /etc/startaes/selectaes /etc/rc.d/rc.aes
		echo " AES autostart: SelectAES enabled."
		#echo
        fi
	;;
  status)
  	echo -n " AES autostart status: "
	if [ -f /etc/rc.d/rc.aes ]; then
		#echo
		echo $aes "enabled." 
		#echo
	else
		#echo
		echo "disabled."
		#echo
	fi
	;;
  *)
	echo "Usage: startaes {enable|disable|naes|xaaes|myaes|multitos|select|status}"
	echo
	exit 1
esac

# again, just to look nicer
echo

exit 0
