#!/bin/sh

libtsocks="/usr/pkg/lib/libtsocks.so"

usage() {
	echo "usage:"
	echo "  $0 [program [arguments...]]"
	echo "  . $0 on"
	echo "  . $0 off"
}

tsocks_on() {
	case $LD_PRELOAD in
	*"$libtsocks"*)
		;;
	"")	LD_PRELOAD="$libtsocks"
		;;
	*)	LD_PRELOAD="$LD_PRELOAD $libtsocks"
		;;
	esac
	export LD_PRELOAD
	if [ "LD_PRELOAD" = "DYLD_INSERT_LIBRARIES" ]; then
		export DYLD_FORCE_FLAT_NAMESPACE=1
	fi
}

tsocks_off() {
	LD_PRELOAD=`echo $LD_PRELOAD | sed 's,$libtsocks[ :]?,,'`
	if [ "$LD_PRELOAD" ]; then
		export LD_PRELOAD
	else
		unset LD_PRELOAD
	fi
}

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

case "$1" in
on)
	tsocks_on
	;;
off)
	tsocks_off
	;;

show | sh)
	echo "LD_PRELOAD=\"$LD_PRELOAD\""
	if [ "LD_PRELOAD" = "DYLD_INSERT_LIBRARIES" ]; then
		echo "DYLD_FORCE_FLAT_NAMESPACE=${DYLD_FORCE_FLAT_NAMESPACE}"
	fi
	;;
-h | -?)
	usage
	;;
-*)	usage 1>&2
	exit 1
	;;
*)
	tsocks_on
	exec "$@"
	;;
esac
