#!/bin/sh

# customized info for different package
PRIVATE_LOCATION="/var/packages/CloudStation/target"
UPGRADE_FILE="/tmp/cloudstation.upgrade"
PKG_USERCONF_DIR="/var/packages/CloudStation/etc"
SYNO_GET_KEY_VALUE="/usr/syno/bin/synogetkeyvalue"


# general routine
. /var/packages/CloudStation/scripts/pkg_utils
get_upgrade_status
if [ "1" = "$?" ]; then
	echo "Package is upgrading. You shall wait until upgrading is complete." > $SYNOPKG_TEMP_LOGFILE
	return 1;
fi

# special action for real-uninstall
logger -p 0 "Cloud Station preunist: check upgrade file $UPGRADE_FILE ..."
if [ ! -f "${UPGRADE_FILE}" ]; then

    if [ "$pkgwizard_keep_cstn_db" != "true" ]; then

	${CSTN_CLOUD_CONTROL} cleanup

	# remove mark file for broken data base
	rm -rf "/usr/syno/etc/packages/CloudStation/cstn_dbmv_mark"
	rm -rf "/usr/syno/etc/packages/CloudStation/cstndb_ver.conf"
	rm -rf "${PKG_USERCONF_DIR}/db-path.conf"
	rm -rf "$CSTN_INSTALL_TMP_FILE"
    fi

    ## remove enable_status to clean up status in previous version
    rm -rf "/usr/syno/etc/packages/CloudStation/enable_status"

    #Stop relay service for cloudstation when package is uninstalled.
    /usr/syno/sbin/synorelayd -r cloudstation > /dev/null 2>&1

    # remove port-forwarding config file (bug#1713 should not remove if upgrading)
    ${SERVICETOOL} --remove-configure-file --package cloudstation_port
fi


# Remove symbolic link from share hook folder

RemoveHookscripts

# remove user data collection scripts
rm -f ${USER_DATA_COLLECTION_DIR}/cloud_station.config

exit 0
