#!/bin/sh
SYNOPKG_PKGNAME="Calendar"
PACKAGE_DIR="/var/packages/${SYNOPKG_PKGNAME}"
PACKAGE_TARGET="${PACKAGE_DIR}/target"
PKG_APP_PATH="${PACKAGE_DIR}/target/ui"
PKG_TOOL_DIR="${PACKAGE_DIR}/target/tool"
PKG_CONF_DIR="${PACKAGE_DIR}/target/conf"
SYSTEM_WEBAPI_DIR="/usr/syno/synoman/webapi"
PACKAGE_WEBAPI_DIR="${PACKAGE_DIR}/target"
VOLUME="$(/usr/bin/readlink "${PACKAGE_TARGET}" | /usr/bin/cut -d '/' -f2)"
SERVICETOOL="/usr/syno/bin/servicetool"
PKG_TOOL_BIN="${PKG_TOOL_DIR}/synocalendar"

DSM_INDEX_ADD="/usr/syno/bin/pkgindexer_add"
DSM_INDEX_DEL="/usr/syno/bin/pkgindexer_del"
DSM_VERSION=${SYNOPKG_DSM_VERSION_MAJOR}${SYNOPKG_DSM_VERSION_MINOR}
APP_PATH="/usr/syno/synoman/webman/3rdparty/$SYNOPKG_PKGNAME"
DB_VERSION_PATH="${PKG_CONF_DIR}/db_version.json"

PUSHSERVER_UPSTART_NAME="pkg-synocalendar-pushserver"
NOTIFYDAEMON_UPSTART_NAME="pkg-synocalendar-notify-daemon"
PHPFPM_UPSTART_NAME="pkg-synocalendar-phpfpm"

PSQL=/bin/psql
DBNAME=calendar
DBA="-U postgres"

synocal_tmp_path="/tmp/synocalendar/"
is_new_beginning_file="${synocal_tmp_path}/is_new_beginning"
synocal_db_dump_path="/${VOLUME}/@synocalendar/"
synocal_db_dump="${synocal_db_dump_path}calendar_db_dump"

CAL_PKG_INFO=/var/packages/Calendar/INFO
CAL_PKG_ENABLE=/var/packages/Calendar/enabled

synocal_log()
{
	/bin/logger -s -p err -t calendar $1
}

# synocal.log
synocal_info()
{
	/bin/logger -s -p local3.info -t calendar $1
}

PackageInfoGet(){
	if [ -f "${CAL_PKG_INFO}" ]; then
		CAL_PKG_NAME=`/bin/get_key_value ${CAL_PKG_INFO} package`
		CAL_PKG_VERSION=`/bin/get_key_value ${CAL_PKG_INFO} version`
		CAL_PKG_VENDOR="Synology"
		CAL_PKG_MODVER="1.0"
	else
		echo "Calendar package info file does not exist!";
		exit 1;
	fi
}

PackageEnableGet(){
	if [ -f "${CAL_PKG_ENABLE}" ]; then
		CAL_PKG_ENABLE=1;
	else
		CAL_PKG_ENABLE=0;
	fi
}

sdk_hook_response()
{
	case $1 in
		--sdk-mod-ver)
			#Print SDK support version
			echo "1.0"
			;;
		--name)
			#Print package name
			echo "Calendar"
			;;
		--pkg-ver)
			#Print package version
			echo "1.0"
			#echo ${CAL_PKG_VERSION};
			;;
		--vendor)
			#Print package vendor
			echo "Synology"
			;;
		--pre)
			;;
		--post)
			;;
		*)
			echo "Usage: $0 --sdk-mod-ver|--name|--pkg-ver|--vendor|--pre|--post"
			;;
	esac
}

db_migration()
{
	RESET_DB_VERSION=1
	${PKG_TOOL_DIR}/synocalendar --db-migrate  >> /var/log/synocal.log 2>&1
	if [ $? -ne 0 ];then
		echo "run migrate DB failed" >> /var/log/synocal.log 2>&1
		RESET_DB_VERSION=0
	else
		echo "run migrate DB success" >> /var/log/synocal.log 2>&1
	fi

	# reset db version
	if [ ${RESET_DB_VERSION} -eq 1 ]; then
		echo "reset db version file to new system" >> /var/log/synocal.log 2>&1
		${PKG_TOOL_DIR}/synocalendar --reset-db-version type=new >> /var/log/synocal.log 2>&1
	else
		echo "reset db version file to old db" >> /var/log/synocal.log 2>&1
		${PKG_TOOL_DIR}/synocalendar --reset-db-version type=db >> /var/log/synocal.log 2>&1
	fi
}

IsDSM7OrAbove()
{
	local dsm_major_version="$(get_key_value /etc.defaults/VERSION majorversion)"
	local YES=0
	local NO=1

	if [[ "${dsm_major_version}" -gt 6 ]]; then
		return ${YES}
	else
		return ${NO}
	fi
}

ControlService()
{
        if IsDSM7OrAbove; then
                synosystemctl $*
        else
                initctl $*
        fi
}
