#!/bin/sh

PACKAGE_DEST=$SYNOPKG_PKGDEST
PRIVATE_LOCATION="/usr/local/synoldap"
SIMPLESAML_DIR="/var/packages/DirectoryServer/target/simplesamlphp/"
BACKUP_DIR="/$(readlink /var/packages/DirectoryServer/target | cut -d/ -f2)/@appstore/.synoldap"
SERVICETOOL="/usr/syno/bin/servicetool"

# creating symbolic link in user preserved area
if [ -d ${PRIVATE_LOCATION} ]; then
	rm -rf ${PRIVATE_LOCATION}
fi
ln -sf "${PACKAGE_DEST}" ${PRIVATE_LOCATION}

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

# prepare default config file
# FIXME move open ldap configs install here

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

# add mail dir infomation into config

if [ -e ${UPGRADE_FILE} ]; then
	for f in authsources.conf saml20-idp-hosted.conf saml20-sp-remote.conf; do
		[ -f "$BACKUP_DIR/$f" ] || continue
		mv -f $BACKUP_DIR/$f $SIMPLESAML_DIR/
	done
	/bin/rm -rf $BACKUP_DIR
fi

exit 0
