#!/bin/sh

WebalizerEnabled="/var/packages/Webalizer/enabled"
WebalizerBin="/var/packages/Webalizer/target/bin/webalizer"
WebalizerConf="/var/packages/Webalizer/target/webalizer.conf"
WebalizerUIDir="/usr/syno/synoman/webman/3rdparty/Webalizer"
WebalizerLog="/var/packages/Webalizer/target/debug.log"
ApacheConf="/usr/local/etc/httpd/conf/httpd.conf-user"
PkgName="Webalizer"
PkgTopDir="/var/packages/${PkgName}"
WebalizerTarget="${PkgTopDir}/target"
WebalizerUI="${WebalizerTarget}/ui"
AccessLogDir="${WebalizerTarget}/log/httpd"
CronJob="${PkgTopDir}/scripts/cronjob"
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 $WebalizerEnabled;
			;;
		stop)
			rm -f $WebalizerEnabled
			;;
	esac
fi

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

		if [ ! -d "/var/services/web/webalizer" ]; then
			mkdir /var/services/web/webalizer

			if [ $? != 0 ]; then
				date >> $WebalizerLog
				echo "Failed to create webalizer directory in web shared folder." >> $WebalizerLog
				exit 1
			else
				date >> $WebalizerLog
				echo "Create the web/webalizer to store web statistic." >> $WebalizerLog
				chown -R http:http /var/services/web/webalizer 
			fi
		fi

		if [ ! -d "$AccessLogDir" ]; then
			mkdir -p "$AccessLogDir" 
		fi 
		# Enable Apache access log
		/bin/grep "^CustomLog $AccessLogDir/user-access_log combined" $ApacheConf > /dev/null 2>&1
		if [ $? != 0 ]; then
			echo "CustomLog $AccessLogDir/user-access_log combined" >> $ApacheConf
		fi

		if [ ! -f "$AccessLogDir/user-access_log" ]; then
			# To generate the first record. So the webalizer can create the first data
			synoservicecfg --restart webstation
			/bin/wget --spider http://127.0.0.1/webalizer > /dev/null 2>&1
		fi
		
		# Execute the webalizer once. So we will have data before crontab run it.
		$WebalizerBin -c $WebalizerConf

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

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

		;;
	stop)
		if [ -f "$WebalizerEnabled" -a -z "$SYNOPKG_DSM_VERSION_MAJOR" ]; then
			exit;
		fi
		# Remove webalizer from crontab
		/bin/grep $CronJob /etc/crontab > /dev/null 2>&1
		if [ $? = 0 ]; then
			/bin/grep -v $CronJob /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} ${WebalizerUI}/index.conf
		fi
		if [ -d "$WebalizerUIDir" ]; then
			rm -rf $WebalizerUIDir
		fi

		# Disable Apache access log
		/bin/grep "^CustomLog $AccessLogDir/user-access_log combined" $ApacheConf > /dev/null 2>&1
		if [ $? = 0 ]; then
			grep -v "^CustomLog $AccessLogDir/user-access_log combined" $ApacheConf> ${ApacheConf}.tmp
			mv ${ApacheConf}.tmp $ApacheConf
		fi
		;;
	status)
		/bin/grep $CronJob /etc/crontab > /dev/null 2>&1
		if [ $? = 0 ]; then
			exit 0;
		fi
		exit 1
		;;
	log)
		echo $WebalizerLog
		;;
	*)
		echo "Usage: $0 {start|stop|status|log}" >&2
		exit 1
		;;
esac

exit 0

