#!/bin/sh

# This script generates openwrt binaries. It will need some space
# to extract the files from root.tgz in the /tmp dir. The script
# will output a new bin or trx file on stdout. 7 variants can be gene-
# rated (g, gs, gs40, g3g, a, moto, se505 and trx). You can supply an IP
# address as well as other net params to be included as fixed defaults for
# first start or "reset to defaults". A firmware with an IP default
# will be marked in the version string on the main router web page.
# If an IP is supplied, the defaults are (add more params to change):

# DEF_MSK=255.0.0.0
# DEF_SSID=olsr.freifunk.net
# DEF_CHAN=10   (In the early days, some devices insist on ch 10 for ad-hoc)
# DEF_FRAG=512  (To optimize for olsr bcast receive on havy loaded links)
# DEF_RTS=250   (For bad link it is better to announce "I will send now")
# DEF_ANTDIV=1  (For Ad-Hoc it is better to use the wrong antenna than both)
# DEF_HOSTNAME= (The hostname can be predetermined also)

# If you add files to root.tgz, please supply the "gen-openwrt -v XXX"
# as the first parameter. This will pace the XXX on the main page, in 
# order to make it possible to identify a firmware with addons.

# Parameter for Versions:
# g = Firmware for wrt54g 1.0, 1.1, 2.0, 2.2, 3.0, 3.1 and 4.0
# gs = Firmware for wrt54gs 1.0 und 1.1 (for gs 3.0 use gs40, unverified)
# gs40 = Firmware for wrt54gs 4.0 only
# g3g = Firmware for wrt54g3g (device with UMTS)
# a = Firmware for Allnet all0277
# moto = Firmware for Motorola WR850G
# se505 = Firmware for Siemens se505
# trx = Firmware for wap54g/wl500 or web update via Freifunk-Firmware

WL_WEP=
if [ "-w" = "$1" ];then
  WL_WEP=1
  shift
fi

ADDVER=
if [ "-v" = "$1" ];then
  ADDVER=$2
  shift
  shift
fi
if [ -n "$2" ]; then
  if [ -n "$ADDVER" ];then
    ADDVER="$ADDVER/$2"
  else
    ADDVER=$2
  fi
fi

