#! /bin/sh

# Wrapper script for use of the torksocks(8) transparent socksification library
# See the torksocks(1) and torkify(1) manpages.

# Copyright (c) 2004, 2006 Peter Palfrader
# Modified by Jacob Appelbaum <jacob@appelbaum.net> April 16th 2006
# Modified by Robert Hogan <robert@roberthogan.net> March 2007
# May be distributed under the same terms as Tor itself


# Define and ensure we have torksocks
# XXX: what if we don't have which?
TSOCKS="`which torksocks`"
if [ ! -x "$TSOCKS" ]
then
        echo "$0: Can't find torksocks in PATH. Perhaps you haven't installed it?" >&2
        exit 1
fi

# Check for any argument list
if [ "$#" = 0 ]
then
        echo "Usage: $0 <command> [<options>...]" >&2
        exit 1
fi
if [ "$#" = 1 ] && ( [ "$1" = "-h" ] || [ "$1" = "--help" ] )
then
        echo "Usage: $0 <command> [<options>...]"
        exit 0
fi

# Define our torksocks config file
TSOCKS_CONF_FILE="/etc/tork-tsocks.conf"
export TSOCKS_CONF_FILE

# Check that we've got a torksocks config file
if [ -r "$TSOCKS_CONF_FILE" ]
then
	exec torksocks "$@"
	echo "$0: Failed to exec torksocks $@" >&2
	exit 1
else
	echo "$0: Missing torksocks configuration file \"$TSOCKS_CONF_FILE\"." >&2
	exit 1
fi
