#!/bin/sh
# Copyright (c) 2000-2021 Synology Inc. All rights reserved.
PACKAGE_NAME="ThreatPrevention"
PACKAGE_TARGET_DIR="/var/packages/${PACKAGE_NAME}/target"
PACKAGE_USR_CONF_DIR="/var/packages/${PACKAGE_NAME}/etc"
SYNOTPSTOOL="${PACKAGE_TARGET_DIR}/tool/synotpstool"
SRM_STRING_FILE="/usr/syno/synoman/webman/texts/${SYNOPKG_DSM_LANGUAGE}/strings"
SYNOGETKV="/usr/syno/bin/synogetkeyvalue"

do_export="no"

if [ "xtrue" == "x${pkgwizard_export_event_option_yes}" ]; then
	do_export="yes"
elif [ "xtrue" == "x${pkgwizard_export_event_option_no}" ]; then
	do_export="no"
else
	# Auto upgrade, user did not interact with the wizard
	local KEY="enable_auto_export_events_during_postupgrade"
	do_export="$(${SYNOGETKV} ${PACKAGE_USR_CONF_DIR}/sensor.conf ${KEY})"
fi

if [ "xyes" == "x${do_export}" ]; then
	${SYNOTPSTOOL} --export-events
	if [ 0 -eq $? ]; then
		${SYNOTPSTOOL} --clear-events
	else
		local string=$(/usr/syno/bin/get_section_key_value ${SRM_STRING_FILE} usb usb_handlefail)
		echo ${string} > ${SYNOPKG_TEMP_LOGFILE}
	fi
fi

exit 0
