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/MailClient/scripts/start-stop-status
#!/bin/sh

. "$(dirname $0)"/common
. "$(dirname $0)"/mailserver-plugin

WEB_DIR="/usr/syno/synoman/webman/3rdparty/${SYNOPKG_PKGNAME}"

PACKAGE_TINYMCE_PATH="${PACKAGE_DIR}/target/ui/js/tinymce_plugins"
DSM_TINYMCE_PATH="/usr/syno/synoman/webman/modules/TinyMCE/js/tinymce/plugins"

DAEMON_UPSTART_NAME="pkg-MailPlus-synomailclientd"
APID_UPSTART_NAME="pkg-MailPlus-apid"
PUSH_SERVER_UPSTART_NAME="pkg-MailPlus-pushserver"

MANAGE_POP3_DAEMON="${PACKAGE_DIR}/target/tool/manage_pop3_daemon"
MIGRATE_CONTACTS_PKG_TOOL="${PACKAGE_DIR}/target/tool/migrate_contacts_pkg"
NOTIFY_SYNO_SCHEDULER="${PACKAGE_DIR}/target/tool/scheduler"
NOTIFY_DAEMON="${PACKAGE_DIR}/target/tool/notify_daemon"
REGULAR_OP_TOOL="${PACKAGE_DIR}/target/tool/regular_op"

SYSLOG_NOT_2_MSG_CONF="${PACKAGE_DIR}/target/etc/mail_client_not_2_msg.conf"
SYSLOG_NOT_2_MSG_PATH="/usr/local/etc/syslog-ng/patterndb.d/include/not2msg"
PID_FILE_DIR_PATH="/run/MailClient"
LOCK_FILE_DIR_PATH="/var/lock/MailClient"

Control()
{
	if [ -f "/usr/syno/bin/synosystemctl" ]; then
		synosystemctl $*
	else
		initctl $*
	fi
}

Status()
{
	if [ -f "/bin/systemctl" ]; then
		systemctl status $1 | grep running > /dev/null
	else
		initctl status $1 | grep running > /dev/null
	fi
	return $?
}

LinkTinyMCEPlugins()
{
	for entry in "${PACKAGE_TINYMCE_PATH}"/syno_mc_*
	do
		if [ -d $entry ]; then
			ln -sf ${entry} ${DSM_TINYMCE_PATH}
		fi
	done
}

UnlinkTinyMCEPlugins()
{
	rm -rf ${DSM_TINYMCE_PATH}/syno_mc_*
}

CopyNot2MsgConfig()
{
	# TODO: use resource worker when we can depend on DSM 6.1
	if [ -d ${SYSLOG_NOT_2_MSG_PATH} ]; then
		cp -f ${SYSLOG_NOT_2_MSG_CONF} ${SYSLOG_NOT_2_MSG_PATH}
		/var/packages/MailClient/scripts/reload-syslog-ng
	fi
}

RemoveNot2MsgConfig()
{
	# TODO: use resource worker when we can depend on DSM 6.1
	if [ -d ${SYSLOG_NOT_2_MSG_PATH} ]; then
		rm -f ${SYSLOG_NOT_2_MSG_PATH}/mail_client_not_2_msg.conf
		/var/packages/MailClient/scripts/reload-syslog-ng
	fi
}

RegisterSynoScheduler()
{
	${NOTIFY_SYNO_SCHEDULER} --add -n do_regular_op -c "${REGULAR_OP_TOOL} --all" -h 02 -m 00
	${NOTIFY_SYNO_SCHEDULER} --add -n addressbook_externalsource_sync -c "${NOTIFY_DAEMON} --sync_externalsource" -r
}

DeregisterSynoScheduler()
{
	${NOTIFY_SYNO_SCHEDULER} --remove -n do_regular_op
	${NOTIFY_SYNO_SCHEDULER} --remove -n addressbook_externalsource_sync
}

EnsurePIDFileDirExist()
{
	# just ensure this path exist, no need to remove it when pkg stop (for now)
	# TODO: consider creating pid_file in cpp code
	mkdir -p ${PID_FILE_DIR_PATH}
}

EnsureLockFileDirExist()
{
	mkdir -p ${LOCK_FILE_DIR_PATH}
}

MigrateContactsPkg()
{
	# Addressbooks are removed from version 2.2.0-1217,
	# migrate them to Contacts package when upgrading from version older then 2.2.0-1217.
	if [ -e ${OLD_PKGVER_FILE} ] ; then
		OLD_PACKAGE_VER=`get_key_value "${OLD_PKGVER_FILE}" "old_version"`
		OLD_BUILD_NUMBER=`echo ${OLD_PACKAGE_VER} | awk -F'[.-]' '{print $NF}'`

		if [ -z ${OLD_BUILD_NUMBER} ] || [ $((10#${OLD_BUILD_NUMBER} % 10000)) -lt "1217" ]; then
			${MIGRATE_CONTACTS_PKG_TOOL}
		fi
	fi
}

pkg_start()
{
	EnsurePIDFileDirExist
	EnsureLockFileDirExist

	# InstallServerHooks also refreshes cached info. so it should be called before daemon start.
	InstallServerHooks

	Control start $DAEMON_UPSTART_NAME
	Control start $PUSH_SERVER_UPSTART_NAME
	Control start $APID_UPSTART_NAME

	InstallDovecotPlugin
	LinkTinyMCEPlugins
	CopyNot2MsgConfig
	${MANAGE_POP3_DAEMON} -a start

	RegisterSynoScheduler
	${NOTIFY_DAEMON} --migrate_contact_id
	MigrateContactsPkg

	# remove upgrade temp file
	rm -f ${OLD_PKGVER_FILE}
}

pkg_stop()
{
	Control stop $APID_UPSTART_NAME
	Control stop $DAEMON_UPSTART_NAME
	Control stop $PUSH_SERVER_UPSTART_NAME

	${MANAGE_POP3_DAEMON} -a stop

	UninstallDovecotPlugin
	UninstallServerHooks
	UnlinkTinyMCEPlugins
	RemoveNot2MsgConfig

	DeregisterSynoScheduler
}

pkg_status()
{
	Status pkg-MailPlus-synomailclientd
	local synomailclientd=$?
	Status pkg-MailPlus-pushserver
	local pushserver=$?

	if [ -e "${WEB_DIR}" -a "${synomailclientd}" == 0 -a "${pushserver}" == 0 ]; then
		exit 0
	else
		exit 1
	fi
}

case $1 in
	start)
		pkg_start
		exit 0
	;;
	stop)
		pkg_stop
		exit 0
	;;
	# Do not use enabled to determate status here
	# Otherwize, the package status would be "stopped" after package upgraded.
	status)
		pkg_status
	;;
	killall)
	;;
	log)
		exit 0
	;;
esac