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

PACKAGE_NAME="ThreatPrevention"
OLD_PACKAGE_NAME="IntrusionPrevention"
PACKAGE_TARGET_DIR="/var/packages/${PACKAGE_NAME}/target"
PACKAGE_USR_CONF_DIR="/var/packages/${PACKAGE_NAME}/etc"
PACKAGE_TARGET_RULES="${PACKAGE_TARGET_DIR}/etc/rules"
OLD_PACKAGE_USR_CONF_DIR="/usr/syno/etc/packages/${OLD_PACKAGE_NAME}"
DB_SCHEMA="${PACKAGE_TARGET_DIR}/schema/syno_create_postgresql"
SYNOTPSTOOL="${PACKAGE_TARGET_DIR}/tool/synotpstool"
CREATE_DB="/usr/bin/createdb"
PSQL="/usr/bin/psql"
SYNOSETKV="/usr/syno/bin/synosetkeyvalue"
SYNOGETKV="/usr/syno/bin/synogetkeyvalue"
NOTIFICATION_FILTER_SETTINGS="/usr/syno/etc/notification/notification_filter.settings"
NOTIFICATION_TAG_NAMES="ThreatPreventionNetworkEventDetected ThreatPreventionNetworkEventDropped ThreatPreventionDatabaseCorrupted ThreatPreventionDatabaseResetSuccess ThreatPreventionDatabaseResetFailed ThreatPreventionExportEventLogSuccess ThreatPreventionExportEventLogFailed"
AUTOUPDATE_DIR="/usr/syno/etc/data_update/project_list"

is_tps_db_existed()
{
	if [ `${PSQL} -U postgres -lqt | awk '{ print $1 }' | grep -e "^synotps$" | wc -l` -gt 0 ]; then
		return 0
	fi
	return 1
}

# move existing suricata rules/signatures from router to usb.
if [ -d ${PACKAGE_USR_CONF_DIR}/suricata ]; then
	if [ -d ${PACKAGE_TARGET_RULES}/suricata ]; then
		/bin/rm -rf ${PACKAGE_USR_CONF_DIR}/suricata
		/bin/rm -rf ${PACKAGE_USR_CONF_DIR}/old.rules.tar.gz
	else
		/bin/mv ${PACKAGE_USR_CONF_DIR}/suricata ${PACKAGE_TARGET_RULES}/suricata
		/bin/mv ${PACKAGE_USR_CONF_DIR}/etopen.version.txt ${PACKAGE_TARGET_RULES}/etopen.version.txt
		/bin/mv ${PACKAGE_USR_CONF_DIR}/etpro.version.txt ${PACKAGE_TARGET_RULES}/etpro.version.txt
		/bin/mv ${PACKAGE_USR_CONF_DIR}/old.rules.tar.gz ${PACKAGE_TARGET_RULES}/old.rules.tar.gz
	fi
else
	if [ ! -d ${PACKAGE_TARGET_RULES}/suricata/rules ]; then
		/bin/mkdir -p ${PACKAGE_TARGET_RULES}/suricata/
		/bin/tar zxf ${PACKAGE_TARGET_RULES}/emerging.rules.tar.gz -C ${PACKAGE_TARGET_RULES}/suricata/
		/bin/cp ${PACKAGE_TARGET_RULES}/emerging.rules.tar.gz ${PACKAGE_TARGET_RULES}/old.rules.tar.gz
		/bin/cp ${PACKAGE_TARGET_RULES}/version.txt ${PACKAGE_TARGET_RULES}/etopen.version.txt
		/bin/cp ${PACKAGE_TARGET_RULES}/version.txt ${PACKAGE_TARGET_RULES}/etpro.version.txt
	fi
fi

if [ "x$SYNOPKG_PKG_STATUS" != "xUPGRADE" ]; then
	if ! is_tps_db_existed; then
		# Create database
		${CREATE_DB} -U postgres synotps
		${PSQL} -U postgres synotps < ${DB_SCHEMA}
	fi
	if [ ! -f ${PACKAGE_USR_CONF_DIR}/notification_filter.conf ]; then
		/bin/cp ${PACKAGE_TARGET_DIR}/etc/notification/notification_filter.conf ${PACKAGE_USR_CONF_DIR}/notification_filter.conf
	fi

	if [ ! -f ${PACKAGE_USR_CONF_DIR}/sensor.conf ]; then
		/bin/cp ${PACKAGE_TARGET_DIR}/etc/sensor/sensor.conf ${PACKAGE_USR_CONF_DIR}/sensor.conf
	fi
