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

PKG_DIR="vtigercrm"
IS_SET_HTACCESS="false"

CleanMigrationPatchInWebsiteRoot()
{
	rm -rf "$WEBSITE_ROOT/vtiger7.zip" "$WEBSITE_ROOT/migrate-patch" "$WEBSITE_ROOT/migration" "$WEBSITE_ROOT/"*migrate
}

CleanMigrationPatchInBackup()
{
	rm -rf "$BACKUP_PATH/vtiger7.zip" "$BACKUP_PATH/migrate-patch" "$BACKUP_PATH/migration" "$BACKUP_PATH/"*migrate
}

CustomBackupData()
{
	local backup_dir="$(CustomGetBackupPrefix)/$PKG_DIR";
	rm -rf "${backup_dir}"
	mkdir -p "${backup_dir}"
	CleanMigrationPatchInWebsiteRoot
	cp -a --reflink=auto "$WEBSITE_ROOT/." "${backup_dir}"
}

CustomChangeDBUserInfo()
{
	local path="$1"
	local db_user="$(echo "$2" | QuoteSed)"
	local db_pass="$(echo "$3" | QuoteSed)"
	if [ -s "$path/$CONF_FILE" ]; then
		sed -i "s/\$dbconfig\['db_username']\\s*=.*/\$dbconfig\['db_username'] = '$db_user';/g" "$path/$CONF_FILE"
		sed -i "s/\$dbconfig\['db_password']\\s*=.*/\$dbconfig\['db_password'] = '$db_pass';/g" "$path/$CONF_FILE"
	fi
}

GetVtigerVersion()
{
	local vtiger_source_root="$1"
	local vtiger_ver_path="${vtiger_source_root}/vtigerversion.php"
	local vtiger_version=""
	if [ -s "$vtiger_ver_path" ]; then
		vtiger_version=$(php -r "include('$vtiger_ver_path'); echo \$vtiger_current_version;")
	fi
	echo "$vtiger_version"
}

IsUpgradingVtigerCRM()
{
	local prev_vtiger_version="$(GetVtigerVersion "$BACKUP_PATH")"
	local new_vtiger_version="$(GetVtigerVersion "$SYNOPKG_PKGDEST/vtigercrm")"
	if [ "$prev_vtiger_version" == "$new_vtiger_version" ]; then
		false
		return
	fi
	true
	return
}

RestoreToPreviousVersion()
{
	CleanMigrationPatchInBackup
	rm -rf "$WEBSITE_ROOT"
	cp -a --reflink=auto "$BACKUP_PATH/." "$WEBSITE_ROOT"
	# fix login issue by using new extract code
	cp -a "$SYNOPKG_PKGDEST/$PKG_DIR/modules/Migration/actions/Extract.php" "$WEBSITE_ROOT/modules/Migration/actions/Extract.php"
}

SetupMigrationPatch()
{
	CleanMigrationPatchInWebsiteRoot
	# put migration patch
	cp -af "$SYNOPKG_PKGDEST/migrate-patch/"* "$WEBSITE_ROOT/"
	# redirect to migration patch
	/bin/sed -i "2i\include_once 'syno_redirect.php';//SYNOLOGY-REDIRECT" "$WEBSITE_ROOT/index.php"
	cp -a "$SYNOLOGY_ADDED_PATH/syno_redirect.php" "$WEBSITE_ROOT/"
}

CustomRestoreUpgrade()
{
	RestoreToPreviousVersion
	if $(IsUpgradingVtigerCRM); then
		echo "setup migration patch"
		SetupMigrationPatch
	fi
}

RemoveSource()
{
	rm -rf "$SYNOPKG_PKGDEST/migrate-patch"
}

CustomStart()
{
	local ini_source="/var/packages/vtigerCRM/target/synology_added/etc/SYNO.SDS.vtigerCRM.ini"
	local ini_dest="/usr/local/etc/php74/cli/conf.d/"
	cp $ini_source $ini_dest
	restart pkg-WebStation-php74
}

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

	restart pkg-WebStation-php74
}

CustomSetInit()
{
	RemoveSource
	RemoveHandler
	if [ -s "$WEBSITE_ROOT/$DB_SETTING" ]; then
		sed -i "s/@SYNO_DB_NAME@/$DB_NAME/g" "$WEBSITE_ROOT/$DB_SETTING"
		sed -i "s/@SYNO_DB_USER@/$DB_USER/g" "$WEBSITE_ROOT/$DB_SETTING"
	fi
}

InstallNewCleanUI()
{
	cp -a "$SYNOPKG_PKGDEST/$PKG_DIR" "$(dirname "$WEBSITE_ROOT")"
	CleanMigrationPatchInWebsiteRoot
}

CustomSetCleanNew()
{
	InstallNewCleanUI
}

CustomNewInst()
{
	InstallNewCleanUI
}

