#!/bin/sh -e

# checking debian-tor account

uid=`getent passwd debian-tor | cut -d ":" -f 3`
home=`getent passwd debian-tor | cut -d ":" -f 6`

# if there is the uid the account is there and we can do
# the sanit(ar)y checks otherwise we can safely create it.

if [ "$uid" ]; then
	if [ "$home" = "/var/lib/tor" ]; then
		:
		#echo "debian-tor homedir check: ok"
	else
		echo "ERROR: debian-tor account has an unexpected home directory!"
		echo "It should be '/var/lib/tor', but it is '$home'."
		echo "Removing the debian-tor user might fix this, but the question"
		echo "remains how you got into this mess to begin with."
		exit 1
	fi
else
	adduser --quiet \
		--system \
		--disabled-password \
		--home /var/lib/tor \
		--no-create-home \
		--shell /bin/false \
		--group \
		debian-tor
fi


for i in lib log; do
	if ! [ -d "/var/$i/tor" ]; then
		echo "Something or somebody made /var/$i/tor disappear."
		echo "Creating one for you again."
		mkdir "/var/$i/tor"
	fi
done

command -v restorecon >/dev/null 2>&1 && restorecon /var/lib/tor
chown debian-tor:debian-tor /var/lib/tor
chmod 02700 /var/lib/tor

command -v restorecon >/dev/null 2>&1 && restorecon /var/log/tor
chown debian-tor:adm /var/log/tor
chmod 02750 /var/log/tor


tor_error_init() {
	echo "Tor was unable to start due to configuration errors.";
	echo "Please fix them and manually restart the tor daemon using";
	echo " ´service start tor´";
}

# Automatically added by dh_apparmor/4.1.0-1
if [ "$1" = "configure" ]; then
    APP_PROFILE="/etc/apparmor.d/system_tor"
    if [ -f "$APP_PROFILE" ]; then
        # Add the local/ include
        LOCAL_APP_PROFILE="/etc/apparmor.d/local/system_tor"

        test -e "$LOCAL_APP_PROFILE" || {
            mkdir -p `dirname "$LOCAL_APP_PROFILE"`
            install --mode 644 /dev/null "$LOCAL_APP_PROFILE"
        }

        # Reload the profile, including any abstraction updates
        if aa-enabled --quiet 2>/dev/null; then
            apparmor_parser -r -T -W "$APP_PROFILE" || true
        fi
    fi
fi
# End automatically added section
# Automatically added by dh_systemd_enable/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'tor.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'tor.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'tor.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'tor.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_runit/2.16.4
# Unlike postrm, I can be sure, that runit-helper is present on
# postinst.
#start: remove after trixie
if  [ ! -e /usr/lib/runit-helper/runit-helper ]; then
    #should not happen
    echo "warning: can't find runit-helper, postinst action skipped!"
    echo "warning: runit-helper: please report this bug"
fi
#end: remove after trixie
if [ -z "${DPKG_ROOT:-}" ] && [ -x /usr/lib/runit-helper/runit-helper ]; then
    NAME='tor' ENABLE='yes' ONUPGRADE='restart' /usr/lib/runit-helper/runit-helper postinst "$@"
fi
# End automatically added section
# Automatically added by dh_installdeb/13.24.2
dpkg-maintscript-helper rm_conffile /etc/tor/tor-tsocks.conf 0.2.4.12-alpha-1 tor -- "$@"
# End automatically added section
# Automatically added by dh_installinit/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -z "$DPKG_ROOT" ] && [ -x "/etc/init.d/tor" ]; then
		update-rc.d tor defaults >/dev/null
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		invoke-rc.d tor $_dh_action || tor_error_init
	fi
fi
# End automatically added section


exit 0
