#! /bin/bash
# Generated by tor.SlackBuild for tor-0.1.1.24-i486-1McD_slack10.2.tgz on 19-Oct-2006

function upd_conf {
 	# If there's no config file by that name, mv it over:
 	if ! test -f $1; then
 		mv $1.new $1
 	elif diff --brief $1 $1.new; then	# toss the redundant copy
 		rm $1.new
 	elif ! test -s $1.new; then
 		rm $1.new   # do not keep empty initial files (e.g. log)
 	fi
 	# Otherwise, we leave the .new copy for the admin to consider...
}

function free_user_id {
 	# Find a free user-ID >= 100 (should be < 1000 so it's not a normal user)
 	local FREE_USER_ID=100
 	while grep --quiet "^.*:.*:$FREE_USER_ID:.*:.*:.*:" /etc/passwd; do
 		let FREE_USER_ID++
 	done
 	echo $FREE_USER_ID
}

# Edit config file with currently used user/group
# (it's not worth leaving a .new file over)
if test -f etc/tor/torrc && ! grep --quiet --extended-regexp \
 	'^User[[:blank:]]+tor_d[[:blank:]]*$' etc/tor/torrc
then
 	echo "Changing user in /etc/tor/torrc to [1mtor_d[0m."
 	TMPFILE=$(mktemp /tmp/tor.XXXXXX)
 	sed '	/^User[[:blank:]]/	s/[[:blank:]].*/ tor_d/
 		/^Group[[:blank:]]/	s/[[:blank:]].*/ daemon/
 		' etc/tor/torrc > $TMPFILE
 	cat $TMPFILE > etc/tor/torrc
 	rm $TMPFILE
fi

# Edit logrotate.d file with currently used user/group
# (it's not worth leaving a .new file over)
if test -f etc/logrotate.d/tor && ! grep --quiet --extended-regexp \
 	'^[[:blank:]]*create[[:blank:]]+[0-7][0-7][0-7][0-7]?[[:blank:]]+tor_d[[:blank:]]+daemon[[:blank:]]*$' \
 	etc/logrotate.d/tor
then
 	echo "Changing user in /etc/logrotate.d/tor to [1mtor_d[0m."
 	TMPFILE=$(mktemp /tmp/tor.XXXXXX)
 	sed '	/^[[:blank:]]*create/	s/\([0-7][[:blank:]]\).*/\1tor_d daemon/
 		' etc/logrotate.d/tor > $TMPFILE
 	cat $TMPFILE > etc/logrotate.d/tor
 	rm $TMPFILE
fi

# Set up group if needed
grep --quiet '^daemon:' etc/group || groupadd daemon

# Set up user: add it if it doesn't exist, update it if it already does.
# In any case, make sure it has or gets a user ID between 100 and 999.
# If username changed, determine old username so we can change it (look
# for entry with homedir /var/{log,run,lib}/tor).
echo "Setting up unprivileged user [1mtor_d[0m to run the daemon."
if OLD_ENTRY=$(grep --max-count=1 '^tor_d:' /etc/passwd)		\
 	|| OLD_ENTRY=$(grep --max-count=1				\
 		':/var/[lr][oui][gnb]/tor:[a-z/]*$' /etc/passwd)
then
 	# Modify existing user
 	OLD_USER=$(echo $OLD_ENTRY | cut --fields=1 --delimiter=':')
 	USER_ID=$(echo $OLD_ENTRY | cut --fields=3 --delimiter=':')
 	test $USER_ID -ge 1000 && USER_ID=$(free_user_id)
 	usermod -l tor_d -g daemon -d /var/run/tor -u $USER_ID	\
 		-s /bin/false -c "Tor Internet Anonymizer" $OLD_USER
else
 	# Add new user
 	useradd -g daemon -d /var/run/tor -u $(free_user_id)	\
 		-s /bin/false -c "Tor Internet Anonymizer" tor_d
fi

# Set up permissions
chown --recursive tor_d:daemon var/log/tor var/lib/tor var/run/tor

# Tell user how to make us start on boot
echo "[32mIn order to have Tor start at bootup time, add the following line to"
echo "/etc/rd.d/rc.local: [1;31mtest -x /etc/rc.d/rc.tor && . /etc/rc.d/rc.tor start[0m"

# Update configuration files
{
	upd_conf etc/logrotate.d/tor
	upd_conf etc/rc.d/rc.tor
	upd_conf etc/tor/tor-tsocks.conf
	upd_conf etc/tor/torrc
} | sed 's/^/[1mConfig alert: [0m/'

# Initialize log files if not already present
upd_conf var/log/tor/notices.log 2>&1 > /dev/null

