#!/bin/sh

# If you add files to root.tgz, please supply the "gen-openwrt -v XXX"
# to mark your additions/changes with a version identifier.
#
# g = Firmware for Wrt54g 1.0, 1.1, 2.0, 2.2, 3.0, 3.1, 4.0 and Wrt54gl
# 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 SE-505
# trx = Firmware for wap54g/wl500 or update via Freifunk Web-UI

mask=255.0.0.0
ssid=olsr.freifunk.net
bssid=
chan=10
rts=250
frag=
ant=0
name=

usage() {
	cat >&2 <<EOF
Usage: $0 [Options]... (g|gs|gs40|g3g|a|moto|se505|trx) [ip] > outfile.ext

Options:
  -m netmask	Specify netmask (default: $mask)
  -s ssid	Specify ESSID (default: $ssid)
  -b bssid	Specify BSSID (default: ${bssid:-random join})
  -c channel	Specify channel (default: $chan)
  -f frag	Specify fragmentation (default: ${frag:-off})
  -r rts	Specify RTS (default: ${rts:-off})
  -a antdiv	Specify antenna (${ant:-diversity})
  -n name	Specify node name (use [a-z0-9], minus ok, no dot nor spc)
  -v version	Specify version identifier
  -w		Generate WEP/WPA capable firmware (needs > 2Mb Flash)

Exampl:	$0 trx > code.trx (generates generic trx for mtd write to flash)
	$0 g 10.0.0.1 > code.bin (generates bin file for wrt54g ver1-4)

Note:	Older devices with PMON (wrt54g-v1) need "code.bin" for TFTP

This script generates freifunk 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. If you supply an IP address, the IP address is configured
on the first start or when doing "reset to defaults". A firmware with an IP 
default will be marked in the version string on the main router web page
and is not downloadable through that web page.
EOF
	exit 1
}

while getopts "m:s:b:c:f:r:a:n:v:w" flag; do
	case $flag in
	m)
		mask=$OPTARG
	;;
	s)
		ssid=$OPTARG
	;;
	b)
		bssid=$OPTARG
	;;
	c)
		chan=$OPTARG
	;;
	f)
		frag=$OPTARG
	;;
	r)
		rts=$OPTARG
	;;
	a)
		ant=$OPTARG
	;;
	n)
		name=$OPTARG
	;;
	v)
		vers=$OPTARG
	;;
	w)
		wep=yes
	;;
	*)
		usage
	;;
	esac
done
shift $(($OPTIND - 1))

if [ "yes" = "$wep" ]; then
	# WEP/WPA version to generate
	$vers="$vers+WEP"
fi

TMPDIR=/tmp
MYPATH=${0%/*}
MKSQASHFS=$MYPATH/mksquashfs-lzma
KERNEL="$MYPATH/loader.gz $MYPATH/vmlinux.lzma"

case $1 in
	g|gs|gs40|g3g|a|moto|se505|trx)
		test -f $MYPATH/root.tgz || ! echo "No root sys $MYPATH/root.tgz">&2 || exit 1
		test -f $MYPATH/vmlinux.lzma || ! echo "No kernel image $MYPATH/vmlinux.lzma">&2 || exit 1
		mkdir $TMPDIR/openwrt-root.$$
		tar -C $TMPDIR/openwrt-root.$$ -xzf $MYPATH/root.tgz
		if [ -f $TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram ] && [ -n "$2" ]; then
			# Caller has issued default IP address
			if [ -n "$vers" ];then
				vers="$vers/$2"
			else
				vers=$2
			fi
			cp $TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram $TMPDIR/openwrt-in.$$
			sed -e "s/^\(DEF_ADR\)[^#$]\+/\1=$2/" \
			    -e "s/^\(DEF_MSK\)[^#$]\+/\1=$mask/" \
			    -e "s/^\(DEF_SSID\)[^#$]\+/\1=$ssid/" \
			    -e "s/^\(DEF_BSSID\)[^#$]\+/\1=$bssid/" \
			    -e "s/^\(DEF_CHAN\)[^#$]\+/\1=$chan/" \
			    -e "s/^\(DEF_FRAG\)[^#$]\+/\1=$frag/" \
			    -e "s/^\(DEF_RTS\)[^#$]\+/\1=$rts/" \
			    -e "s/^\(DEF_ANTDIV\)[^#$]\+/\1=$ant/" \
			    -e "s/^\(DEF_HOSTNAME\)[^#$]\+/\1=\"$name\"/" \
			    $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/etc/init.d/S14ffnvram
			rm $TMPDIR/openwrt-in.$$
			test -f $TMPDIR/openwrt-root.$$/www/cgi-bin-index.html || ! echo "No cgi-bin-index.html">&2 || exit 1
			cp $TMPDIR/openwrt-root.$$/www/cgi-bin-index.html $TMPDIR/openwrt-in.$$
			sed -e "s/STYLE=\"firmwarelinks:0\;display:list-item\;\"/STYLE=\"firmwarelinks:0\;display:none\;\"/" \
			    -e "s/<!--%KIT_IPADDR%-->/($vers)/" $TMPDIR/openwrt-in.$$>$TMPDIR/openwrt-root.$$/www/cgi-bin-index.html
			rm $TMPDIR/openwrt-in.$$
		else
			test -f $TMPDIR/openwrt-root.$$/www/cgi-bin-index.html || ! echo "No cgi-bin-index.html">&2 || exit 1
			cp $TMPDIR/openwrt-root.$$/www/cgi-bin-index.html $TMPDIR/openwrt-in.$$
			if [ -n "$vers" ]; then
				sed -e "s/<!--%KIT_IPADDR%-->/($vers)/" $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

		mkdir -p $TMPDIR/openwrt-root.$$/lib/modules/
		cp -a $MYPATH/2.4.30 $TMPDIR/openwrt-root.$$/lib/modules/
		if [ "yes" = "$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.$$
	;;
	*)
		usage
	;;
esac
