#!/bin/sh
#
# description: Starts and stops the vsftpd daemon
#
export mos=/usr/local/etc/mos
start() {
	echo "Starting remote control..."
	if [ ! -d /tmp/www/rc ] ; then
	  [ ! -d /tmp/www ] && mkdir /tmp/www
	  echo "  Copying files..."
	  cp -Rfp $mos/rc/web/* /tmp/www/
	fi
	echo "Done"
	# antisleep
	echo "{" >> /tmp/ir
}	
stop() {
	echo "Stopping remote control..."
	if [ -d /tmp/www/rc ] ; then
	  echo "  Remove files..."
	  rm -Rf /tmp/www/rc
	  rm -f /tmp/www/cgi-bin/cmd.cgi
	fi
	echo "Done"
}	

rcS="/usr/local/etc/rcS"

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart | status)
	;;
  enable)
	echo "Enabling remote control..."
	#check for already enabled
	stat="`cat /usr/local/etc/rcS | grep 'touch /tmp/ir'`"
	if [ -z "$stat" ] ; then
	  echo "Patching rcS..."
	  cat $rcS | sed '
/cd \$DEFAULT_AP_DIR/a\
	touch /tmp/ir
s/^	*\(.*RootApp DvdPlayer\&.*\)$/		tail -f \/tmp\/ir | \1/
s/^	*\(.*\.\/DvdPlayer\&.*\)$/		tail -f \/tmp\/ir | \1/
' > $rcS.new
	  mv $rcS.new $rcS
	  chmod +x $rcS
	  start
	fi
	echo "Remote control enabled"
	echo "Please, reboot device!"
	;;
  disable)
	echo "Disabling remote control..."
	#check for already disabled
	stat="`cat /usr/local/etc/rcS | grep 'touch /tmp/ir'`"
	if [ ! -z "$stat" ] ; then
	  echo "Patching rcS..."
	  cat $rcS | sed '
/touch \/tmp\/ir/d
s/^	*tail -f \/tmp\/ir | \(.*\)$/		\1/
' > $rcS.new
	  mv $rcS.new $rcS
	  chmod +x $rcS
	  stop
	fi
	echo "Remote control disabled"
	echo "Please, reboot device!"
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status|enable|disable}"
esac

exit $?
