#!/bin/bash
# Copyright (c) 2000-2017 Synology Inc. All rights reserved.

CustomPreupgrade()
{
	local old_pkg_ver=$(echo "$SYNO_PKG_OLD_PKGVER" | cut -d- -f1)
	local new_pkg_ver=$(echo "$SYNO_PKG_PKGVER" | cut -d- -f1)
	local old_build_num=$(echo "$SYNOPKG_OLD_PKGVER" | cut -d- -f2)
	local file_list=""

	if [ "$old_pkg_ver" != "$new_pkg_ver" ] && [ -s "$WEBSITE_ROOT/$CONF_FILE" ]; then
		logger -p 0 "Orangehrm schema changed (Enforce to run updater)"
		touch /tmp/.OrangeHRM_Schema_Change
	fi

	if [ "$old_build_num" -le 0109 ]; then
		file_list="$WEBSITE_ROOT/etc/${PKG_DIR}_file.all"
		CustomBackupData "$file_list"
	fi

	if [ "$old_build_num" -le 0119 ]; then
		local pkg_store="/usr/syno/etc/packages/$PKG_NAME"
		cp -a "$WEBSITE_ROOT/.htaccess.syno" "$pkg_store/"
	fi
}

CustomPostupgrade()
{
	old_build_number=$(echo "$SYNOPKG_OLD_PKGVER" | cut -d- -f2)
	local pkg_store="/usr/syno/etc/packages/$PKG_NAME"
	if [ "$old_build_number" -le 0119 ]; then
		[ -s "$WEBSITE_ROOT/.htaccess" ] && mv "$WEBSITE_ROOT/.htaccess" "$pkg_store/.htaccess.bak"
		mv "$pkg_store/.htaccess.syno" "$WEBSITE_ROOT/"
	fi
}

CustomBackupData()
{
	local file_list="$SYNOLOGY_ADDED_PATH/orangehrm_file.all"
	local backup_prefix=$(CustomGetBackupPrefix)
	local backup_pkg_path=$(CustomGetBackupPkgPath)
	local symfony_cache="symfony/cache"
	rm -rf "$backup_prefix"
	mkdir -p "$backup_prefix"

	cp -a "$WEBSITE_ROOT" "$backup_prefix/"
	while read file; do
		[ "$backup_pkg_path/$file" = "$backup_pkg_path/./.htaccess.syno" ] && continue
		[ -f "$backup_pkg_path/$file" ] && rm -f "$backup_pkg_path/$file"
	done < "$file_list"
	rm -rf "$backup_pkg_path/$symfony_cache/"*
}

CustomSetWebsiteRoot()
{
	mv "$SYNOPKG_PKGDEST/$PKG_DIR" "$WEBSITE_ROOT"
}

GetOrangeVer()
{
	CustomParseDBConf "$1" "orange_ver" "version"
}

CustomRestoreUpgrade()
{
	#83 remove old version files
	local old_build_num=$(get_key_value "$INFO_FILE" "version" | cut -d- -f2)
	local new_build_num=$(echo $(get_key_value "/var/packages/$PKG_NAME/INFO" "version" | cut -d- -f2))
	if [ "$old_build_num" -eq "0119" ]; then
		pushd "$BACKUP_PATH"
		while read file; do
			[ -f "$file" ] && rm -rf "$file"
		done < "$SYNOLOGY_ADDED_PATH/orangehrm_file.0119"
		popd
		sed -i "s/hits->dbpass/this->dbpass/g" "$BACKUP_PATH/$CONF_FILE"
	fi

	# remove .htaccess containing order statement that not compatible with apache 2.4
	find "$BACKUP_PATH" | grep "/.htaccess$\|/.htaccess.syno$" | xargs grep -l order | xargs rm -f

	cp -a "$BACKUP_PATH" "$(dirname "$WEBSITE_ROOT")"

	# remove htaccess for not depending on php56 when 4.3.2
	[ "$new_build_num" -ge "0122" ] && RemoveHandler
	rm -rf "$WEBSITE_ROOT/symfony/cache/"*

	# for pkg's version change, need to do db schema upgrade
	local orange_ver="$(GetOrangeVer "$WEBSITE_ROOT")"
    local cur_pkg_ver=$(echo "$SYNOPKG_PKGVER" | cut -d- -f1)
	if [ "$orange_ver" != "$cur_pkg_ver" ]; then
		touch "$WEBSITE_ROOT/.OrangeHRM_Schema_Change"
		local upgrader_db_file="upgrader/apps/upgrader/modules/upgrade/templates/getDatabaseInfoSuccess.php"
		sed -i "s/@SYNO_DB_NAME@/$DB_NAME/g" "$WEBSITE_ROOT/$upgrader_db_file"
		sed -i "s/@SYNO_DB_USER@/$DB_USER/g" "$WEBSITE_ROOT/$upgrader_db_file"

		# add variable declared
		if ! grep -q "var \$dbpass;" "$WEBSITE_ROOT/$CONF_FILE"; then
			sed -i "/var \$dbuser;/a\ \tvar \$dbpass;" "$WEBSITE_ROOT/$CONF_FILE"
		fi

		# set original pkg version automaticly
		local upgrader_orange_ver_file="upgrader/apps/upgrader/lib/form/VersionInfoForm.php"
		sed -i "s/@SYNO_ORANGE_VER@/$orange_ver/g" "$WEBSITE_ROOT/$upgrader_orange_ver_file"
	fi
}

CustomUseUnixSocket()
{
	sed -i "s|this->dbport.*|this->dbport   = 'socket:$UNIX_SOCKET';|g" "$WEBSITE_ROOT/$CONF_FILE"
}

CustomSetInit()
{
	if ! CustomHasRunWebsiteSetup "$WEBSITE_ROOT"; then
		local install_file="installer/dbConfig.php"
		sed -i "s/@SYNO_DB_NAME@/$DB_NAME/g" "$WEBSITE_ROOT/$install_file"
		sed -i "s/@SYNO_DB_USER@/$DB_USER/g" "$WEBSITE_ROOT/$install_file"
	fi
}

CustomPostupgrade()
{
	# Check if OrangeHRM schema is changed
	local file_osc="/tmp/.OrangeHRM_Schema_Change"
	if [ -f "$file_osc" ]; then
		mv "$file_osc" "$WEBSITE_ROOT"
	fi
}

CustomStart()
{
	local ini_source="$SYNOLOGY_ADDED_PATH/etc/SYNO.SDS.$PKG_NAME.ini"
	local ini_dest="/usr/local/etc/php73/cli/conf.d"

	cp "$ini_source" "$ini_dest"
	restart pkg-WebStation-php73
}

CustomStop()
{
	local ini_dest="/usr/local/etc/php73/cli/conf.d/"
	rm "$ini_dest/SYNO.SDS.$PKG_NAME.ini"

	restart pkg-WebStation-php73
}
