#!/bin/bash

prepare() {
	if [ $1 = "v0.1" ]; then
		#Get the OpenWrt Core Source for Firmware version 0.1
		svn checkout --revision=25628 svn://svn.openwrt.org/openwrt/branches/backfire/ ./build_dir

		svn export http://svn.freifunk-ol.de/firmware/build_environment/build_patches/v0.1/feeds.conf ./build_dir/feeds.conf

		./build_dir/scripts/feeds update

#		 This does not work!
#                svn update --revision=25253 ./build_dir/feeds/packages/net/batman-adv
#		 svn update --revision=25253 ./build_dir/feeds/packages/net/batman-advanced

		./build_dir/scripts/feeds install -a
	fi
}

configure_build() {
	#Move the build configuration for v0.1 to the build_dir
	if [ $1 = "v0.1" ]; then
		#Move the build configuration for v0.1 to the build_dir
		if [ $2 = "dir300" ]; then
			svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/build_configuration/v0.1/Atheros_AR231x_AR5312/.config ./build_dir/.config
		fi
		
		if [ $2 = "fonera" ]; then
			svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/build_configuration/v0.1/Atheros_AR231x_AR5312/.config ./build_dir/.config
		fi
		
		if [ $2 = "wrt54g_ap" ]; then
			svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/build_configuration/v0.1/Broadcom_BCM947xx_953xx_ap/.config ./build_dir/.config
		fi
		
		if [ $2 = "wrt54g_adhoc" ]; then
			svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/build_configuration/v0.1/Broadcom_BCM947xx_953xx_adhoc/.config ./build_dir/.config
		fi
	fi
}

build() {
	cd ./build_dir
	make world
	cd ../
}

configure() {
	if [ $1 = "v0.1" ]; then
		if [ $2 = "dir300" ]; then
			./build_dir/staging_dir/host/bin/unsquashfs4 -d ./tmp ./build_dir/bin/atheros/openwrt-atheros-root.squashfs
			svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/router_configuration/ ./tmp2
			rm ./tmp/etc/TZ
			cp -R ./tmp2/* ./tmp/
			rm -Rf ./tmp2
			mv ./tmp/etc/config_$2/* ./tmp/etc/config/
			rm -Rf ./tmp/etc/config_*
			./build_dir/staging_dir/host/bin/mksquashfs4 ./tmp ./root.squashfs -comp lzma
			
			dd if=./root.squashfs of=./tmpfile.1 bs=64k conv=sync
			echo -ne '\xde\xad\xc0\xde' >> ./tmpfile.1
			dd of=./root.squashfs if=./tmpfile.1 bs=64k conv=sync
			echo -ne '\xde\xad\xc0\xde' >> ./root.squashfs
			dd if=./root.squashfs of=./openwrt-$2-root.squashfs bs=128k conv=sync
			/bin/rm -rf ./root.squashfs ./tmpfile.1
			rm -Rf ./tmp
			cp ./build_dir/bin/atheros/openwrt-atheros-vmlinux.lzma ./openwrt-$2-vmlinux.lzma
		fi
		
		if [ $2 = "fonera" ]; then
                        ./build_dir/staging_dir/host/bin/unsquashfs4 -d ./tmp ./build_dir/bin/atheros/openwrt-atheros-root.squashfs
                        svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/router_configuration/ ./tmp2
                        rm ./tmp/etc/TZ
                        cp -R ./tmp2/* ./tmp/
			rm -Rf ./tmp2
                        mv ./tmp/etc/config_$2/* ./tmp/etc/config/
                        rm -Rf ./tmp/etc/config_*
                        ./build_dir/staging_dir/host/bin/mksquashfs4 ./tmp ./root.squashfs -comp lzma
			
			dd if=./root.squashfs of=./tmpfile.1 bs=64k conv=sync
			echo -ne '\xde\xad\xc0\xde' >> ./tmpfile.1
			dd of=./root.squashfs if=./tmpfile.1 bs=64k conv=sync
			echo -ne '\xde\xad\xc0\xde' >> ./root.squashfs
			dd if=./root.squashfs of=./openwrt-$2-root.squashfs bs=128k conv=sync
			/bin/rm -rf ./root.squashfs ./tmpfile.1
			rm -Rf ./tmp
			cp ./build_dir/bin/atheros/openwrt-atheros-vmlinux.lzma ./openwrt-$2-vmlinux.lzma
		fi
		
		if [ $2 = "wrt54g_ap" ]; then
			echo "Nothing implemented here yet"
		fi
		
		if [ $2 = "wrt54g_adhoc" ]; then
			echo "Nothing implemented here yet"
		fi
	fi
}

flash() {
	svn export --revision=271 http://svn.freifunk-ol.de/firmware/build_environment/flash_tools/ ./flash_tools

	if [ $1 = "dir300" ]; then
		chmod +x ./flash_tools/dir300-flash/dir300-flash.sh
		./flash_tools/dir300-flash/dir300-flash.sh $2 ./openwrt-$1-vmlinux.lzma ./openwrt-$1-root.squashfs
	fi

	if [ $1 = "fonera" ]; then
		chmod +x ./flash_tools/fonera-flash/ap51-flash-1.0-42
		ln -s /usr/lib/libpcap.so.1.1.1 /usr/lib/libpcap.so.0.8
		./flash_tools/fonera-flash/ap51-flash-1.0-42 $2 ./openwrt-$1-root.squashfs ./openwrt-$1-vmlinux.lzma
	fi

	if [ $1 = "wrt54g_ap" ]; then
		echo "Nothing implemented here yet"
	fi

	if [ $1 = "wrt54g_adhoc" ]; then
		echo "Nothing implemented here yet"
	fi
}

if [ "$1" = "help" ] || [ "$1" = "" ]; then
	echo "This is the Build Environment Script of the Freifunk Community Oldenburg."
	echo "Usage: build_script.sh [option]"
	echo " "
	echo "    prepare"
	echo "    build"
	echo "    configure"
	echo "    flash"
	echo " "
	echo "If you need help to one of these options just type build_script.sh [option] help"
fi

if [ "$1" = "prepare" ]; then
	if [ "$2" = "help" ] || [ "$2" = "" ]; then
		echo "This option fetches the sources for the images and configurates the build so that it can be compiled"
		echo "Apply the version (e.g. \"v0.1\") as first and the router type (e.g. \"dir300\") as second parameter"
	else
		prepare "$2"
		configure_build "$2" "$3"
	fi
fi

if [ "$1" = "build" ]; then
	if [ "$2" = "help" ]; then
		echo "This option compiles the firmware"
	else
		build
	fi
fi

if [ "$1" = "configure" ]; then
	if [ "$2" = "help" ] || [ "$2" = "" ]; then
		echo "This option inserts the configuration of the freifunk community oldenburg into the firmware after it has been compiled"
		echo "Apply the version (e.g. \"v0.1\") as first and the router type (e.g. \"dir300\") as second parameter"
	else
		configure "$2" "$3"
	fi
fi

if [ "$1" = "flash" ]; then
	if [ "$2" = "help" ] || [ "$2" = "" ]; then
		echo "This option flashes the router."
		echo "Apply router type (e.g. \"dir300\") as first parameter and the interface (e.g. \"eth0\" as second parameter"
	else
		flash "$2" "$3"
	fi
fi

