#!/bin/sh
# Copyright (c) 2000-2011 Synology Inc. All rights reserved.

# customized items of package
if [ -z "$SYNOPKG_PKGNAME" ]; then
        # prior DSM 3.2, start script will be run without environment variable
        #  at boot time, thus we need a workaround to gather name and version info
        PACKAGE_NAME="DownloadStation"
        PACKAGE_VER=`get_key_value "/var/packages/${PACKAGE_NAME}/INFO" "version"`
else
        PACKAGE_NAME=$SYNOPKG_PKGNAME
        PACKAGE_VER=$SYNOPKG_PKGVER
fi


PACKAGE_DIR="/var/packages/DownloadStation"
PACKAGE_DEST=$SYNOPKG_PKGDEST
PACKAGE_CONFIG_MIGRATE_SH="/usr/syno/bin/package_config_migrate.sh"
SERVICETOOL="/usr/syno/bin/servicetool"
SED="/bin/sed"

SYNOINFO_KEYS="download_ul_rate download_dl_rate download_http_dl_rate download_start_port
 download_port_map_enabled download_seeding_interval download_seeding_ratio download_order download_enable_dht download_dhtport
 download_btencrypt download_btmaxpeers download_maxtasks download_nzb_server download_nzb_port download_nzb_auth download_bt_cache_limit
 download_nzb_user download_nzb_password download_nzb_numconn download_nzb_encryption download_nzb_dl_rate
 download_ftpmax_enable download_ftpmax download_email_notification download_amule_dl_max
 download_desktop_notification download_schedule_enable download_schedule download_rss_update_interval
 download_enable_amule download_amule_ul_rate download_amule_dl_rate download_amule_tcp_port download_amule_udp_port
 download_amule_max_conn download_amule_enable_obfuscation download_amule_outgoing_obfuscation
 download_amule_only_obfuscation download_amule_auto_update_server download_amule_email_notification
 download_amule_desktop_notification download_amule_auto_connect download_amule_schedule_enable download_amule_schedule
 download_alt_dl_rate download_alt_ul_rate download_amule_alt_dl_rate download_amule_alt_ul_rate download_enable_xunlei"

DELETE_SYNOINFO_KEYS="download_enabled download_when download_end_port support_download download_rapidshare_user
 download_rapidshare_password download_megaupload_user download_megaupload_password filehosting_server download_httpftp_dl_rate"

install_etc_and_initial() {

	if [ ! -f "${PACKAGE_DIR}/etc/settings.conf" ]; then
		cp -rf "${PACKAGE_DEST}"/etc/settings.conf "${PACKAGE_DIR}"/etc
	fi

	if [ -d "/usr/syno/etc/amule" ]; then
		cp -rf /usr/syno/etc/amule "${PACKAGE_DIR}"/etc
		if [ $? = 0 ]; then
			rm -rf /usr/syno/etc/amule
		fi
	elif [ ! -d "${PACKAGE_DIR}/etc/amule" ]; then
		cp -rf "${PACKAGE_DEST}"/etc/amule "${PACKAGE_DIR}"/etc
		touch "${PACKAGE_DIR}"/etc/amule/synouser_default_destination.conf
	fi
	if [ ! -f "${PACKAGE_DIR}/etc/amule/synouser_default_destination.conf" ]; then
		touch "${PACKAGE_DIR}"/etc/amule/synouser_default_destination.conf
	fi

	if [ -d "/usr/syno/etc/download" ]; then
		cp -rf /usr/syno/etc/download "${PACKAGE_DIR}"/etc
		if [ $? = 0 ]; then
			rm -rf /usr/syno/etc/download
		fi
	elif [ ! -d "${PACKAGE_DIR}/etc/download" ]; then
		mkdir -m 755 "${PACKAGE_DIR}"/etc/download
		touch "${PACKAGE_DIR}"/etc/download/host.conf
		touch "${PACKAGE_DIR}"/etc/download/btsearch.conf
		touch "${PACKAGE_DIR}"/etc/download/xunlei.conf
	fi
	if [ ! -f "${PACKAGE_DIR}/etc/download/host.conf" ]; then
		touch "${PACKAGE_DIR}"/etc/download/host.conf
	fi
	if [ ! -f "${PACKAGE_DIR}/etc/download/btsearch.conf" ]; then
		touch "${PACKAGE_DIR}"/etc/download/btsearch.conf
	fi
	if [ ! -f "${PACKAGE_DIR}/etc/download/xunlei.conf" ]; then
		touch "${PACKAGE_DIR}"/etc/download/xunlei.conf
	fi
}


