#!/bin/sh

#--------TOR pre uninstall script by gigix
#--------package maintained at ggteknikus.tk

PACKAGE_NAME=$SYNOPKG_PKGNAME
PACKAGE_VER=$SYNOPKG_PKGVER
PACKAGE_DIR="/var/packages/TorRelay"
PACKAGE_DEST=$SYNOPKG_PKGDEST

STARTUP="/usr/syno/etc.defaults/rc.d"
ID_PATH="$PACKAGE_DIR/target/var/lib/tor/keys"
TORCONF="$PACKAGE_DIR/target/etc/tor"

#Save tor_id to "Public" shared folder when back-up is enabled
if "$TOR_BACKUP" ; then
	synoshare --get public
	if [ $? != 0 ]; then
		echo "Public share folder does not exist. Back-up cannot be performed." > $SYNOPKG_TEMP_LOGFILE
		exit 1
	else
		pb_path=$(synoshare --get public | grep Path)
		pb_path=${pb_path#*\[}
		pb_path=${pb_path%\]*}
		cp -f "${ID_PATH}/secret_id_key" "$pb_path"
	fi	
fi
#Save torrc to "Public" shared folder when back-up is enabled
if "$TOR_BACKUPCONF" ; then
	synoshare --get public
	if [ $? != 0 ]; then
		echo "Public share folder does not exist. Back-up cannot be performed." > $SYNOPKG_TEMP_LOGFILE
		exit 1
	else
		pb_path=$(synoshare --get public | grep Path)
		pb_path=${pb_path#*\[}
		pb_path=${pb_path%\]*}
		cp -f "${TORCONF}/torrc" "$pb_path"
	fi	
fi

# Services removal
if ["UNINSTALL" = "$SYNOPKG_PKG_STATUS" ]; then
# Note: only remove service configure file when uninstalling the package
/usr/syno/bin/servicetool --remove-configure-file --package torrelay.sc
fi

#Remove startup script
rm -f "${STARTUP}/S99torrelay.sh"

#remove daemon user
synouser --del torusr


exit 0