else
	# Add new config to sensor.conf if not exists yet
	local KEY="enable_auto_export_events_during_postupgrade"
	local USER_SENSOR_CONF="${PACKAGE_USR_CONF_DIR}/sensor.conf"
	local value="$(${SYNOGETKV} ${USER_SENSOR_CONF} ${KEY})"
	if [ -z "${value}" ]; then
		local defaultValue="$(${SYNOGETKV} ${PACKAGE_TARGET_DIR}/etc/sensor/sensor.conf ${KEY})"
		${SYNOSETKV} ${USER_SENSOR_CONF} ${KEY} ${defaultValue}
	fi
fi

# Copy a classification.config for reference when updating signatures
if [ ! -f ${PACKAGE_TARGET_RULES}/suricata/classification.config ]; then
	cp ${PACKAGE_TARGET_RULES}/classification.config ${PACKAGE_TARGET_RULES}/suricata/classification.config
fi

# Copy a reference.config for reference when updating signatures
if [ ! -f ${PACKAGE_TARGET_RULES}/suricata/reference.config ]; then
	cp ${PACKAGE_TARGET_RULES}/reference.config ${PACKAGE_TARGET_RULES}/suricata/reference.config
fi

# Clean build_signature_database.conf
/bin/rm -f ${PACKAGE_USR_CONF_DIR}/build_signature_database.conf

if [ ! -f ${PACKAGE_USR_CONF_DIR}/db_size.conf ]; then
	/bin/touch ${PACKAGE_USR_CONF_DIR}/db_size.conf
fi

# Remove ruleset.conf if exist
if [ -f ${PACKAGE_USR_CONF_DIR}/ruleset.conf ]; then
	rm -f ${PACKAGE_USR_CONF_DIR}/ruleset.conf
fi

/bin/cp ${PACKAGE_TARGET_DIR}/etc/rules/signature.conf ${PACKAGE_USR_CONF_DIR}/signature.conf
/bin/cp ${PACKAGE_TARGET_DIR}/etc/rules/support_etpro_model.conf ${PACKAGE_USR_CONF_DIR}/support_etpro_model.conf
/bin/cp ${PACKAGE_TARGET_DIR}/etc/display/map.conf ${PACKAGE_USR_CONF_DIR}/map.conf
/bin/cp ${PACKAGE_TARGET_DIR}/etc/others/misc.conf ${PACKAGE_USR_CONF_DIR}/misc.conf
/bin/mkdir -p ${AUTOUPDATE_DIR} && /bin/cp ${PACKAGE_TARGET_DIR}/etc/autoupdate/synotps-database.json ${AUTOUPDATE_DIR}/synotps-database.json

# Set notification settings
for tagName in ${NOTIFICATION_TAG_NAMES}; do
	synosetkeyvalue ${NOTIFICATION_FILTER_SETTINGS} ${tagName} "mail,sms,mobile"
done
/usr/syno/bin/notification_utils --sync_setting

#migrate settings from ips
if [ -d ${OLD_PACKAGE_USR_CONF_DIR} ]; then
	/bin/mv ${OLD_PACKAGE_USR_CONF_DIR}/sensor.conf ${PACKAGE_USR_CONF_DIR}/sensor.conf
	/bin/mv ${OLD_PACKAGE_USR_CONF_DIR}/updater.conf ${PACKAGE_USR_CONF_DIR}/updater.conf
	/bin/mv ${OLD_PACKAGE_USR_CONF_DIR}/db_size.conf ${PACKAGE_USR_CONF_DIR}/db_size.conf
	/bin/mv ${OLD_PACKAGE_USR_CONF_DIR}/notification.conf ${PACKAGE_USR_CONF_DIR}/notification.conf
	/bin/mv ${OLD_PACKAGE_USR_CONF_DIR}/notification_filter.conf ${PACKAGE_USR_CONF_DIR}/notification_filter.conf
fi

synopkg uninstall ${OLD_PACKAGE_NAME} &
${SYNOSETKV} /var/packages/${PACKAGE_NAME}/INFO install_conflict_packages ${OLD_PACKAGE_NAME}

#alter synotps database schema before 0741
${PSQL} -U postgres synotps -c "ALTER TABLE device ADD COLUMN detect BOOLEAN DEFAULT true"
${PSQL} -U postgres synotps -c "ALTER TABLE device ADD COLUMN loading_score INT8 NOT NULL DEFAULT 0"
${PSQL} -U postgres synotps -c "CREATE TABLE loading (start_time INT8 NOT NULL,
													  end_time INT8 NOT NULL,
													  ip varchar(46) NOT NULL,
													  packet_number INT4 NOT NULL); "
#remove unknown device before 0741
${PSQL} -U postgres synotps -c "DELETE FROM device WHERE mac='GG:00:00:00:00:00'"

${SYNOTPSTOOL} --mkdir-for-export-events

exit 0
