#!/bin/sh

PACKAGE_DEST=$SYNOPKG_PKGDEST
PRIVATE_LOCATION="/var/packages/CloudStation/target"
EXVERSION_LOCATION="/usr/local/cloudstation"
PKG_MAIL_DIR="${PRIVATE_LOCATION}/UI/texts/"


. /var/packages/CloudStation/scripts/pkg_utils

# creating symbolic link in user preserved area
if [ -d ${EXVERSION_LOCATION} ]; then
	rm -rf ${EXVERSION_LOCATION}
fi

# creating symbolic link in non-preserved area
#  (these should be created in start() script)

# prepare default config file
PKG_USERCONF_DIR="/var/packages/CloudStation/etc"

# copy conf settings
if [ ! -e "${PKG_USERCONF_DIR}/setting.conf" ]; then
    cp "${PRIVATE_LOCATION}/etc/setting.conf" "${PKG_USERCONF_DIR}/setting.conf"
elif [ "yes" != `get_key_value /etc/synoinfo.conf userHomeEnable` ]; then
    cat "${PKG_USERCONF_DIR}/setting.conf" > /tmp/$$.sfconf_tmp
    rm "${PKG_USERCONF_DIR}/setting.conf"
    mv "/tmp/$$.sfconf_tmp" "${PKG_USERCONF_DIR}/setting.conf"
fi

# add port-forwarding config file
${SERVICETOOL} --install-configure-file --package ${PRIVATE_LOCATION}/etc/cloudstation_port

# add mail dir infomation into config

# Create symolic link to share hook folder

AddNewHookscripts

# Copy db version mark to package conf folder.
# This mark file indicates that this db is generated in which version of Cloud Station package.
# It is used for package downgrading check.
cp -rf "${CSTN_PKG_DB_MARK_FILE}" "${CSTN_DB_MARK_FILE}"

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

# start relay service for Cloud Station when the package is installed
/usr/syno/sbin/synorelayd -a cloudstation > /dev/null 2>&1

# add user data collection script
ln -sf ${CLOUD_STATION_PACKAGE}/target/etc/cloud_station.config ${USER_DATA_COLLECTION_DIR}

exit 0