TMPDIR=/tmp
MYPATH=${0%/*}

# Normal compressed kernel
#KERNEL=$MYPATH/vmlinuz
#MKSQASHFS=$MYPATH/mksquashfs

# LZMA compressed kernel
KERNEL="$MYPATH/loader.gz $MYPATH/vmlinux.lzma"
MKSQASHFS=$MYPATH/mksquashfs-lzma

case "$1" in
  g|gs|gs40|g3g|a|moto|se505|trx)
    if [ -f $MYPATH/root.tgz ]; then
      if [ -f $MYPATH/vmlinux.lzma ]; then
        mkdir $TMPDIR/openwrt-root.$$
        tar -C $TMPDIR/openwrt-root.$$ -xzf $MYPATH/root.tgz
        if [ -f $TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram ]; then
          # Otherwise this is the minimal kit, no freifunk addons then
          if [ -n "$2" ]; then
            # Caller has issued default IP address
            cp $TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram $TMPDIR/openwrt-in.$$

            if [ -n "$9" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                  -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$4/" \
                  -e "s/^\(DEF_SSID\)[^#$]\+/\1=$5/" \
                  -e "s/^\(DEF_FRAG\)[^#$]\+/\1=$6/" \
                  -e "s/^\(DEF_RTS\)[^#$]\+/\1=$7/" \
                  -e "s/^\(DEF_ANTDIV\)[^#$]\+/\1=$8/" \
                  -e "s/^\(DEF_HOSTNAME\)[^#$]\+/\1=\"$9\"/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            elif [ -n "$8" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                  -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$4/" \
                  -e "s/^\(DEF_SSID\)[^#$]\+/\1=$5/" \
                  -e "s/^\(DEF_FRAG\)[^#$]\+/\1=$6/" \
                  -e "s/^\(DEF_RTS\)[^#$]\+/\1=$7/" \
                  -e "s/^\(DEF_ANTDIV\)[^#$]\+/\1=$8/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            elif [ -n "$7" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                  -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$4/" \
                  -e "s/^\(DEF_SSID\)[^#$]\+/\1=$5/" \
                  -e "s/^\(DEF_FRAG\)[^#$]\+/\1=$6/" \
                  -e "s/^\(DEF_RTS\)[^#$]\+/\1=$7/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            elif [ -n "$6" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                  -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$4/" \
                  -e "s/^\(DEF_SSID\)[^#$]\+/\1=$5/" \
                  -e "s/^\(DEF_FRAG\)[^#$]\+/\1=$6/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            elif [ -n "$5" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                  -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$4/" \
                  -e "s/^\(DEF_SSID\)[^#$]\+/\1=$5/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            elif [ -n "$4" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                  -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$4/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            elif [ -n "$3" ]; then
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                  -e "s/^\(DEF_MSK\)[^#$]\+/\1=$3/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            else
              sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
                $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
            fi
            rm $TMPDIR/openwrt-in.$$

            cp $TMPDIR/openwrt-root.$$/www/cgi-bin-index.html $TMPDIR/openwrt-in.$$
            if [ -n "$ADDVER" ]; then
              sed -e "s/STYLE="firmwarelinks:0\;display:list-item\;"/STYLE="firmwarelinks:0\;display:none\;"/" \
                  -e "s/<!--%KIT_IPADDR%-->/($ADDVER)/" $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/www/cgi-bin-index.html
            else
              sed -e "s/STYLE="firmwarelinks:0\;display:list-item\;"/STYLE="firmwarelinks:0\;display:none\;"/" \
                  -e "s/<!--%KIT_IPADDR%-->//" $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/www/cgi-bin-index.html
            fi
            rm $TMPDIR/openwrt-in.$$
          else
            cp $TMPDIR/openwrt-root.$$/www/cgi-bin-index.html $TMPDIR/openwrt-in.$$
            if [ -n "$ADDVER" ]; then
              sed -e "s/<!--%KIT_IPADDR%-->/($ADDVER)/" $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/www/cgi-bin-index.html
            else
              sed -e "s/<!--%KIT_IPADDR%-->//" $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/www/cgi-bin-index.html
            fi
            rm $TMPDIR/openwrt-in.$$
          fi
        fi

	mkdir -p $TMPDIR/openwrt-root.$$/lib/modules/
        cp -a $MYPATH/2.4.30 $TMPDIR/openwrt-root.$$/lib/modules/
        if [ -n "$WL_WEP" ]; then
          cp -f $MYPATH/wep/wl.o $TMPDIR/openwrt-root.$$/lib/modules/2.4.30/
          cp -f $MYPATH/wep/wifi $TMPDIR/openwrt-root.$$/sbin
          cp -f $MYPATH/wep/libiw*.so $TMPDIR/openwrt-root.$$/usr/lib
        fi

        # The -nopad parameter is required if fix-crc-error.patch is applied to buildroot
        # That patch is an old one published by mtakahar in the openwrt forum in feb 2005
        # Because I have sporadic "attempt to access beyond end of device" errors with
        # trx/bin files if they reach a 0x10000 boundary I disabled the -nopad here.
        $MKSQASHFS $TMPDIR/openwrt-root.$$ $TMPDIR/openwrt-fs.$$ -noappend -root-owned -le>/dev/null

        rm -rf $TMPDIR/openwrt-root.$$
        case "$1" in
          g)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$|$MYPATH/addpattern -4 -g|sed -e "1s,^W54S,W54G,"
          ;;
          gs)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$|$MYPATH/addpattern -4 -g
          ;;
          gs40)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$|$MYPATH/addpattern -4 -g|sed -e "1s,^W54S,W54s,"
          ;;
          g3g)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$|$MYPATH/addpattern -4 -g|sed -e "1s,^W54S,W54F,"
          ;;
          a)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$|$MYPATH/addpattern -4 -g|sed -e "1s,^W54S,W54A,"
          ;;
          se505|moto)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$>$TMPDIR/openwrt-fs.$$.trx
            $MYPATH/freifunk-upload -$1 $TMPDIR/openwrt-fs.$$.trx
            rm -f $TMPDIR/openwrt-fs.$$.trx
          ;;
          trx)
            $MYPATH/trx $KERNEL $TMPDIR/openwrt-fs.$$
          ;;
        esac
        rm -f $TMPDIR/openwrt-fs.$$
      else
        echo "No kernel image $MYPATH/vmlinu*" > /dev/stderr
      fi
    else
      echo "No $MYPATH/root.tgz" > /dev/stderr
    fi
  ;;
  *)
    echo "Usage: $0 [-w] [-v \"My Vers\"] (g|gs|gs40|g3g|a|moto|se505|trx)" > /dev/stderr
    echo "Add defaults: [ip] [netmask] [channel] [ssid] [frag] [rts] [antdiv] [hostname]" > /dev/stderr
    echo "-w will use a wl.o driver with WEP/WPA capability." > /dev/stderr
    echo "-v will patch in a version id on the main web page." > /dev/stderr
  ;;
esac
exit 1
