###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

VER = ipfire

THISAPP    = flash-image
TARGET     = $(DIR_INFO)/$(THISAPP)

###############################################################################
# Top-level Rules
###############################################################################

install : $(TARGET)

check :

download :

md5 :

###############################################################################
# Installation Details
###############################################################################
IMGinst  := /install/images/$(SNAME)-$(VERSION).1gb-ext2.$(MACHINE)-$(ED)-core$(CORE).img
IMGinsts := /install/images/$(SNAME)-$(VERSION).1gb-ext2-scon.$(MACHINE)-$(ED)-core$(CORE).img
MNThdd   := /install/harddisk
IMGpart  := /install/images/part_area
IMGboot  := /install/images/bootfs
# don't change SIZEboot this without checking Image size
SIZEboot := 20
IMGroot  := /install/images/rootfs
SIZEroot := 500
IMGvar   := /install/images/varfs
SIZEvar  := 440

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	rm -rf $(MNThdd) $(IMGinst) $(IMGpart) $(IMGboot) $(IMGroot) $(IMGvar) && mkdir -p $(MNThdd)

	# Create first 32 sectors of image
	dd bs=1k if=/dev/zero of=$(IMGpart) count=16

	# Create images for filesystems
	dd bs=1K if=/dev/zero of=$(IMGboot) count=20464  # 20MB-16K
	dd bs=1M if=/dev/zero of=$(IMGroot) count=$(SIZEroot)
	dd bs=1M if=/dev/zero of=$(IMGvar) count=$(SIZEvar)

	# Format them
	mkfs.ext2 -F $(IMGboot)
	mkfs.ext2 -F $(IMGroot)
	mkfs.ext2 -F $(IMGvar)

	# Mount Images
	mount -o loop $(IMGroot) $(MNThdd)
	mkdir $(MNThdd)/boot
	mkdir $(MNThdd)/var
	mkdir $(MNThdd)/var/log
	mount -o loop $(IMGboot) $(MNThdd)/boot
	mount -o loop $(IMGvar)  $(MNThdd)/var

	# Install IPFire
	tar -C $(MNThdd)/ -xvjf /install/cdrom/$(SNAME)-$(VERSION).tbz2
	echo "LANGUAGE=en" >> $(MNThdd)/var/ipfire/main/settings
	echo "HOSTNAME=$(SNAME)" >> $(MNThdd)/var/ipfire/main/settings
	echo "THEME=ipfire" >> $(MNThdd)/var/ipfire/main/settings
	touch $(MNThdd)/lib/modules/$(KVER)-ipfire/modules.dep
	mv $(MNThdd)/bin/uname.bak $(MNThdd)/bin/uname
	mkdir $(MNThdd)/proc
	mount --bind /proc $(MNThdd)/proc
	mount --bind /dev  $(MNThdd)/dev
	mount --bind /sys  $(MNThdd)/sys
	chroot $(MNThdd) /usr/bin/perl -e "require '/var/ipfire/lang.pl'; &Lang::BuildCacheLang"
	sed -i -e "s|MOUNT|ro|g" $(MNThdd)/boot/grub/grub.conf
	sed -i -e "s|KVER|$(KVER)|g" $(MNThdd)/boot/grub/grub.conf
	sed -i -e "s|ROOT|/dev/sda3|g" $(MNThdd)/boot/grub/grub.conf
	ln -s grub.conf $(MNThdd)/boot/grub/menu.lst
	sed -i -e "s|DEVICE2|#DEVICE2|g" $(MNThdd)/etc/fstab
	sed -i -e "s|DEVICE|/dev/sda|g" $(MNThdd)/etc/fstab
	sed -i -e "s|FSTYPE|ext2|g" $(MNThdd)/etc/fstab
	cp -f $(MNThdd)/etc/mkinitcpio.conf $(MNThdd)/etc/mkinitcpio.conf.org
	sed -i -e "s| autodetect | |g" $(MNThdd)/etc/mkinitcpio.conf
	chroot $(MNThdd) /sbin/mkinitcpio -g /boot/ipfirerd-$(KVER)-emergency.img -k $(KVER)-ipfire 
	cp -f $(MNThdd)/etc/mkinitcpio.conf.org $(MNThdd)/etc/mkinitcpio.conf
	ln -s ipfirerd-$(KVER)-emergency.img $(MNThdd)/boot/ipfirerd-$(KVER).img

	# Copy grub files manually
	cp -f $(MNThdd)/usr/lib/grub/i386-pc/* $(MNThdd)/boot/grub/
	chroot $(MNThdd) /usr/sbin/grub-set-default 0

	# Create a script for rebuild-initrd and setup
	echo "#!/bin/bash"                                   > $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup
	echo "/bin/rm -f /boot/ipfirerd-$(KVER).img"        >> $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup
	echo "/usr/local/bin/rebuild-initrd"                >> $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup
	echo "/usr/local/sbin/setup /dev/null INSTALL"      >> $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup
	echo "rm -f /etc/rc.d/rcsysinit.d/S75firstsetup \\" >> $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup
	echo "&& /sbin/reboot"                              >> $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup

	chmod +x $(MNThdd)/etc/rc.d/rcsysinit.d/S75firstsetup

	# Unmount
	umount $(MNThdd)/proc
	umount $(MNThdd)/dev
	umount $(MNThdd)/sys
	umount $(MNThdd)/boot
	umount $(MNThdd)/var
	umount $(MNThdd)

	# Cat to an image
	cat $(IMGpart) $(IMGboot) $(IMGroot) $(IMGvar) > $(IMGinst)
	# Write Partition table
	echo -e ",$(SIZEboot),L,*\n,0,0\n,$(SIZEroot),L\n,$(SIZEvar),L\n" \
		| sfdisk -D -uM -H 64 -S 32 $(IMGinst)

	# Install grub
	echo "device (hd0) $(IMGinst)" > /tmp/grubinst.tmp
	echo "root (hd0,0)"           >> /tmp/grubinst.tmp
	echo "setup (hd0)"            >> /tmp/grubinst.tmp
	echo "quit"                   >> /tmp/grubinst.tmp
	grub < /tmp/grubinst.tmp
	rm -f /tmp/grubinst.tmp

	# Compress Image
	gzip -f9 $(IMGinst)

	# Now Build serialcon Version
	# Mount Images
	mount -o loop $(IMGroot) $(MNThdd)
	mount -o loop $(IMGboot) $(MNThdd)/boot

	sed -i -e "s|splashimage|#splashimage|g" $(MNThdd)/boot/grub/grub.conf
	sed -i -e "s|#serial|serial|g" $(MNThdd)/boot/grub/grub.conf
	sed -i -e "s|#terminal|terminal|g" $(MNThdd)/boot/grub/grub.conf
	sed -i -e "s| panic=10 | console=ttyS0,38400n8 panic=10 |g" $(MNThdd)/boot/grub/grub.conf

	sed -i -e "s|#7:2345:respawn:|7:2345:respawn:|g" $(MNThdd)/etc/inittab

	umount $(MNThdd)/boot
	umount $(MNThdd)

	# Cat to an image
	cat $(IMGpart) $(IMGboot) $(IMGroot) $(IMGvar) > $(IMGinsts)
	rm -vf $(IMGpart) $(IMGboot) $(IMGroot) $(IMGvar)
	# Write Partition table
	echo -e ",$(SIZEboot),L,*\n,0,0\n,$(SIZEroot),L\n,$(SIZEvar),L\n" \
		| sfdisk -D -uM -H 64 -S 32 $(IMGinsts)

	# Install grub
	echo "device (hd0) $(IMGinsts)" > /tmp/grubinst.tmp
	echo "root (hd0,0)"            >> /tmp/grubinst.tmp
	echo "setup (hd0)"             >> /tmp/grubinst.tmp
	echo "quit"                    >> /tmp/grubinst.tmp
	grub < /tmp/grubinst.tmp
	rm -f /tmp/grubinst.tmp

	# Compress Image
	gzip -f9 $(IMGinsts)

	rm -rf $(MNThdd) $(IMGpart) $(IMGboot) $(IMGroot) $(IMGvar)
