HEX
Server: Apache/2.4.63 (Unix)
System: Linux Synopilou92 4.4.302+ #72806 SMP Mon Jul 21 23:16:00 CST 2025 x86_64
User: pilou92 (1026)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: //var/packages/MediaServer/scripts/postinst
#!/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"
SERVICE_TOOL="/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"
PKG_VAR_DIR="/var/packages/${PACKAGE_NAME}/var"
OLD_LIGHTTPD_LOG_DIR="/var/log/lighttpd"
LIGHTTPD_LOG_DIR="${PKG_VAR_DIR}/log/lighttpd"
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

# delete codecs.conf
if [ -f "${PKG_USERCONF_DIR}/codecs.conf" ]; then
	rm -f ${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
	${SERVICE_TOOL} --setshare music
	${SERVICE_TOOL} --setshare photo
	${SERVICE_TOOL} --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

# create config.conf
if [ ! -f ${PKG_USERCONF_DIR}/config.conf ]; then
    touch ${PKG_USERCONF_DIR}/config.conf
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_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}
}

ensure_lighttpd_log_dir_privilege() {
	if [ -d "${LIGHTTPD_LOG_DIR}" ]; then
		/bin/chmod 0755 "${LIGHTTPD_LOG_DIR}"
	fi
}

remove_lighttpd_log_rotate_file() {
	local destconf="/usr/local/etc/logrotate.d/lighttpd"
	rm -f ${destconf}
}

remove_dms_log_rotate_file() {
	local destconf="/usr/local/etc/logrotate.d/dms"
	rm -f ${destconf}
}

move_log_file() {
	local pkg_log_dir="${PKG_VAR_DIR}/log"

	if [ ! -d "${pkg_log_dir}" ]; then
		/bin/mkdir "${pkg_log_dir}"
	fi
	/bin/find /var/log -name "dms.log*" -exec /bin/mv "{}" "${pkg_log_dir}" \;
	if [ -d "${OLD_LIGHTTPD_LOG_DIR}" ]; then
		/bin/mv "${OLD_LIGHTTPD_LOG_DIR}" "${pkg_log_dir}"
	fi
}

change_database_collate() {
	local collate=`/bin/psql -tA -U postgres -c "SELECT datcollate FROM pg_database WHERE datname='mediaserver';"`

	if [ "$collate" != "C" ]; then
		echo "update db collate"
		/bin/psql -U postgres -c "UPDATE pg_database SET datcollate='C', datctype='C' WHERE datname='mediaserver';"
		/bin/psql -U postgres mediaserver -c "REINDEX database mediaserver;" &
	fi
}

# check_upgrade_video_metadata
install_user_data_collector_file

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

move_log_file

ensure_lighttpd_log_dir_privilege

# remove former log rotate file
remove_lighttpd_log_rotate_file
remove_dms_log_rotate_file

change_database_collate

exit 0