#!/bin/sh

PACKAGE_NAME=$SYNOPKG_PKGNAME
PKG_STATUS=$SYNOPKG_PKG_STATUS
PRIVATE_LOCATION="/var/packages/${PACKAGE_NAME}/target"
ORIG_CUST_MENU_LOCATION="/usr/syno/etc/dms"
SERVICETOOL="/usr/syno/bin/servicetool"

# creating symbolic link in user preserved area

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

# prepare default config file
PKG_USERCONF_DIR="/var/packages/${PACKAGE_NAME}/etc"
SYNOINFO="/etc/synoinfo.conf"
UPGRADE_FILE="/tmp/synodms.upgrade"
DEFAULT_RADIO_FOLDER="${PRIVATE_LOCATION}/etc/radio"
NEW_RADIO_FOLDER="/usr/syno/etc/radio"

if [ ! -d ${PKG_USERCONF_DIR} ]; then
	mkdir -p /usr/syno/etc/packages/${PACKAGE_NAME}
fi

if [ ! -d ${NEW_RADIO_FOLDER} ]; then
	mkdir ${NEW_RADIO_FOLDER}
fi

# move user setting from old location to new location
if [ ! -f ${PKG_USERCONF_DIR}/menu_custom1.xml ]; then
	if [ -f ${ORIG_CUST_MENU_LOCATION}/menu_custom1.xml ]; then
		cp ${ORIG_CUST_MENU_LOCATION}/menu_custom1.xml ${PKG_USERCONF_DIR}/menu_custom1.xml
	else
		cp ${PRIVATE_LOCATION}/etc/menu_custom1.xml ${PKG_USERCONF_DIR}/menu_custom1.xml
	fi
fi
if [ ! -f ${PKG_USERCONF_DIR}/menu_custom2.xml ]; then
	if [ -f ${ORIG_CUST_MENU_LOCATION}/menu_custom2.xml ]; then
		cp ${ORIG_CUST_MENU_LOCATION}/menu_custom2.xml ${PKG_USERCONF_DIR}/menu_custom2.xml
	else
		cp ${PRIVATE_LOCATION}/etc/menu_custom2.xml ${PKG_USERCONF_DIR}/menu_custom2.xml
	fi
fi
if [ ! -f ${PKG_USERCONF_DIR}/menu_custom3.xml ]; then
	if [ -f ${ORIG_CUST_MENU_LOCATION}/menu_custom3.xml ]; then
		cp ${ORIG_CUST_MENU_LOCATION}/menu_custom3.xml ${PKG_USERCONF_DIR}/menu_custom3.xml
	else
		cp ${PRIVATE_LOCATION}/etc/menu_custom3.xml ${PKG_USERCONF_DIR}/menu_custom3.xml
	fi
fi
if [ ! -f ${PKG_USERCONF_DIR}/client_list.json ]; then
	if [ -f ${ORIG_CUST_MENU_LOCATION}/client_list.json ]; then
		cp ${ORIG_CUST_MENU_LOCATION}/client_list.json ${PKG_USERCONF_DIR}/client_list.json
	fi
fi

# remove old customized menu
if [ ! -e ${UPGRADE_FILE} ]; then
	if [ -d ${ORIG_CUST_MENU_LOCATION} ]; then
		rm -rf ${ORIG_CUST_MENU_LOCATION}
	fi
fi

# prepare index config file
INDEX_PACKAGE_PATH=/usr/syno/etc/synoindex/packages/${PACKAGE_NAME}
if [ ! -d ${INDEX_PACKAGE_PATH} ]; then
	mkdir -p ${INDEX_PACKAGE_PATH}
fi

#Stop related daemons
/usr/syno/etc.defaults/rc.d/S66synoindexd.sh stop
/usr/syno/etc.defaults/rc.d/S77synomkthumbd.sh stop
/usr/syno/etc.defaults/rc.d/S88synomkflvd.sh stop

