#
# cqure.net AP by Patrik Karlsson 2002-10-01
#

BASEDIR=/home/patrik/cqureap
KERNELCONF=kernel.conf
KERNELDIR=src/linux
KERNELVERSION="2.4.20"
PCMCIADIR=src/pcmcia-cs-3.1.34
SYSLINUX=src/syslinux-2.00/syslinux
FLOPPYDEV=/dev/fd0
LOOPDEV=/dev/loop1
BUSYBOXDIR=src/busybox
DHCPDIR=src/udhcp

all: clean packages floppy

kernelfixup:
	cd src/linux; patch -p1 < ../hostap/kernel-patches/hostap-linux-2.4.19-rc3.patch;cp ../hostap/driver/modules/hostap*.[ch] drivers/net/wireless
	cp kernel.conf src/linux/.config


floppy:

	@echo "Making cqure ap boot floppy"
	@rm -rf floppies/loop_rootdisk_ext2fs.fs
	@dd if=/dev/zero of=floppies/loop_rootdisk_ext2fs.fs bs=1k count=4096 2> /dev/null
	@echo "Creating rootdisk filesystem"
	@/sbin/mke2fs -F -m 0 -N 2000 floppies/loop_rootdisk_ext2fs.fs > /dev/null 2> /dev/null

	@echo "Mounting rootdisk to /mnt/cqureap"
	@mount -o loop -t ext2 floppies/loop_rootdisk_ext2fs.fs /mnt/cqureap

	@echo "Copying rootdisk to /mnt/cqureap"
	@cp -dpR rootfs/* /mnt/cqureap
	@echo "Unmounting rootdisk"
	@umount /mnt/cqureap

	@echo "Deleting compressed rootfs"
	@rm -rf initrd.gz

	@echo "Creating compressed rootfs"
	@dd if=floppies/loop_rootdisk_ext2fs.fs bs=1k 2>/dev/null | gzip -9 > initrd.gz 2> /dev/null

	@echo "Creating bootfloppy image file"
	@dd if=/dev/zero of=floppies/bootfloppy.img bs=1k count=1440 2> /dev/null
	@mkdosfs floppies/bootfloppy.img > /dev/null 2> /dev/null

	@echo "Mounting bootfloppy image file"
	@mount -o loop floppies/bootfloppy.img /mnt/cqureap

	@echo "Copying kernel to bootfloppy"
	@cp vmlinuz /mnt/cqureap/

	@echo "Copying initial root file system to bootfloppy"
	@cp initrd.gz /mnt/cqureap

	@echo "Creating syslinux.cfg"
	@echo "default floppy" > /mnt/cqureap/syslinux.cfg
	@echo "label floppy" >> /mnt/cqureap/syslinux.cfg
	@echo "kernel vmlinuz" >> /mnt/cqureap/syslinux.cfg
	@echo "append initrd=initrd.gz root=/dev/ram0 ramdisk_size=8192" >> /mnt/cqureap/syslinux.cfg

	@echo "Copying pcmcia-cs package to floppy"
	@cp packages/pcmcia.tgz /mnt/cqureap	

	@echo "Copying modules package to floppy"
	@cp packages/modules.tgz /mnt/cqureap	

	@echo "Copying wtools package to floppy"
	@cp packages/wtools.tgz /mnt/cqureap	

	@echo "Copying udhcp package to floppy"
	@cp packages/udhcp.tgz /mnt/cqureap	

	@echo "Copying brctl package to floppy"
	@cp packages/brctl.tgz /mnt/cqureap	

	@echo "Copying iptables package to floppy"
	@cp packages/iptables.tgz /mnt/cqureap

	@echo "Copying hostapd package to floppy"
	@cp packages/hostapd.tgz /mnt/cqureap

	@echo "Copying initial config to floppy"
	@cp cqureap.conf /mnt/cqureap

	@echo "Copying floppyroot to floppy"
	@cp -R floppyroot/* /mnt/cqureap

	@echo "Unmounting boot floppy image"
	@umount /mnt/cqureap

	@echo "Running losetup to setup loopdev"
	@/sbin/losetup $(LOOPDEV) floppies/bootfloppy.img

	@echo "Running syslinux on floppy"
	@$(SYSLINUX) -s $(LOOPDEV)

	@echo "Running losetup to delete loopdev"
	@/sbin/losetup -d $(LOOPDEV)

	@echo "Please insert a floppy to $(FLOPPYDEV) and press enter ..."
	@read

	@echo "Copying boot floppy image to floppy"
	@dd if=floppies/bootfloppy.img of=$(FLOPPYDEV) bs=1k 2> /dev/null

kernel:
	@echo "Starting menuconfig"
	make -C $(KERNELDIR) menuconfig
	make -C $(KERNELDIR) dep; make -C $(KERNELDIR) clean;
	make -C $(KERNELDIR) bzImage
	mv $(KERNELDIR)/arch/i386/boot/bzImage vmlinuz

kernel-modules:
	@echo "Making kernel modules"
	make -C $(KERNELDIR) modules
	make -C $(KERNELDIR) modules_install INSTALL_MOD_PATH=$(BASEDIR)/modules
	/sbin/depmod -a $(KERNELVERSION) -b modules
	mkdir -p rootfs/lib/modules/$(KERNELVERSION)
	mkdir -p rootfs/lib/modules/$(KERNELVERSION)/kernel
	mkdir -p rootfs/lib/modules/$(KERNELVERSION)/kernel/fs
	mkdir -p rootfs/lib/modules/$(KERNELVERSION)/kernel/fs/nls
	cp modules/lib/modules/$(KERNELVERSION)/modules.dep rootfs/lib/modules/$(KERNELVERSION)
	cp -r modules/lib/modules/$(KERNELVERSION)/kernel/fs/nls/*.o rootfs/lib/modules/$(KERNELVERSION)/kernel/fs/nls/

pcmcia-cs:
	@echo "Creating pcmcia-package"
	cd $(PCMCIADIR); ./Configure --kernel=$(BASEDIR)/$(KERNELDIR) --target=$(BASEDIR)/packages/pcmcia --moddir=$(BASEDIR)/modules/$(KERNELVERSION) -n
	make -C $(PCMCIADIR) all

busybox:
	make -C $(BUSYBOXDIR)
	make -C $(BUSYBOXDIR) install
	cp -dpR $(BUSYBOXDIR)/_install/* rootfs/
	rm -rf rootfs/linuxrc

backup:
	tar cvf "../cqureap_`date +%Y%m%d`.tar" -C .. cqureap --exclude 'src/linux*'
	gzip -9 "../cqureap_`date +%Y%m%d`.tar"

packages: package-brctl package-iptables package-wtools package-udhcp package-pcmcia package-modules package-hostapd

package-brctl:
	make -C src/bridge-utils
	strip src/bridge-utils/brctl/brctl
	mkdir packages/brctl
	mkdir packages/brctl/sbin
	cp src/bridge-utils/brctl/brctl packages/brctl/sbin
	tar cvf packages/brctl.tar -C packages/brctl sbin
	gzip -9 packages/brctl.tar
	mv packages/brctl.tar.gz packages/brctl.tgz

package-iptables:
	make -C src/iptables
	mkdir packages/iptables
	mkdir packages/iptables/sbin
	mkdir packages/iptables/usr
	mkdir packages/iptables/usr/local
	mkdir packages/iptables/usr/local/lib
	mkdir packages/iptables/usr/local/lib/iptables
	cp src/iptables/iptables packages/iptables/sbin
	cp src/iptables/iptables-restore packages/iptables/sbin
	cp src/iptables/iptables-save packages/iptables/sbin
	cp src/iptables/extensions/*.so packages/iptables/usr/local/lib/iptables/
	strip packages/iptables/sbin/*
	strip packages/iptables/usr/local/lib/iptables/*
	tar cvf packages/iptables.tar -C packages/iptables sbin usr
	gzip -9 packages/iptables.tar
	mv packages/iptables.tar.gz packages/iptables.tgz

package-wtools:
	make -C src/wireless_tools.25
	mkdir packages/wtools
	mkdir packages/wtools/sbin
	cp src/wireless_tools.25/iwconfig packages/wtools/sbin
	cp src/wireless_tools.25/iwevent packages/wtools/sbin
	cp src/wireless_tools.25/iwgetid packages/wtools/sbin
	cp src/wireless_tools.25/iwlist packages/wtools/sbin
	cp src/wireless_tools.25/iwpriv packages/wtools/sbin
	cp src/wireless_tools.25/iwspy packages/wtools/sbin
	strip packages/wtools/sbin/*
	tar cvf packages/wtools.tar -C packages/wtools sbin
	gzip -9 packages/wtools.tar
	mv packages/wtools.tar.gz packages/wtools.tgz

package-udhcp:
	make -C src/udhcp
	mkdir packages/udhcp
	mkdir packages/udhcp/etc
	mkdir packages/udhcp/etc/udhcpc
	mkdir packages/udhcp/sbin
	mkdir packages/udhcp/usr
	mkdir packages/udhcp/usr/share
	mkdir packages/udhcp/usr/share/udhcpc
	cp src/udhcp/udhcpd packages/udhcp/sbin/
	ln -s udhcpd packages/udhcp/sbin/udhcpc
	strip packages/udhcp/sbin/udhcpd
	ln -s ../resolv.conf packages/udhcp/etc/udhcpc/resolv.conf
	cp src/udhcp/samples/sample.bound packages/udhcp/usr/share/udhcpc/default.bound
	cp src/udhcp/samples/sample.deconfig packages/udhcp/usr/share/udhcpc/default.deconfig
	cp src/udhcp/samples/sample.renew packages/udhcp/usr/share/udhcpc/default.renew
	cp src/udhcp/samples/sample.script packages/udhcp/usr/share/udhcpc/default.script
	tar cvf packages/udhcp.tar -C packages/udhcp sbin etc usr
	gzip -9 packages/udhcp.tar
	mv packages/udhcp.tar.gz packages/udhcp.tgz

package-pcmcia:	pcmcia-cs
	make -C $(PCMCIADIR) install
	cp patches/pcmcia_config_file packages/pcmcia/etc/pcmcia/config
	rm -f packages/pcmcia/sbin/ftl_*
	rm -f packages/pcmcia/sbin/dump_cis
	rm -f packages/pcmcia/sbin/probe
	rm -f packages/pcmcia/sbin/scsi_info
	rm -f packages/pcmcia/sbin/pack_cis
	rm -f packages/pcmcia/sbin/pcinitrd
	strip packages/pcmcia/sbin/*
	tar cvf packages/pcmcia.tar -C packages/pcmcia/ sbin etc var --exclude etc/pcmcia/cis --exclude etc/pcmcia
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/config
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/config.opts
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/network
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/network.opts
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/wireless
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/wireless.opts
	tar rvf packages/pcmcia.tar -C packages/pcmcia/ etc/pcmcia/shared

	gzip -9 packages/pcmcia.tar
	mv packages/pcmcia.tar.gz packages/pcmcia.tgz

package-modules: kernel-modules
	tar cvf packages/modules.tar -C modules lib
	gzip -9 packages/modules.tar
	mv packages/modules.tar.gz packages/modules.tgz

package-hostapd:
	make -C src/hostap/hostapd
	strip -s src/hostap/hostapd/hostapd
	mkdir -p packages/hostapd/usr/sbin
	cp src/hostap/hostapd/hostapd packages/hostapd/usr/sbin
	cp src/hostap/utils/prism2_param packages/hostapd/usr/sbin
	tar cvf packages/hostapd.tar -C packages/hostapd usr
	gzip -9 packages/hostapd.tar
	mv packages/hostapd.tar.gz packages/hostapd.tgz

realclean: clean
	make -C src/busybox clean
	make -C src/iptables clean
	make -C src/udhcp clean
	make -C src/wireless_tools.25 clean
	make -C src/pcmcia-cs-3.1.34 clean

clean:
	rm -rf floppies/*
	rm -rf modules/*
	rm -rf packages/*
	rm -rf initrd.gz
	rm -rf *~
