#!/bin/bash
#
# Startup script to transparantly socksify your system using Dante.
#
# chkconfig: - 89 11
# description: Dsocksify is a means to socksify your system transparantly \
#	(using Dante) on start-up after the network is up and running.
#
# processname: dsocksify
#
# config: /etc/socks.conf
# config: /etc/ld.so.preload

# Source function library.
source /etc/rc.d/init.d/functions

disable() {
	echo $"Warning: your installation is faulty."
	stop
	exit 1
}

[ ! -r /etc/socks.conf ] || disable
[ ! -x /lib/libdsocks.so.0 ] || disable

start() {
	echo -n $"Socksifying system: "
	grep "/lib/libdsocks.so" /etc/ld.so.preload &>/dev/null
	ret=$?
	if [ ! -f /etc/ld.so.preload -o $ret -ne 0 ]; then
		echo $"/lib/libdsocks.so.0" >>/etc/ld.so.preload
		success $"dsocksify startup"
	elif [ $ret -eq 0 ]; then
		passed $"dsocksify startup"
	else
		failure $"dsocksify startup"
	fi
	echo
}

stop() {
	echo -n $"Unsocksifying system: "
	grep "/lib/libdsocks.so" /etc/ld.so.preload &>/dev/null
	if [ $? -eq 0 ]; then
		cat /etc/ld.so.preload | grep -v "/lib/libdsocks.so.0" >/etc/ld.so.preload.cache
		mv -f /etc/ld.so.preload.cache /etc/ld.so.preload
		success $"dsocksify shutdown"
	else
		failure $"dsocksify shutdown"
	fi
	if [ ! -s /etc/ld.so.preload ]; then
		rm -f /etc/ld.so.preload
	fi
	echo
}

restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	RETVAL=1
esac

exit $RETVAL