#Install INFO
cp -rf ${PRIVATE_LOCATION}/etc/index/INFO ${INDEX_PACKAGE_PATH}/INFO

#Start related daemons
/usr/syno/etc.defaults/rc.d/S66synoindexd.sh start
/usr/syno/etc.defaults/rc.d/S77synomkthumbd.sh start
/usr/syno/etc.defaults/rc.d/S88synomkflvd.sh start

if [ "INSTALL" == "$PKG_STATUS" ]; then
    /usr/syno/bin/synoindex -R media > /dev/null 2>&1
fi

# fetch mediaservice related strings
if [ ! -f ${PKG_USERCONF_DIR}/dmsinfo.conf ]; then
	touch ${PKG_USERCONF_DIR}/dmsinfo.conf
	grep -r _mediaservice ${SYNOINFO} >> ${PKG_USERCONF_DIR}/dmsinfo.conf
	sed -i '/_mediaservice/d' ${SYNOINFO}
	sed -i '/runmediaservice/d' ${SYNOINFO}
fi

#move default radio list to /usr/syno/etc/radio
IFS=$'\n'
for i in `ls -1 ${DEFAULT_RADIO_FOLDER}/*`
do
	RADIO_JSON=`basename $i`
	if [ ! -f ${NEW_RADIO_FOLDER}/${RADIO_JSON} ]; then
		cp -a ${DEFAULT_RADIO_FOLDER}/${RADIO_JSON} ${NEW_RADIO_FOLDER}/${RADIO_JSON}
	fi
done

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

install_synosdk_commonhook_plugin() {
	local ScriptsPath="${PRIVATE_LOCATION}/scripts"
	local destservicecommon="/usr/local/libexec"

	if [ ! -d ${destservicecommon} ]; then
		mkdir -p ${destservicecommon}
	fi
	ln -sf "${ScriptsPath}/MediaServerCommon.sh" "${destservicecommon}/MediaServerCommon.sh"
}

install_synosdk_servicehook_plugin() {
	local ScriptsPath="${PRIVATE_LOCATION}/scripts"
	local destservice="/usr/local/libexec/service"

	if [ ! -d ${destservice} ]; then
		mkdir -p ${destservice}
	fi
	cp -f "${ScriptsPath}/mediaserver_service.sh" "${destservice}/mediaserver_service.sh"
}

install_synosdk_sharehook_plugin() {
	local ScriptsPath="${PRIVATE_LOCATION}/scripts"
	local destshare_set="/usr/local/libexec/share_set"
	local destshare_delete="/usr/local/libexec/share_delete"

	if [ ! -d ${destshare_set} ]; then
		mkdir -p ${destshare_set}
	fi
	cp -f "${ScriptsPath}/mediaserver_shareset.sh" "${destshare_set}/mediaserver_shareset.sh"
	if [ ! -d ${destshare_delete} ]; then
		mkdir -p ${destshare_delete}
	fi
	cp -f "${ScriptsPath}/mediaserver_sharedelete.sh" "${destshare_delete}/mediaserver_sharedelete.sh"
}

install_synosdk_NetIfNameHook_plugin() {
	local ScriptsPath="${PRIVATE_LOCATION}/scripts"
	local destNetIfNameHook="/usr/local/libexec/net/ifname_change"

	if [ ! -d ${destNetIfNameHook} ]; then
		mkdir -p ${destNetIfNameHook}
	fi
	cp -f "${ScriptsPath}/mediaserver_net_ifname_change.sh" "${destNetIfNameHook}/mediaserver_net_ifname_change.sh"
}

check_upgrade_video_metadata() {
	local sql_upgrade_script="${PRIVATE_LOCATION}/scripts/sql/video_metadata.sh"

	if [ -f ${sql_upgrade_script} ]; then
		${sql_upgrade_script}
	fi
}

install_synosdk_commonhook_plugin
install_synosdk_servicehook_plugin
install_synosdk_sharehook_plugin
install_synosdk_NetIfNameHook_plugin
check_upgrade_video_metadata

exit 0
