# NTLM proxy set-up script
# (C) 2005 by Garth Dahlstrom (ironstorm@users.sf.net) 
# 
# Sets up a Debian linux box to talk to the internet via an NTLM proxy 
# by using ntlmaps
#
# This script is meant to be invoked by using `. ~/ntlm-proxy-setup` at a 
# shell prompt as it sets and exported the http_proxy environment variable
# 
echo
echo Reconfiguring NTLM-APS ...
echo 
## NTLM APS is installed but not configured.
if [ "`sudo grep USER /etc/ntlmaps/server.cfg|cut -d':' -f2 | sed 's- --g'`" == "username_to_use" ]; then
  sudo dpkg-reconfigure ntlmaps
fi

unset HTTP_PROXY
HTTP_PROXY=$(whiptail --clear --title 'Set http_proxy Environment Variable' --radiolist 'Set or Clear http_proxy variable' 0 76 0 'Set' "http_proxy to point to NTLM-APS at http://localhost:$(sudo grep LISTEN_PORT /etc/ntlmaps/server.cfg | cut -d':' -f2 | sed s-\ --g) " on 'Clear' 'http_proxy' off 2>&1 </dev/tty >/dev/tty)
echo
if [ $? -eq 0 ]; then 
  if [ "$HTTP_PROXY" == "Set" ]; then 
    echo Setting http_proxy to http://localhost:$(sudo grep LISTEN_PORT /etc/ntlmaps/server.cfg|cut -d':' -f2 | sed s-\ --g) ...
    export http_proxy=http://localhost:$(sudo grep LISTEN_PORT /etc/ntlmaps/server.cfg|cut -d':' -f2 | sed s-\ --g)
  elif [ "$HTTP_PROXY" == "Clear" ]; then
    echo Clearing http_proxy ...
    unset http_proxy
  else
    echo "Abort. Not changing http_proxy (currently $http_proxy)"
  fi
fi
unset HTTP_PROXY 
