#!/bin/sh

echo Content-type: text/html
echo ""

cat << EOM
<HTML>
<HEAD><TITLE>TS-WIFIBOX Simple Web Interface</TITLE>
</HEAD>
<BODY bgcolor="#ffffff" text="#000000">
<h1>TS-WIFIBOX Simple Web Interface</h1>
<p>This is a simple web wrapper to the Linux Wireless Tools using Linux Shell Scripts to demonstrate how these tools can be used.
<br>For complete access to the iwtools, connect to your TS-WIFIBOX via telnet (telnet 192.168.0.50, login as root, no password required) and use any of the iwtools available: <b>iwconfig, iwlist, iwevent, iwgetif, iwspy, iwpriv</b>
<br>Also, the 'wlscan' utility is available to make things easier. Just telnet to your TS-WIFIBOX and type 'wlscan'. <br>Enjoy!
</p>
<h3>Available Operations:</h3>
<b><a href="/cgi-bin/wifibox.sh?i=wlan0&a=info&essid=">Interfaces Info</a> 
| <a href="/cgi-bin/wifibox.sh?i=wlan0&a=scan&essid=">Scan Wireless</a> 
| <a href="#" onclick="javascript:if(document.f1.essid.value)window.location.href='wifibox.sh?i=wlan0&a=conn&essid='+document.f1.essid.value;else alert('Type an ESSID to connect.');">Connect</a> 
| <a href="/cgi-bin/wifibox.sh?i=wlan0&a=disc&essid=">Disconnect</a>  
| <a href="/">Back to TS-WIFIBOX home page</a></b>
<hr>
<p>
EOM

st1=`echo $QUERY_STRING | cut '-d&' -f1`
st2=`echo $QUERY_STRING | cut '-d&' -f2`
st3=`echo $QUERY_STRING | cut '-d&' -f3`
p1=`echo $st1 | cut -d= -f2`
p2=`echo $st2 | cut -d= -f2`
p3=`echo $st3 | cut -d= -f2`


if [ "$p2" = "info" ]; then
  echo "<textarea cols=100 rows=20>"
  /sbin/ifconfig
  echo "$p1 interface information:"
  echo ""
  /sbin/ifconfig $p1
  /sbin/iwconfig $p1
  echo "</textarea>"
elif [ "$p2" = "scan" ]; then
  echo "<form name='f1' id='f1' action=''><textarea cols=100 rows=20>"
  /sbin/ifconfig $p1 up
  /sbin/iwlist $p1 scan
  echo "</textarea>"
  echo "<br><br>Type the name of the desired ESSID as it appears in the list and then click 'Connect' on the top menu."
  echo "<br><input type='text' name='essid' id='essid'></form>";
elif [ "$p2" = "conn" ]; then
  echo "Trying to connect to $p3..."
  ifconfig $p1 down
  /sbin/iwconfig $p1 essid off
  /sbin/iwconfig $p1 commit
  /sbin/iwconfig $p1 essid $p3
  /sbin/iwconfig $p1 commit
  /sbin/ifconfig $p1 up
  /sbin/udhcpc -n -i $p1
  echo "<br>Done!"
elif [ "$p2" = "disc" ]; then
  echo "Disconnecting wireless interface..."
  /sbin/ifconfig $p1 down
  /sbin/iwconfig $p1 essid off
  /sbin/iwconfig $p1 commit
  /sbin/ifconfig wlan0 down
  echo "<br>Done!"
fi


cat << EOM
</p>
</BODY>
</HTML>
EOM