upgradesetting() {
	$PACKAGE_CONFIG_MIGRATE_SH move "/etc/synoinfo.conf" "${PACKAGE_DIR}/etc/settings.conf" "$SYNOINFO_KEYS"
	$PACKAGE_CONFIG_MIGRATE_SH del "/etc/synoinfo.conf" "$DELETE_SYNOINFO_KEYS"

	#set bt cache size by MemTotal
	GET_KEY_VALUE="/bin/get_key_value"
	SETTINGS_CONF_PATH="/var/packages/DownloadStation/etc/settings.conf"
	${GET_KEY_VALUE} ${SETTINGS_CONF_PATH} "download_bt_cache_limit" > /dev/null
	if [ $? == 0 ] ; then
		tmp=`cat /proc/meminfo | grep MemTotal | cut -d ':' -f 2 | cut -d 'k' -f 1`;  memSize=`expr $tmp`;
		if [ $memSize -gt 512000 ] ; then
			cacheSize=32;
		elif [ $memSize -gt 256000 ] ; then
			cacheSize=16;
		elif [ $memSize -gt 128000 ] ; then
			cacheSize=8;
		else
			cacheSize=4;
		fi
		echo "download_bt_cache_limit=\""$cacheSize"\"">>${SETTINGS_CONF_PATH};
	fi

}

modify_port_setting()
{
	local confPath="$1"
	local confKey="$2"
	local portDefault="$3"
	local portType="$4"
	local portFile="$5"

	local portValue=`/bin/get_key_value ${confPath} ${confKey}`
	if [ ! "${portValue}" == "" -a ! "${portValue}" == "0" ]; then
		${SED} -i "s,${portDefault}/${portType},${portValue}/${portType}," ${portFile}
	fi
}

install_port_setting()
{
	local dlSettingsConf="${PACKAGE_DIR}/etc/settings.conf"
	local dlPortFileName="synodownload_port"
	local dlPortFilePath="${PACKAGE_DIR}/target/etc/synodownload_port"
	local dlPortFileTemp="/tmp/$dlPortFileName"

	# copy original to temp
	cp -f ${dlPortFilePath} ${dlPortFileTemp}

	# update port value
	modify_port_setting ${dlSettingsConf} download_start_port "16881" tcp ${dlPortFileTemp}
	modify_port_setting ${dlSettingsConf} download_dhtport "6881" udp ${dlPortFileTemp}
	modify_port_setting ${dlSettingsConf} download_amule_tcp_port  "4662" tcp ${dlPortFileTemp}
	modify_port_setting ${dlSettingsConf} download_amule_udp_port  "4672" udp ${dlPortFileTemp}

	# add port-forwarding config file
	${SERVICETOOL} --install-configure-file --package ${dlPortFileTemp}

	rm ${dlPortFileTemp}
}

install_synosdk_userhook_plugin()
{
	local dlScriptsPach="${PACKAGE_DIR}/target/scripts"
	local destusercommon="/usr/local/libexec"
	local destuserset="/usr/local/libexec/user_set"
	local destuserdelete="/usr/local/libexec/user_delete"

	if [ ! -d ${destusercommon} ]; then
		mkdir -p ${destusercommon}
	fi
	ln -s "${dlScriptsPach}/DownloadUserCommon.sh" "${destusercommon}/DownloadUserCommon.sh"
	if [ ! -d ${destuserset} ]; then
		mkdir -p ${destuserset}
	fi
	cp -f "${dlScriptsPach}/synodownload_userset.sh" "${destuserset}/synodownload_userset.sh"
	if [ ! -d ${destuserdelete} ]; then
		mkdir -p ${destuserdelete}
	fi
	cp -f "${dlScriptsPach}/synodownload_userdelete.sh" "${destuserdelete}/synodownload_userdelete.sh"
}

migrate()
{
	OLD_PKGVER_FILE="/tmp/synodl_old_pkgver"
	if [ -f $OLD_PKGVER_FILE ]; then
		read SYNOPKG_OLD_PKGVER < $OLD_PKGVER_FILE
		rm $OLD_PKGVER_FILE
	fi

	migrate_script_dir="/var/packages/${SYNOPKG_PKGNAME}/target/scripts/migrate"
	migrate_scripts=$(find $migrate_script_dir/ -name "*.sh" | sort)
	for script in $migrate_scripts; do
		SYNODL_OLD_PKGVER=${SYNOPKG_OLD_PKGVER} $script
	done
}

install_etc_and_initial
upgradesetting
install_port_setting
install_synosdk_userhook_plugin
migrate

exit 0
