#!/bin/bash
#
# Activates XI626 wlan0 interface to connect to CZFree.Net
#
# Source function library.
. /etc/init.d/functions

case "$1" in
  start)
    if [ `/sbin/lsmod | grep -c prism` -gt 1 ]
        then
	    echo -n "Removing wlan driver..."
	    /sbin/ifconfig wlan0 down
	    /sbin/rmmod prism2_pci
	    /sbin/rmmod p80211
	    action "" /bin/true
    fi

    if [ `/sbin/lsmod | grep -c hostap` -gt 1 ]
        then
	    echo -n "Removing hostap driver..."
	    /sbin/ifconfig wlan0 down
	    /sbin/rmmod hostap_pci
	    /sbin/rmmod hostap
	    /sbin/rmmod hostap_crypt
	    action "" /bin/true
    fi
	echo -n "Setting up wlan0..."
	/sbin/modprobe hostap_pci
	sleep 0.2
	/usr/local/sbin/iwconfig wlan0 mode managed
# zde vase IP
	/sbin/ifconfig wlan0 inet 10.18.1.X netmask 255.255.255.128
	sleep 0.4
# zde vyplnte essid vaseho AP
	/usr/local/sbin/iwconfig wlan0 essid "winel.CZFree.Net"
	sleep 1
	/usr/local/sbin/iwconfig wlan0 retry 20
	/usr/local/sbin/iwconfig wlan0 txpower fixed # ALC fixed
	sleep 1
	/usr/local/sbin/iwpriv wlan0 writemif 62 230
	/usr/local/sbin/iwconfig wlan0 rate 2M fixed
	/usr/local/sbin/iwconfig wlan0 sens 3
# nastaveni routovaci tabulky - neni podminkou a pokud bude pouzit DHCP tak zrusit
	/sbin/route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.18.1.1
	/sbin/route del default
	/sbin/route add default gw 10.18.1.1
# a nastaveni nameserveru - stejne jako route - s DHCP zrusit	
	cp /etc/resolv.conf /etc/resolv.conf.tmp
	echo "nameserver 10.18.1.129" > /etc/resolv.conf
	cat /etc/resolv.conf.tmp >> /etc/resolv.conf
	
	action "" /bin/true
    ;;
  stop)
    if [ `/sbin/lsmod | grep -c prism` -gt 1 ]
        then
	    echo -n "Removing wlan driver..."
	    /sbin/ifconfig wlan0 down
	    /sbin/rmmod prism2_pci
	    /sbin/rmmod p80211
    fi

    if [ `/sbin/lsmod | grep -c hostap` -gt 1 ]
        then
	    echo -n "Removing hostap driver..."
	    /sbin/ifconfig wlan0 down
	    /sbin/rmmod hostap_pci
	    /sbin/rmmod hostap
	    /sbin/rmmod hostap_crypt
    fi
    action "" /bin/true
    ;;
  *)
    echo "Usage: wlan0 {start|stop}"
    exit 1
esac


exit 0