#!/bin/sh

#--------TOR post upgrade 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
DAEMON_USER="torusr"

TORCONF="$PACKAGE_DIR/target/etc/tor"
ID_PATH="$PACKAGE_DIR/target/var/lib/tor/keys"

#Create required paths
mkdir -p $PACKAGE_DIR/target/var/lib/tor/cached-status
mkdir -p $PACKAGE_DIR/target/var/lib/tor/keys
mkdir -p $PACKAGE_DIR/target/var/log/tor
mkdir -p $PACKAGE_DIR/target/var/run/tor
mkdir -p $PACKAGE_DIR/target/etc/tor

#Restore tor_id and torrc from "Public" shared folder
	synoshare --get public
	if [ $? != 0 ]; then
		echo "Public share folder does not exist. Upgrade cannot be performed."> $SYNOPKG_TEMP_LOGFILE
		exit 1
	else
		pb_path=$(synoshare --get public | grep Path)
		pb_path=${pb_path#*\[}
		pb_path=${pb_path%\]*}
		echo $pb_path
		cp -f "${pb_path}/secret_id_key" "$ID_PATH"
		cp -f "${pb_path}/torrc" "$TORCONF"
	fi	


#Change owner of Tor Relay folder tree
chown -R $DAEMON_USER:root $PACKAGE_DIR/target
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/bin
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/etc
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/etc/tor
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/share
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/run
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/run/tor
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/lib
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/lib/tor
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/lib/tor/keys
chmod 700 $PACKAGE_DIR/target/var/lib/tor/keys
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/log
chown -R $DAEMON_USER:root $PACKAGE_DIR/target/var/log/tor



exit 0
