#!/bin/sh
# Script that converts a generic socks configuration file to 
# /etc/tsocks.conf and modifies /etc/ld.so.preload
OUTFILE="/tmp/tsocks.conf.$$"
SOCKSPARAMSFILE=$1
TSOCKSFILE=$2
PRELOADFILE=$3

echo "# Generated by socksparams2tsocks, do not edit" > $OUTFILE

parhandclient getgroup root.SOCKS /tmp/socksparams.env.$$ SCRIPT
. /tmp/socksparams.env.$$
rm /tmp/socksparams.env.$$

echo "server = $root_SOCKS_Server" >> $OUTFILE
echo "server_port = $root_SOCKS_ServerPort" >> $OUTFILE
echo "server_type = $root_SOCKS_ServerType" >> $OUTFILE

# The localnet parameter is a space or comma separated list of several 
# local nets, eg.
# "10.0.0.0/255.0.0.0, 192.168.0.0/255.255.255.0 "
# Each net should have a "local = net/mask" row in tsocks.conf
echo "$local_nets"
for net_and_mask in $root_SOCKS_LocalNetworks 
do
  net_and_mask=`echo "$net_and_mask" |sed -ne 's/\([0-9\.\/]*\),*/\1/p'`;
  echo "local = $net_and_mask" >>$OUTFILE;
done
if [ "$root_SOCKS_ServerType" = "5" ]; then
  if [ "$root_SOCKS_UserName" != "" ]; then 
    if [ "$root_SOCKS_Password" != "" ]; then 
      echo "default_user = $root_SOCKS_UserName" >>$OUTFILE;
      echo "default_pass = $root_SOCKS_Password" >>$OUTFILE;
    fi
  fi
fi
cmp -s $TSOCKSFILE $OUTFILE
if [ "$?" != "0" ]; then
  cp $OUTFILE $TSOCKSFILE
fi
rm $OUTFILE
if [ ! -e $PRELOADFILE ]; then
  echo "#/lib/libtsocks.so" >$PRELOADFILE
fi
if [ "$root_SOCKS_Enabled" = "yes" ]; then
sed -e 's/#\(\/lib\/libtsocks.so\)/\1/' < $PRELOADFILE >/tmp/ld.so.preload.$$
fi
if [ "$root_SOCKS_Enabled" = "no" ]; then
sed -e 's/^\(\/lib\/libtsocks.so\)/#\1/' < $PRELOADFILE >/tmp/ld.so.preload.$$
fi
cmp -s $PRELOADFILE /tmp/ld.so.preload.$$
if [ "$?" != "0" ]; then
  cp /tmp/ld.so.preload.$$ $PRELOADFILE
fi
rm /tmp/ld.so.preload.$$
