#!/bin/sh
# ***************************************************************************
# *                                                                         *
# *                                                                         *
# *   Copyright (C) 2008 by Robert Hogan                                    *
# *   robert@roberthogan.net                                                *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU General Public License for more details.                          *
# *                                                                         *
# *   You should have received a copy of the GNU General Public License     *
# *   along with this program; if not, write to the                         *
# *   Free Software Foundation, Inc.,                                       *
#*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
# ***************************************************************************
# *                                                                         *
# *   This is a modified version of a source file from the Tor project.     *
# *   Original copyright information follows:                               *
# ***************************************************************************
# Wrapper script for use of the torsocks(8) transparent socksification library
#
# There are three forms of usage for this script:
#
# /usr/bin/torsocks program [program arguments...]
#
# This form sets the users LD_PRELOAD environment variable so that torsocks(8)
# will be loaded to socksify the application then executes the specified 
# program (with the provided arguments). The following simple example might 
# be used to telnet to www.foo.org via a torsocks.conf(5) configured socks server:
#
# /usr/bin/torsocks telnet www.foo.org
#
# The second form allows for torsocks(8) to be switched on and off for a
# session (that is, it adds and removes torsocks from the LD_PRELOAD environment
# variable). This form must be _sourced_ into the user's existing session
# (and will only work with bourne shell users):
#
# . /usr/bin/torsocks on
# telnet www.foo.org 
# . /usr/bin/torsocks off
# 
# Or
# 
# source /usr/bin/torsocks on
# telnet www.foo.org
# source /usr/bin/torsocks off
#
# The third form creates a new shell with LD_PRELOAD set and is achieved
# simply by running the script with no arguments 
# 
# /usr/bin/torsocks
#
# When finished the user can simply terminate the shell with 'exit'
# 
# This script is originally from the debian torsocks package by
# Tamas Szerb <toma@rulez.org>
# Modified by Robert Hogan <robert@roberthogan.net> April 16th 2006

if [ $# = 0 ] ; then
   echo "$0: insufficient arguments"
   exit
fi

case "$1" in
  on)
    if [ -z "$LD_PRELOAD" ]
      then
        export LD_PRELOAD="/usr/lib/torsocks/libtorsocks.so"
      else
        echo $LD_PRELOAD | grep -q "/usr/lib/torsocks/libtorsocks\.so" || \
        export LD_PRELOAD="/usr/lib/torsocks/libtorsocks.so $LD_PRELOAD"
    fi
  ;;
  off)
    #replace '/' with '\/' in /usr
    escprefix=`echo '/usr' |sed 's/\\//\\\\\//g'`
    export LD_PRELOAD=`echo -n $LD_PRELOAD | sed "s/$escprefix\/lib\/torsocks\/libtorsocks.so \?//"`
    if [ -z "$LD_PRELOAD" ]
      then
        unset LD_PRELOAD
    fi
  ;;
  show|sh)
    echo "LD_PRELOAD=\"$LD_PRELOAD\""
  ;;
  -h|-?)
      echo "$0: Please see torsocks(1) or read comment at top of $0"
   ;;
  *)
    if [ -z "$LD_PRELOAD" ]
    then
      export LD_PRELOAD="/usr/lib/torsocks/libtorsocks.so"
    else
      echo $LD_PRELOAD | grep -q "/usr/lib/torsocks/libtorsocks\.so" || \
      export LD_PRELOAD="/usr/lib/torsocks/libtorsocks.so $LD_PRELOAD"
    fi

    if [ $# = 0 ]
    then
      ${SHELL:-/bin/sh}
    fi

    if [ $# -gt 0 ]
    then
      exec "$@"
    fi
  ;;
esac

#EOF
