#!/bin/sh
. "$(dirname "$0")/common"

case "$1" in
	start)
		if [ ! -L /usr/syno/etc/www/WebStation.json ]; then
			rm -f /usr/syno/etc/www/WebStation.json
			ln -sf /usr/syno/etc/packages/WebStation/WebStation.json /usr/syno/etc/www/WebStation.json
		fi
		if [ ! -d /usr/local/etc/httpd/sites-enabled/ ]; then
			/usr/bin/mkdir -p /usr/local/etc/httpd/sites-enabled/
		fi
		"${TARGET}/tools/synowebstation" --update web
		"${TARGET}/tools/synowebstation" --update vhost

		if /usr/syno/sbin/synoservicectl --status pkg-php56-fpm; then
			/usr/sbin/reload pkg-php56-fpm
		fi

		/usr/sbin/initctl start pkg-apache22
		/usr/sbin/initctl start pkg-WebStation-fcgiwrap
		;;
	stop)
		/usr/bin/rm -f /etc/nginx/app.d/server.webstation-vhost.conf
		/usr/sbin/initctl stop pkg-WebStation-fcgiwrap
		/usr/sbin/initctl stop pkg-apache22
		rm -f /usr/syno/etc/www/WebStation.json
		rm -f /usr/local/etc/php56/conf.d/webstation-vhost.ini

		if /usr/syno/sbin/synoservicectl --status pkg-php56-fpm; then
			/usr/sbin/reload pkg-php56-fpm
		fi
		;;
	status)
		/usr/syno/sbin/synoservicectl --status pkg-apache22
		ret=$?
		if [ 0 -eq $ret ] && [ -L /usr/syno/etc/www/WebStation.json ]; then
			exit 0
		elif [ 1 -eq $ret ] && [ -e /usr/syno/etc/packages/WebStation/WebStation.json ]; then
			exit 1
		else
			exit 150
		fi
		;;
esac
