#!/bin/sh
VOLUME=`/usr/bin/readlink /var/services/homes | /usr/bin/cut -d'/' -f2`

PACKAGE_DIR="/var/packages/MailStation"
WebMailEnabled="$PACKAGE_DIR/enabled"
WebMailDir="$PACKAGE_DIR/target"
WebmailDesktop="/usr/syno/synoman/webman/3rdparty/MailStation"
WebMailExt=${WebMailDir}/roundcubemail/ext
WebMailConf=${WebMailDir}/roundcubemail/config/main.inc.php
UIStringDir="$PACKAGE_DIR/target/ui/texts"
DSM_INDEX_ADD="/usr/syno/bin/pkgindexer_add"
DSM_INDEX_DEL="/usr/syno/bin/pkgindexer_del"
PHP_CONF_DIR="/etc/php/conf.d"
HTTPD_CONF_DIR="/etc/httpd/sites-enabled-user"

FetchBin="/usr/syno/bin/synofetch"

popusers=`/usr/bin/find ${WebMailExt} -name "*_fetch" | /usr/bin/cut -d'/' -f8 |/usr/bin/cut -d'_' -f1`

GenerateMessage() {
	local key=$1
	case $SYNOPKG_DSM_LANGUAGE in
		chs | cht |csy | dan | enu | fre | ger | hun | ita | jpn | krn | nld | nor | plk| ptb | ptg | rus | spn | sve | trk)
			echo $(sed -n '/^\[app\]/,/^'$key'/s/'$key'.*=.*"\(.*\)"/\1/p' $UIStringDir/$SYNOPKG_DSM_LANGUAGE/strings) > $SYNOPKG_TEMP_LOGFILE
			;;
		* )
			echo "This package requires you to enable Mail Server." > $SYNOPKG_TEMP_LOGFILE
			;;
	esac
}

RegenConf() {
	
	WebmailConf="${WebMailDir}/roundcubemail/config/main.inc.php"
	if [ $SYNOPKG_DSM_VERSION_BUILD -gt 2500 ]; then
		MailStationHostname=`/bin/get_key_value "$PACKAGE_DIR/etc/mailserver.conf" smtp_hostname`
		WebmailSMTP=`/bin/get_key_value "$PACKAGE_DIR/etc/mailserver.conf" smtp_hostname`
	else
		MailStationHostname=`/bin/get_key_value /etc/synoinfo.conf mailstation_hostname`
		WebmailSMTP=`/bin/get_key_value /etc/synoinfo.conf webmail_smtp_server`
	fi

    if [ -z "$WebmailSMTP" ]; then
        SMTP_SERVER="localhost"
        SMTP_PORT="25"
    else
        SMTP_SERVER=`echo ${WebmailSMTP} | cut -d':' -f1`
        SMTP_PORT=`echo ${WebmailSMTP} | cut -d':' -f2`
        if [ "${SMTP_PORT}" = "${WebmailSMTP}" ]; then
            SMTP_PORT="25"
        fi
    fi
	if [ -z "$MailStationHostname" ]; then
		MailStationHostname="localhost"
	fi

	sed "/^\$rcmail_config\['mail_domain'\]/c\\\$rcmail_config['mail_domain'] = '${MailStationHostname}';" ${WebmailConf} > /tmp/tempinfo
	sed "/^\$rcmail_config\['smtp_server'\]/c\\\$rcmail_config['smtp_server'] = '${SMTP_SERVER}';" /tmp/tempinfo > /tmp/tempinfo.$$
	sed "/^\$rcmail_config\['smtp_port'\]/c\\\$rcmail_config['smtp_port'] = ${SMTP_PORT};" /tmp/tempinfo.$$ > /tmp/tempinfo
	/bin/mv /tmp/tempinfo ${WebmailConf}
	/bin/chown -R http:http ${WebmailConf}
}

CheckEnv() {
	if [ $SYNOPKG_DSM_VERSION_BUILD -gt 2500 ]; then
		if [ ! -f "$PACKAGE_DIR/enabled" ]; then
			GenerateMessage "require_service"
			exit 1;
		fi
	fi
}

POP3FetchControl(){
	POP3Fetch_enabled=`grep -E "rcmail_config.*extmailallow" ${WebMailConf} | cut -d ' ' -f3`
	if [ "$POP3Fetch_enabled" != "true;" ]; then
		return
	fi
	for popusr in $popusers
	do
		if [ -e ${WebMailExt}/${popusr}_fetch ]; then
			${FetchBin} ${popusr} $1
		fi
	done

}

StartDaemons() {

	CheckEnv
	RegenConf

	cp -a ${WebMailDir}/bin/synofetch ${FetchBin}
}

case "$1" in
	start)
		if [ ! -f "${WebMailEnabled}" ]; then
			exit 1
		fi
		StartDaemons
		POP3FetchControl -1
		${DSM_INDEX_ADD} ${WebMailDir}/ui/index.conf
		cp -f "$PACKAGE_DIR/target/etc/SYNO.SDS.MailStation.ini" "$PHP_CONF_DIR"
		cp -f "$PACKAGE_DIR/target/etc/SYNO.SDS.MailStation.conf" "$HTTPD_CONF_DIR"
		;;
	stop)
		killall fetchmail
		if [ -f "${WebMailEnabled}" ]; then
			exit 0
		fi
		${DSM_INDEX_DEL} ${WebMailDir}/ui/index.conf
		rm -f "$PHP_CONF_DIR/SYNO.SDS.MailStation.ini"
		rm -f "$HTTPD_CONF_DIR/SYNO.SDS.MailStation.conf"
		;;
	restart)
		sleep 1
		StartDaemons
		;;
	status)
		if [ -f "${WebMailEnabled}" ]; then
			exit 0
		fi
		exit 1
		;;
	log) 
		echo ""
		;;  
	*)
		echo "Usage: $0 {start|stop|restart|status}" >&2
		exit 1
		;;
esac

exit 0

