HEX
Server: Apache/2.2.34 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141
System: Linux Kou-Etsu-Dou 4.4.59+ #25556 SMP PREEMPT Thu Mar 4 18:03:46 CST 2021 x86_64
User: hosam (1026)
PHP: 7.2.29
Disabled: NONE
Upload Files
File: //var/packages/SynologyApplicationService/scripts/utils
#!/bin/sh

SynoSettingsGet()
{
	synogetkeyvalue /var/packages/SynologyApplicationService/etc/settings.conf $1
}

SynoSettingsSet()
{
	synosetkeyvalue /var/packages/SynologyApplicationService/etc/settings.conf $1 $2
}

SynoService()
{
	if [ -z "$SYNOPKG_DSM_VERSION_MAJOR" ]; then
		echo "Require SYNOPKG_DSM_VERSION_MAJOR";
	fi;

	if [[ "$SYNOPKG_DSM_VERSION_MAJOR" -le "6" ]]; then
		SynoUpstartService "$1" "$2"
	else
		SynoSystemdService "$1" "$2"
	fi;

	return $?;
}

SynoUpstartService()
{
	if (( $# < 2 )); then
		echo "Fatal, Wrong params." > $SYNOPKG_TEMP_LOGFILE
		return 2
	fi

	NEED_GREP=$( [ $1 == 'stop' ] && echo 'stop' || echo 'running' )
	if [ "$1" != "status" ]; then
		initctl $1 $2
		sleep 1
	fi

	if (( $# > 2 )); then
		initctl status $2 | grep $NEED_GREP;
		while (( $? == 0 )); do
			result=true
			for (( idx = 3 ; $idx <= $# ; ++idx )); do
				if ! ${!idx}; then
					result=false
					break;
				fi
			done
			if $result; then
				return 0
			fi
			sleep 0.5
			initctl status $2 | grep $NEED_GREP >/dev/null 2>&1;
		done
		echo "Service \"$2\" $1 failed." > $SYNOPKG_TEMP_LOGFILE
		return 1
	fi
	initctl status $2 | grep $NEED_GREP >/dev/null 2>&1;
	return $?
}

SynoSystemdService()
{
	if (( $# < 2 )); then
		echo "Fatal, Wrong params." > $SYNOPKG_TEMP_LOGFILE
		return 2
	fi

	NEED_GREP=$( [ $1 == 'stop' ] && echo 'inactive' || echo 'active' )
	if [ "$1" != "status" ]; then
		/usr/syno/bin/synosystemctl "$1" "$2"
		sleep 3
	fi

	if (( $# > 2 )); then
		/usr/syno/bin/synosystemctl get-active-status "$2" | grep "${NEED_GREP}"
		while (( $? == 0 )); do
			result=true
			for (( idx = 3 ; $idx <= $# ; ++idx )); do
				if ! ${!idx}; then
					result=false
					break;
				fi
			done
			if $result; then
				return 0
			fi
			sleep 0.5
			/usr/syno/bin/synosystemctl get-active-status "$2" | grep "${NEED_GREP}" >/dev/null 2>&1;
		done
		echo "Service \"$2\" $1 failed." > $SYNOPKG_TEMP_LOGFILE
		return 1
	fi
	/usr/syno/bin/synosystemctl get-active-status "$2" | grep "${NEED_GREP}" >/dev/null 2>&1;
	return $?
}

SynoSetVolume()
{
	volume="$(SynoSettingsGet volume)"
	install_volume=`realpath /var/packages/SynologyApplicationService/target | sed 's/appstore\///g'`

	if [ ! -d ${install_volume} ]; then
		/bin/mkdir -p ${install_volume}
	fi

	if [[ "" != ${volume} && -d ${volume} && ${volume} != ${install_volume} ]]; then
		/bin/rm -rf ${install_volume}
		/bin/mv -f ${volume} ${install_volume}
	fi

	if [ ! -L /usr/local/SynologyApplicationService ]; then
		/bin/rm -rf /usr/local/SynologyApplicationService
	fi

	/bin/unlink /usr/local/SynologyApplicationService
	/bin/ln -s ${install_volume} /usr/local/SynologyApplicationService

	SynoSettingsSet volume ${install_volume}
}