#!/bin/sh
#
# description: Starts and stops the moServices core 
#
mos=/usr/local/etc/mos/core
spa=/usr/local/etc/dvdplayer/savedrss/scripts

case "$1" in
  start)
	echo "Starting core..."
	if [ ! -d /tmp/www/mos ] ; then
	  [ ! -d /tmp/www ] && mkdir /tmp/www
	  echo "  Copying files..."
	  cp -Rfp $mos/web/* /tmp/www/
	fi
	echo "Done"
	;;
  stop)
	echo "Stopping core..."
	if [ -d /tmp/www/mos ] ; then
		echo "  Remove files..."
		rm -Rf /tmp/www/mos
		rm -f /tmp/www/cgi-bin/services.cgi
	fi
	echo "Done"
	;;
  enable)
	echo "Enabling core..."
	# patching anything
	if [ -f $mos/patch ] ; then
	  $mos/patch
	  rm $mos/patch
	fi

	echo "patching rcS..."
	if [ -z "$( cat /usr/local/etc/rcS | grep '/usr/local/etc/mos/services start' )" ] ; then
	  echo "Adding start services to rcS..."
	  echo "/usr/local/etc/mos/services start" >> /usr/local/etc/rcS
	fi
	echo "patching passwd..."
	if [ ! -z "$( cat /usr/local/etc/passwd | grep 'root::/bin/sh' )" ] ; then
	  echo "Patching home directory for root..."
	  sed -i 's/root::\/bin/root:\/:\/bin/' /usr/local/etc/passwd
	fi
	echo "adding RSS..."
	if [ ! -f $spa/news.rss ] ; then
	  mkdir -p $spa
	  cp /usr/local/bin/scripts/news.rss $spa/
	fi

	if [ ! -f /usr/local/etc/moMenu.rss ] ; then
	  cp $mos/rss/moMenu.rss /usr/local/etc/
	fi

	if [ -z "$( cat $spa/news.rss | grep moMenu.rss )" ] ; then
	  sed -i '
/<title>Add URL - Click to add RSS URL<\/title>/i\
		<title>Additional services</title>\
		<link>rss_file://../etc/moMenu.rss</link>\
		<media:thumbnail url="/usr/local/etc/mos/core/rss/moMenu.png" width="120" height="90" />\
	</item>\
	<item>
' $spa/news.rss
	fi
	echo "Done"
	;;
  disable)
	echo "Disabling core..."
	echo "unpatching rcS..."
	if [ ! -z "$( cat /usr/local/etc/rcS | grep '/usr/local/etc/mos/services start' )" ] ; then
	  echo "Remove start services to rcS..."
	  sed -i '/\/usr\/local\/etc\/mos\/services start/d' /usr/local/etc/rcS
	fi
	echo "unpatching passwd..."
	if [ -z "$( cat /usr/local/etc/passwd | grep 'root::/bin/sh' )" ] ; then
	  echo "Unpatching home directory for root..."
	  sed -i 's/root:\/:\/bin/root::\/bin/' /usr/local/etc/passwd
	fi
	echo "Done"
	;;
  restart | status)
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status|enable|disable}"
esac

exit $?
