#!/bin/sh
#
# Copyright (C) 1995-1999 Silicon Graphics, Inc.
#
# $Id: pmsocks.sh,v 1.1 2000/06/22 06:54:05 markgw Exp $
#
# Dynamically sockify the argument program using tsocks
# from http://www.progsoc.uts.edu.au/~delius/
#
. /etc/pcp.env

prog=`basename $0`

if [ $# -eq 0 -o "X$1" = "X-?" ]
then
    echo "Usage: $prog [path]program [args ...]"
    exit 1

fi

if [ ! -f /etc/tsocks.conf -o ! -f /usr/lib/libtsocks.so ]
then
    echo "$prog: Error \"tsocks\" doesn't seem to be installed."
    echo "*** Get it from http://www.progsoc.uts.edu.au/~delius/"
    exit 1
fi

cmd="$1"
target=`which $cmd 2>/dev/null`

if echo "$target" | grep "$cmd not in" >/dev/null
then
    echo "$prog: Error: $target"
    exit 1
fi

shift
args=""
for arg
do
    args="$args \"$1\""
    shift
done

if [ ! -x "$target" ]
then
    echo "$prog: Error \"$cmd\": Command not found."
    exit 1
fi

LD_PRELOAD=/usr/lib/libtsocks.so
export LD_PRELOAD
eval exec $target $args
