#!/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"
fi

# add mail dir infomation into config


# 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 bandwidth config addon
if [ ! -d ${BANDWIDTH_ADDON_DIR} ]; then
	mkdir -p ${BANDWIDTH_ADDON_DIR}
fi

ln -sf ${CLOUD_STATION_PACKAGE}/target/etc/CloudStation_Bandwidth.json ${BANDWIDTH_ADDON_DIR}/CloudStation_16.json

exit 0
