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

# install default codecs.conf
if [ ! -f "${PKG_USERCONF_DIR}/codecs.conf" ]; then
	cp ${PRIVATE_LOCATION}/etc/codecs.conf ${PKG_USERCONF_DIR}/codecs.conf
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

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

	`$cmd`

	logger -p user.err -t "MediaServer" "Re-index triggered, cmd [$cmd], script [$(basename $0) $@]"

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

# After DSM 6.0 beta 2, Video Station Integration only available when Video Station
# is enabled (Media Server commit bb51dc6d). There is no need to update video_metadata DB
# because DSM will force user upgrad Video Station.
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}
}

# check_upgrade_video_metadata
install_lighttpd_log_rotate_file
install_user_data_collector_file

# For lower package privilege
chown -R ${PACKAGE_NAME}:${PACKAGE_NAME} ${PKG_USERCONF_DIR}/

exit 0
