#!/bin/sh
. /usr/syno/bin/jsoncmd

readonly BACKUP_LIST="${SYNOPKG_PKGPATH}/etc/mailserver.conf \
${SYNOPKG_PKGPATH}/etc/mailserver.db \
${SYNOPKG_PKGPATH}/etc/spamlist.conf \
${SYNOPKG_PKGPATH}/etc/alias.db \
${SYNOPKG_PKGPATH}/etc/rules"

backup_enabled_rule_list()
{
	local export_path=${1}
	local get_enable_rule_list=`ls "${SYNOPKG_PKGPATH}/target/etc/spamassassin/" | grep -v '.pre$' | grep -v '^local.cf$'`
	echo "${get_enable_rule_list}" > "${export_path}/enable_rule_list"
}

backup_report_time()
{
	local export_path=${1}
	local enable_report=`cat /etc/crontab | grep /var/packages/MailServer/target/bin/syno_send_report | wc -l`
	if [ x"0" != x"${enable_report}" ]; then
		cat "/etc/crontab" | grep "/var/packages/MailServer/target/bin/syno_send_report" > "${export_path}/report_time"
	fi
}

backup_dkim_key()
{
	local export_path=${1}
	if [ -e "${SYNOPKG_PKGPATH}/etc/dkim.key" ]; then
		cp -rf "${SYNOPKG_PKGPATH}/etc/dkim.key" "${export_path}/dkim.key"
	fi
}

backup()
{
	local export_path=${1}

	for file in ${BACKUP_LIST}
	do
		if [ -e "${file}" ]; then
			/bin/cp -rf "${file}" "${export_path}"
		fi
	done

	backup_enabled_rule_list "${export_path}"
	backup_report_time "${export_path}"
	backup_dkim_key "${export_path}"
}

main()
{
	local export_path=$(jget "${SYNOPKG_BKP_INPUT}" ".temp_path")
	if [ $? -ne 0 ];then
		jerr "bad parameters"
		exit 1;
	fi

	backup "${export_path}"

	jout_begin
	joutstr "app_data_version" "1.0"
	jout_end

	exit 0
}

main
