# for package making
VERSION = 2.1.0
BASE_KERNEL = 2.6.6
SUBDIRS = command addons
CLEANUPS = command/lkstutils.spec

TARGETS = $(SUBDIRS) hooks LICENSE linux26.patch Makefile Install howto.txt \
	  changelog extra-patch examples scripts
RM_TARGETS = scripts/lsm_gen examples/lkst_test_run
PKGNAME = lkst-$(VERSION)
BASE_KERNEL_ABBR = `echo $(BASE_KERNEL) | tr -d . `
KSRC = /lib/modules/`uname -r`/build

#specified from command line
KPRESRC =
LKCDDIR =

ifeq ($(LKCDDIR), "")
CONFOPT =
else
CONFOPT = --with-lkcdutils=$(LKCDDIR)
endif

.PHONY:	all configure package dist distclean adjustver config patch patch_vmsync help

all: configure
	@( for dir in $(SUBDIRS) ; do \
		( cd $$dir ; \
		[ -f Makefile ] && make all ; \
		cd .. ); \
	done )

package: dist

dist: distclean adjustver
	@mkdir $(PKGNAME)
	@cp -a $(TARGETS) $(PKGNAME)
	@rm -rf `find $(PKGNAME) -name CVS -a -type d`
	@(cd $(PKGNAME); rm -rf $(RM_TARGETS))
	tar czvf $(PKGNAME).tar.gz $(PKGNAME)
	@rm -rf $(PKGNAME)

distclean:
	@( for dir in $(SUBDIRS) ; do \
		( cd $$dir ; \
		[ -f Makefile ] && make distclean ; \
		cd .. ); \
	done )
	rm -rf $(CLEANUPS)

clean:
	@( for dir in $(SUBDIRS) ; do \
		( cd $$dir ; \
		[ -f Makefile ] && make clean ; \
		cd .. ); \
	done )

configure: config

config:
	@( for dir in $(SUBDIRS) ; do \
		( cd $$dir ; \
		echo "configuring " $$dir ; \
		[ -f configure ] || autoconf ; \
		[ -x configure -a ! -f .config ] && ./configure --with-ksrc=$(KSRC) $(CONFOPT); \
		echo "done"; \
		cd ..); \
	done )

install:
	@( for dir in $(SUBDIRS) ; do \
		( cd $$dir ; \
		echo "installing " $$dir ; \
		[ -f Makefile ] && make install ; \
		echo "done"; \
		cd ..); \
	done )

adjustver:
	@cat command/cmd.conf | sed /^LKSTVER/s/.*/LKSTVER=$(VERSION)/ > temp.conf
	@mv temp.conf command/cmd.conf
	@cat addons/cmd_mod.conf | sed /^LKSTVER/s/.*/LKSTVER=$(VERSION)/ > temp.conf
	@mv temp.conf addons/cmd_mod.conf

PATCH = patch -d $(KPRESRC) -p1

patch:
	@( if [ -d "$(KPRESRC)" ] ;then \
	$(PATCH) < extra-patch/preassigned-$(BASE_KERNEL).patch	; \
	$(PATCH) < hooks/kernelhooks-v1.8-$(BASE_KERNEL_ABBR)-base.patch ; \
	$(PATCH) < hooks/kernelhooks-v1.8-fix_kconfig.patch	; \
	$(PATCH) < hooks/kernelhooks-v1.8-fix_kmakefile.patch	; \
	$(PATCH) < hooks/kernelhooks-v1.8-resolve_dependency.patch	 ; \
	$(PATCH) < hooks/kernelhooks-v1.8-support_ia64.patch	; \
	$(PATCH) < linux26.patch				; \
	else \
	echo "please specify KPRESRC=\"original kernel source directory\" "; \
	fi )

patch_vmsync:
	@( if [ -d "$(KPRESRC)" ] ;then \
	$(PATCH) < extra-patch/vmsync-$(BASE_KERNEL).patch	; \
	$(PATCH) < hooks/kernelhooks-v1.8-$(BASE_KERNEL_ABBR)-base.patch ; \
	$(PATCH) < hooks/kernelhooks-v1.8-fix_kconfig.patch	; \
	$(PATCH) < hooks/kernelhooks-v1.8-fix_kmakefile.patch	; \
	$(PATCH) < hooks/kernelhooks-v1.8-resolve_dependency.patch	 ; \
	$(PATCH) < hooks/kernelhooks-v1.8-support_ia64.patch	; \
	$(PATCH) < linux26.patch				; \
	$(PATCH) < extra-patch/lkst-use_vmsync.patch		; \
	else \
	echo "please specify KPRESRC=\"original kernel source directory\" "; \
	fi )

help:
	@echo "Building targets:"
	@echo "  all (default) - build all (command/ and addons/)"
	@echo
	@echo "Cleaning targets:"
	@echo "  clean        - remove most generated files but keep the config"
	@echo "  distclean    - remove all generated files + config files"
	@echo 
	@echo "Configuration target:"
	@echo "  config       - generate new config"
	@echo "            (Options) KSRC=<dir>    specify kernel source tree" 
	@echo "                           (default:="/lib/modules/`uname -r`/build")"
	@echo "                      LKCDDIR=<dir> enable LKCD support and"
	@echo "                                    specify lkcdutils source tree"
	@echo "                           (disabled by default)"
	@echo
	@echo "Patching targets:"
	@echo "  patch        - patch a series of patches for LKST and kernelhooks"
	@echo "  patch_vmsync - patch a series of patches but use vmsync patch"
	@echo "                 instead of preassigned pagetable patch"
	@echo "            (Argument) KPRESRC=<dir> specify kernel source tree before patched"
	@echo 
	@echo "Other targets:"
	@echo "  install      - install all binaries (command/ and addons/)"
	@echo "  dist/package - generate a package"
	@echo 
