#!/bin/sh

# Settings for WLAN interface:
INTERFACE="eth1"                        
IPADDRESS="192.168.1.205"
MODULE="ga_linuxdrv_211_imx_sdio"
WLAN_SSID="can_net"
WLAN_MODE="ad-hoc"

# Variables for start and wait loop
LOOP_RESULT="running"
LOOP_COUNT=0
LOOP_LIMIT=100
DEBUG="off"
ETH0="disable"

echo Start WLAN script "S91adhoc"

#------------------------------------------------------------
# 1. set eth0 down to remove interface from routing table
if [ "$ETH0" = "disable" ]; then
  echo 1. set eth0 down to remove interface from routing table
  ifconfig eth0 down
fi


#------------------------------------------------------------
# 2. load WLAN kernel module and wait for interface
echo 2. load WLAN kernel module "$MODULE=" and wait for interface "$INTERFACE"

modprobe $MODULE

### wait for eth1 interface
while [ "$LOOP_RESULT" = "running" ]; do
	if [ -d /sys/class/net/$INTERFACE ]; then
		LOOP_RESULT="$INTERFACE found"
	fi
	usleep 100000
 
	let LOOP_COUNT=$LOOP_COUNT+1
	if [ "$LOOP_COUNT" = "$LOOP_LIMIT" ]; then
		LOOP_RESULT="timeout $INTERFACE not found"
	fi
done

if [ "$DEBUG" != "off" ]; then
	echo "Loop Result: "$LOOP_RESULT" --- Loop Count: "$LOOP_COUNT
fi




#------------------------------------------------------------
# 3. configure eth1
echo 3. configure "$INTERFACE" = "$IPADDRESS"

ifconfig $INTERFACE $IPADDRESS


#------------------------------------------------------------
# 4. set WLAN "eth1" in adhoc mode and 
#    start WLAN interface with ssid "can_net"
#    important: first mode then essid
echo 4. set WLAN "$INTERFACE" in mode "$WLAN_MODE" and 
echo    start WLAN interface with ssid "$SSID"

iwconfig $INTERFACE mode $WLAN_MODE essid $WLAN_SSID

#end fo script