.EXPORT_ALL_VARIABLES:
.PHONY : presetup all clean installer
ifndef TOPDIR
TOPDIR = $(shell pwd)/..
endif
ifndef I_MX35__CC_PREFIX
I_MX35__LINUX_KDIR_PATH:=/mnt/workspace/OSELAS.BSP-Phytec-phyCARD-M-PD10.1.9/platform-phyCARD-M/build-target/linux-2.6.33.6
TOOLCHAIN:=/opt/OSELAS.Toolchain-1.99.3/arm-1136jfs-linux-gnueabi/gcc-4.3.2-glibc-2.8-binutils-2.19-kernel-2.6.27-sanitized
I_MX35__CFLAGS:=
I_MX35__CC_PREFIX:=$(TOOLCHAIN)/bin/arm-1136jfs-linux-gnueabi-
I_MX35__LD_LIBRARY_PATH:=$(TOOLCHAIN)/arm-1136jfs-linux-gnueabi/lib
endif
ifdef TARGETROOT
INSTALL_TARGETROOT:=$(TARGETROOT)/install/
MAIN_TARGETROOT:=$(TARGETROOT)
else
INSTALL_TARGETROOT=$(INSTALLDIR)
MAIN_TARGETROOT=$(INSTALLDIR)target-root
endif
INSTALLDIR = ${TOPDIR}/web/install/
MERGEDIR = $(TOPDIR)/web/merge/
MERGE_ARCHIVE=$(INSTALLDIR)httpd_installer.tar.gz
INST_WEB_FILE=$(INSTALL_TARGETROOT)httpd_installer.sh
TARGETROOT=$(INSTALLDIR)target-root
FIX_RIGHTS_SH:=$(MERGEDIR)fix_rights.sh
SFX_HEADER_WEB:=$(MERGEDIR)sfx-header

##########
#      Rules
##########

all: presetup
	rsync -Crplt $(MERGEDIR)target-root/* $(MAIN_TARGETROOT)/
	@echo "Fixing access rights ( $(shell basename $(FIX_RIGHTS_SH) ) )..."
	@$(FIX_RIGHTS_SH) $(MAIN_TARGETROOT)

installer: $(MERGE_ARCHIVE)
#	@ls $(MERGE_ARCHIVE)
#	cat $(SFX_HEADER_WEB) $(MERGE_ARCHIVE) > $(INST_WEB_FILE)

clean:
	rm -f $(MERGE_ARCHIVE)
	rm -rf $(INSTALLDIR)

presetup:
	@mkdir -p $(TARGETROOT)/httpd/html/cgi-bin/
	@mkdir -p $(MAIN_TARGETROOT)/httpd/html/cgi-bin/
	@mkdir -p $(INSTALL_TARGETROOT)
	@chmod +x $(FIX_RIGHTS_SH)


# ! UNUSED !
# rule to create $(MERGE_ARCHIVE)
# 1. copy merge dir to targets root (rsync -C to ignore CVS files)
# 2. execute the fix_rights.sh Skript, to fix execution-rights
# 3. execute dos2unix for all files, to be sure..
# 4. create the archive using tar
$(MERGE_ARCHIVE): presetup
	rsync -Crplt $(MERGEDIR)target-root/* $(TARGETROOT)/
	@echo "Fixing access rights ( $(shell basename $(FIX_RIGHTS_SH) ) )..."
	@$(FIX_RIGHTS_SH) $(TARGETROOT)
	@echo "Converting all DOS files to UNIX ( dos2unix )..."
	@for element in $(shell find $(TARGETROOT) -name "*" -type f); do \
	 if [ ! -x $$element ]; then \
	   dos2unix $$element; \
	 fi; \
	done
	cd $(TARGETROOT) && tar czvpf $@ ./ 
