#!/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"
SYNOSERVICE="/usr/syno/sbin/synoservice"

# 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

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

	# create default shares
	${SYNOSERVICE} --setshare music
	${SYNOSERVICE} --setshare photo
	${SYNOSERVICE} --setshare video
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


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

#Pause related daemons
/usr/syno/sbin/synoservicecfg --pause-by-reason synoindexd mediaserver
/usr/syno/sbin/synoservicecfg --pause-by-reason synomkthumbd mediaserver
/usr/syno/sbin/synoservicecfg --pause-by-reason synomkflvd mediaserver

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

#Resume related daemons
/usr/syno/sbin/synoservicecfg --resume-by-reason synoindexd mediaserver
/usr/syno/sbin/synoservicecfg --resume-by-reason synomkthumbd mediaserver
/usr/syno/sbin/synoservicecfg --resume-by-reason synomkflvd mediaserver

# 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"
}

install_synosdk_userhook_plugin() {
	local ScriptsPath="${PRIVATE_LOCATION}/scripts"
	local destuser_delete="/usr/local/libexec/user_delete"

	if [ ! -d ${destuser_delete} ]; then
		mkdir -p ${destuser_delete}
	fi
	cp -f "${ScriptsPath}/mediaserver_userdelete.sh" "${destuser_delete}/mediaserver_userdelete.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_lighttpd_log_rotate_file() {
	local src_file="${PRIVATE_LOCATION}/log/lighttpd.conf.logrotate"
	local dest_dir="/usr/local/etc/logrotate.d"

	if [ ! -d ${dest_dir} ]; then
		mkdir -p ${dest_dir}
	fi
	cp -f ${src_file} "${dest_dir}/lighttpd"
}

install_user_data_collector_file() {
	local src_file=""
	local dest_dir=""
	# install conf file
	src_file="${PRIVATE_LOCATION}/user_data_collector/mediaserver.config"
	dest_dir="/usr/syno/etc/user.data.conf"
	if [ ! -d ${dest_dir} ]; then
		mkdir -p ${dest_dir}
	fi
	cp -f ${src_file} ${dest_dir}
}

install_synosdk_commonhook_plugin
install_synosdk_servicehook_plugin
install_synosdk_sharehook_plugin
install_synosdk_NetIfNameHook_plugin
install_synosdk_userhook_plugin
check_upgrade_video_metadata
install_lighttpd_log_rotate_file
install_user_data_collector_file

exit 0
