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

PACKAGE_NAME="ThreatPrevention"
PACKAGE_USR_CONF_DIR="/var/packages/${PACKAGE_NAME}/etc"
DROP_DB="/usr/bin/dropdb"
DB_NAME="synotps"
AUTOUPDATE_DIR="/usr/syno/etc/data_update/project_list"

pgcmd() {
	/usr/bin/psql -U postgres --dbname=${DB_NAME} --command="$1"
}

if [ "x$SYNOPKG_PKG_STATUS" != "xUPGRADE" ]; then
	# database part
	if [ "x${pkgwizard_remove_db}" = "xtrue" ] && [ "x${pkgwizard_remove_config}" = "xtrue" ]; then
		# Remove database
		${DROP_DB} -U postgres synotps
	elif [ "x${pkgwizard_remove_db}" = "xtrue" ]; then
		pgcmd "TRUNCATE event RESTART IDENTITY CASCADE;"
	elif [ "x${pkgwizard_remove_config}" = "xtrue" ]; then
		pgcmd "TRUNCATE policy_class, policy_filter, policy_signature, modified_signature;"
	fi

	# conf files part
	if [ "x${pkgwizard_remove_config}" = "xtrue" ]; then
		# Remove user config
		/bin/rm -f ${PACKAGE_USR_CONF_DIR}/udc_counter
		/bin/rm -f ${PACKAGE_USR_CONF_DIR}/*.conf
		/bin/rm -f ${PACKAGE_USR_CONF_DIR}/*.info
		/bin/rm -rf ${PACKAGE_USR_CONF_DIR}/suricata
	fi
	# remove cache files
	/bin/rm -f ${PACKAGE_USR_CONF_DIR}/udc_event.json
	# remove autoupdate files
	/bin/rm -f ${AUTOUPDATE_DIR}/synotps-database.json
	# remove reset signature database file
	/bin/rm -f /tmp/.synotps_reset_signature_database
fi

exit 0
