#!/bin/sh

dupEnabled="/var/packages/dup/enabled"
dupBin="/var/packages/dup/target/bin/dup"
dupConf="/var/packages/dup/target/dup.conf"
dupUIDir="/usr/syno/synoman/webman/3rdparty/dup"
dupLog="/var/packages/dup/target/debug.log"
RotateLogBin="/var/packages/dup/target/bin/rotatelogs"
ApacheConf="/usr/syno/apache/conf/httpd.conf-user"
PkgName="dup"
PkgTopDir="/var/packages/${PkgName}"
dupTarget="${PkgTopDir}/target"
dupUI="${dupTarget}/ui"
DSM_INDEX_ADD="/usr/syno/bin/pkgindexer_add"
DSM_INDEX_DEL="/usr/syno/bin/pkgindexer_del"


if [ ! -z "$SYNOPKG_PKGDEST" -a -z "$SYNOPKG_DSM_VERSION_MAJOR" ];then
	# From UI. We need to do this. So we will not run the start script
	# at boot time when the package is disabled.
	# When the package is enabled, we would like to run the start script.
	# So we can correct the httpd.conf after DS firmware upgrade
	# The version 731 does not touch the file enabled for us. So we have to
	# do it our self.
	case "$1" in
		start)
			touch $dupEnabled;
			;;
		stop)
			rm -f $dupEnabled
			;;
	esac
fi

case "$1" in
	start)
		if [ ! -f "$dupEnabled" -a -z "$SYNOPKG_DSM_VERSION_MAJOR" ]; then
			exit;
		fi
		if [ ! -L "/var/services/web" ]; then
			date >> $dupLog
			echo "web shared folder does not exist." >> $dupLog
			echo "Please enable Web Station before enable the dup." >> $dupLog
			exit 1
		fi

		if [ ! -d "/var/services/web/dup" ]; then
			mkdir /var/services/web/dup
			mkdir /var/services/web/dup/images
			mkdir /var/services/web/dup/files
			cp -rf /var/packages/dup/target/bin/* /var/services/web/dup
			

			if [ $? != 0 ]; then
				date >> $dupLog
				echo "Failed to create dup directory in web shared folder." >> $dupLog
				exit 1
			else
				date >> $dupLog
				echo "Create the web/dup to store web statistic." >> $dupLog
			fi
		fi

		# Execute the dup once. So we will have data before crontab run it.
		$dupBin -c $dupConf

		# Put the dup to crontab
		/bin/grep $dupBin /etc/crontab > /dev/null 2>&1
		if [ $? != 0 ]; then
			echo "13	*	*	*	*	root	$dupBin -c $dupConf" >> /etc/crontab
			kill -1 `cat /var/run/crond.pid`
		fi

		# Enable desktop icon
		rm -f $dupUIDir
		if [ -n "$SYNOPKG_DSM_VERSION_MAJOR" -a $SYNOPKG_DSM_VERSION_MAJOR -ge 4 ]; then
			ln -sf ${dupUI} $dupUIDir
			${DSM_INDEX_ADD} ${dupUI}/index.conf
		else
			ln -sf ${dupTarget}/desktop $dupUIDir
		fi

		;;
	stop)
		if [ -f "$dupEnabled" -a -z "$SYNOPKG_DSM_VERSION_MAJOR" ]; then
			exit;
		fi
		# Remove dup from crontab
		/bin/grep $dupBin /etc/crontab > /dev/null 2>&1
		if [ $? = 0 ]; then
			/bin/grep -v $dupBin /etc/crontab > /etc/crontab.tmp
			/bin/mv /etc/crontab.tmp /etc/crontab
			kill -1 `cat /var/run/crond.pid`
		fi

		# Disable desktop icon
		if [ -n "$SYNOPKG_DSM_VERSION_MAJOR" -a $SYNOPKG_DSM_VERSION_MAJOR -ge 4 ]; then
			${DSM_INDEX_DEL} ${dupUI}/index.conf
		fi
		if [ -d "$dupUIDir" ]; then
			rm -rf $dupUIDir
		fi

		;;
	status)
		/bin/grep $dupBin /etc/crontab > /dev/null 2>&1
		if [ $? = 0 ]; then
			exit 0;
		fi
		exit 1
		;;
	log)
		echo $dupLog
		;;
	*)
		echo "Usage: $0 {start|stop|status|log}" >&2
		exit 1
		;;
esac

exit 0

