#!/bin/bash

#Change this to match the proxy address (and port) for your school
PROXY=1131proxy
PORT=8080

# Wireless SSID
SSID=iDET

# Proxy Server Mode
# none, manual or auto
MODE=manual

# AutoConfiguration URL, if MODE=auto
AUTOURL=

# true or false
AUTH=true

echo -n "UserName: "
read USERNAME
echo -n "Password: "
read PASSWORD
if [ -n "$PASSWORD" ] ; then
  PASSLINE="password=$PASSWORD"
  HTTPPASS=$PASSWORD
  HTTPUSER=$USERNAME
else
  PASSLINE="password-flags=2"
  HTTPPASS="\"\""
  HTTPUSER="\"\""
fi
cat > /etc/NetworkManager/system-connections/$SSID << ENDOFWIRELESSCONFIG
[connection]
id=$SSID
uuid=`cat /proc/sys/kernel/random/uuid`
type=802-11-wireless

[802-11-wireless]
ssid=$SSID
mode=infrastructure
security=802-11-wireless-security

[802-11-wireless-security]
key-mgmt=wpa-eap

[ipv4]
method=auto

[ipv6]
method=auto

[802-1x]
eap=peap;
identity=$USERNAME
phase2-auth=mschapv2
$PASSLINE
ENDOFWIRELESSCONFIG
chmod 600 /etc/NetworkManager/system-connections/$SSID

if [ "$MODE" = "none" ]; then
  su -c "gconftool-2 --type string --set /system/proxy/mode none" olpc
  su -c "gconftool-2 --type bool --set /system/http_proxy/use_http_proxy false" olpc
else
  su -c "gconftool-2 --type bool --set /system/http_proxy/use_http_proxy true" olpc
  su -c "gconftool-2 --type bool --set /system/http_proxy/use_same_proxy true" olpc
  su -c "gconftool-2 --type bool --set /system/http_proxy/use_authentication $AUTH" olpc
  if [ "$AUTH" = "true" ]; then
    su -c "gconftool-2 --type string --set /system/http_proxy/authentication_user $HTTPUSER" olpc
    su -c "gconftool-2 --type string --set /system/http_proxy/authentication_password $HTTPPASS" olpc
  fi
  su -c "gconftool-2 --type string --set /system/http_proxy/host $PROXY" olpc
  su -c "gconftool-2 --type int --set /system/http_proxy/port $PORT" olpc
  su -c "gconftool-2 --type string --set /system/proxy/mode $MODE" olpc
  if [ "$MODE" = "auto" ]; then
    su -c "gconftool-2 --type string --set /system/proxy/autoconfig_url $AUTOURL" olpc
  fi
  su -c "gconftool-2 --type string --set /system/proxy/secure_host $PROXY" olpc
  su -c "gconftool-2 --type int --set /system/proxy/secure_port $PORT" olpc
  su -c "gconftool-2 --type string --set /system/proxy/ftp_host $PROXY" olpc
  su -c "gconftool-2 --type int --set /system/proxy/ftp_port $PORT" olpc
  su -c "gconftool-2 --type string --set /system/proxy/socks_host $PROXY" olpc
  su -c "gconftool-2 --type int --set /system/proxy/socks_port $PORT" olpc
fi



