#!/bin/sh

# rename this script (the 00 dummy starting order)
# and place it in the right Linux directory such
# that it start when your system boots.
# e.g. on Linksys: /etc/init.d/S42lunar

INS=/sbin/insmod
RMM=/sbin/rmmod

# set dhcp client + arguments to be used:
# "dhclient3" (Linux) or "udhcpc -i" (OpenWRT)
DHCPclient="dhclient3"

# Set LunarIP="" if you do not want the
# lunar interface to be configured with ifconfig,
# or LunarIP="dhcp" to run dhcp client (see
# lnx/README file for details).
# Set ConfigWIFI to "yes" if you want the script to
# run: iwconfig BASEDEVNAME mode ad-hoc essid $SSID
# Note: BASEDEVNAME (see below) MUST be set

LunarIP="dhcp"
LunarMASK=255.255.255.0
LunarGW=192.168.42.1
ConfigWIFI="no"
SSID="lunar"

# LUNAR module options
# BASEDEVNAME: the interface used by LUNAR
# DEBUGLEVEL and SAPFDEBUG: set to 10 for 
# maximum debug information (in /var/log/messages)
# MACKILL: list of MAC address to ignore (used to
# emulate multihop network)

LUNARMODULEDIR="lnx"
BASEDEVNAME="eth0"
DEBUGLEVEL="10"
SAPFDEBUG=""
MACKILL=""

# DON'T CHANGE BELOW THIS LINE !

# create argument passed to lunar module

LUNAROPTIONS=""

load_lunar()
{
  if [ "$BASEDEVNAME" != "" ]; then
    LUNAROPTIONS="${LUNAROPTIONS} basedevname=$BASEDEVNAME"
  fi
  
  if [ "$DEBUGLEVEL" != "" ]; then
    LUNAROPTIONS="${LUNAROPTIONS} debuglevel=$DEBUGLEVEL"
  fi
  
  if [ "$SAPFDEBUG" != "" ]; then
    LUNAROPTIONS="${LUNAROPTIONS} sapfdebug=$SAPFDEBUG"
  fi
  
  if [ "$MACKILL" != "" ]; then
    LUNAROPTIONS="${LUNAROPTIONS} mackill=$MACKILL"
  fi
  
  # echo -e $LUNAROPTIONS
  $INS $LUNARMODULE $LUNAROPTIONS
  
  if [ "$LunarIP" = "dhcp" ]; then
    $DHCPclient lunar
  else
    if [ "$LunarIP" != "" ]; then
      ifconfig lunar $LunarIP netmask $LunarMASK
      ifconfig lunar up
      route add -net default gw $LunarGW
    fi
  fi
  
  if [ "$ConfigWIFI" = "yes" ]; then
    iwconfig $BASEDEVNAME mode ad-hoc essid $SSID
  fi
  
  return
}  

echo -e "\n LUNAR - Lightweight Underlay Network Ad hoc Routing\n"

if [ "$USER" != "root" ]; then
    echo -e "You must be root to run this script\n"
    exit 0
  fi

if [ $# -ne 1 ]; then
  echo -e "Usage: $0 start|stop|restart\n"
  exit 0
fi

### find module ###

if [ "$KERNELVERSION" = "" ]; then
    KERNELVERSION=`uname -r`
  fi

case "$KERNELVERSION" in
  2.4.*)
    LUNARMODULE="$LUNARMODULEDIR/lunar.o";;
  2.6.*)
    LUNARMODULE="$LUNARMODULEDIR/lunar.ko";;
esac

case "$1" in
  start) 

  if ! [ -e $LUNARMODULE ]; then
    echo -e "--> Cannot find LUNAR module ($LUNARMODULE) !\n"
    exit 0
  fi

  for module in `lsmod |grep lunar |cut -d " " -f1`
  do
    if [ "$module" = "lunar" ]; then
      echo -e "--> Lunar module already loaded !\n"
      exit 0
    fi
  done

  echo -e "--> Loading LUNARng module ($LUNARMODULE)\n"
  load_lunar
  ;;
  
  stop|reload)
  
  for module in `lsmod | cut -d " " -f1`
  do
    if [ "$module" = "lunar" ]; then
      echo -e "--> Unloading lunar module\n"
      ifconfig lunar down
      $RMM lunar
    fi
  done
  
  if [ "$1" = "stop" ]; then
        exit 0
  fi
      
  if ! [ -e $LUNARMODULE ]; then
    echo -e "--> Cannot find LUNAR module ($LUNARMODULE) !\n"
    exit 0
  fi
  
  echo -e "--> Reloading LUNARng module ($LUNARMODULE)\n"
  load_lunar
  ;;
esac
  

  
