#! /usr/bin/make
# this is the makefile for the wlus testsuite.
# To run the test invoke make.
# you can also run the testsuite manually.

#include ./config

# FIXME: i want to distinguish if the image exits allready and
# needs updating or if must be created from scratch.

##
## Definitions
##

# this is the location of the preseed files
preseeding_files = ~/src/debian-edu-install/base-config/defaults

preseed_list_main_server :=	main-server networked
preseed_list_terminal_server :=	thin-client-server networked

package_list_common :=		\
				devfsd \
				ssh \
				openafs-modules-uml-2.4.26-3um \
				krb5-user \
				libkrb53 \
				tcpdump \
				host \
				gettext \
				less

PACKAGES_main_server =		$(package_list_common) \
				bind9 \
				openafs-dbserver \
				webmin-ldap-user-simple \
				krb5-admin-server \
				openafs-krb5 \
				krb5-kdc \
				debian-edu-config=0.376 

PACKAGES_terminal_server = 	$(package_list_common) \
				openafs-fileserver \
				openafs-client \
				libpam-krb5

config_vars := UML_IP PACKAGES PRESEED HOSTNAME CONFIG_TAR
UML_IP_main_server :=		10.0.2.2
UML_IP_terminal_server :=	10.0.2.10
HOSTNAME_main_server :=		tjener
HOSTNAME_terminal_server :=	ltspserver00
CONFIG_TAR_main_server :=	$(CURDIR)/config.tar
CONFIG_TAR_terminal_server :=


# these defines are just there to make things more easy to read
define	replace_var
	/usr/bin/perl -pi -e 's:$(var):$($(var)_$*):' $@; 
endef


# integrates all preseed files into one
%.preseed: 
	cat $(foreach file, $(preseed_list_$*), $(strip $(preseeding_files).$(file)) ) > $@
#	echo "base-config base-config/late_command string echo \"/dev/ubd/disc1/part1 /vicepa ext2 defaults 0 0\" >> /etc/fstab ; mkdir /vicepa; echo running early_command" >> $@
#	echo "base-config base-config/early_command string rm -f /etc/init.d/hwclock.sh /etc/init.d/hwclockfirst.sh; mkdir /etc/init.d; touch /etc/init.d/hwclockfirst.sh; touch /etc/init.d/hwclock.sh; echo \"#!/bin/sh -e \" > /etc/init.d/hwclock.sh; cp /etc/init.d/hwclock.sh /etc/init.d/hwclockfirst.sh; echo running early_command" >> $@

# this is supposed to be a pattern specific variable which should
# kick in when the replace_var is done.
main_server.conf: PRESEED_main_server = $(CURDIR)/main_server.preseed
terminal_server.conf: PRESEED_terminal_server = $(CURDIR)/terminal_server.preseed
# this does not work as expected. why?
%.conf: PRESEED_$* = $(CURDIR)/%.preseed

# this loops over the template and fills in the variables 
%.conf: %.preseed
	cp rootstrap.conf.template $@
#	@echo PRESEED_$* = $(CURDIR)/$*.preseed
	$(foreach var, $(config_vars), $(replace_var))

%.img: %.conf
	cp $*.conf rootstrap.conf
	LD_ASSUME_KERNEL="2.4.2" rootstrap -o results/$*.log $@

%.vice:
	bunzip2 -k -c blank.vicep.bz2 > $@

%: 	%.vice %.img
	/usr/bin/linux \
		mem=256M \
		con0=fd:0,fd:1 \
		ubd0=$*_img_cow,$*.img \
		ubd1=$*_vice_cow,$*.vice \
		devfs=mount \
		eth0=daemon,,unix,/var/run/uml-utilities/uml_switch.ctl \
		umid=$@

tests-stamp: rootstrap.img
	run-parts ./tests
	touch tests-stamp

clean:
	-rm -f	*.img \
		*-stamp \
		*.preseed \
		*.conf \
		*.preseed_list \
		*.packages \
		*.vice \
		*_cow \
		results/*.log

### to be integrated
# fakeroot sh -c 'chown -R bind.root etc/bind/; find . -name
# "*bind*" -o -name "hosts" | xargs tar cf ../../config.tar '
