isetup=isetup-5.4.3
#ActivePerl 5.8.9
ActivePerl=ActivePerl
# Convert::ASN1 is required by perl-ldap
oniguruma=oniguruma-v5.9.6
putty=putty-0.63
zlib=zlib-v1.2.8
openssl=openssl-OpenSSL_1_0_2

keep-archives=1

cwd:=$(shell pwd)

packages=$(isetup) $(ActivePerl) $(oniguruma) $(putty) $(zlib)  $(openssl) 


all: download

## Maintainer targets

# Macro to generate download-foo rule to download and unpack a package
#FIXME - This download stuff is horrid
dest=.
downloadcache=/var/tmp
download_rule=								\
download-$(strip $(1)) :  ;						\
	@what=$(strip $(1)) && suffix=$(strip $(2))			\
	&& url=$(strip $(3)) && sub=$(strip $(4))			\
	&& target=$(downloadcache)/$$$$what.$$$$suffix			\
	&& [ -d $$$$what ]						\
	|| { [ -f $$$$target ]						\
	|| { temp=`mktemp /var/tmp/dl.XXXXXX`				\
	&& wget --passive-ftp --no-check-certificate --timeout 30 -O $$$$temp 			\
		$$$$url$$$$what.$$$$suffix    				\
	&& mv -f $$$$temp $$$$target; }					\
	&& case $$$$suffix in						\
		exe|zip) prog="cp" ;;				\
		*.gz|tgz) prog="gzip -d -c" ;;				\
		*.xz) prog="xz -dv" ;;				\
		*.bz2) prog="bzcat -c" ;;				\
		*) prog=cat ;;						\
	esac								\
	&& [ "x$$$$sub" != "x" ] || sub=.				\
	&& echo  $$$$prog     		\
	&& { if [ "x$$$$prog" = "xcp" ]  ; then    \
		  cp -p  $$$$target   $(dest)/   && exit 0; 		\
		fi			\
		}			\
	&& { $$$$prog $$$$target | tar -C $(dest)/$$$$sub -xvf - ; }	\
	&& echo sub:$$$$sub						\
	&& { [ "x$$$$sub" = "x." ] || ln -nsf "$$$$sub" "$$$$what" ; }	\
	&& { [ "x$$$$sub" != "x." ] || [ "$(dest)" = . ]		\
		|| ln -sf $(dest)/$$$$what . ; }			\
	&& if [ "x$(keep-archives)" != "x1" ] ; then			\
		rm $$$$target ;						\
	fi								\
	}

git_rule=								\
download-$(strip $(1)) : ;						\
	@what=$(strip $(1)) && module=$(strip $(2))			\
	&& repo=$(strip $(3)) && vers=$$$${what\#\#*-}			\
	&& target=$(downloadcache)/$$$$what			\
	&& [ -d $$$$what ]						\
	|| { [ -d $$$$target ]						\
	|| { temp=`mktemp -u /var/tmp/dl.XXXXXX`		\
	&& git clone $$$$repo  $$$$temp			\
	&& mv -f $$$$temp $$$$target; }				\
	&& git clone $$$$target  $$$$module					\
	&& cd $$$$module && { git checkout $$$$vers || git checkout $$$$what || : ;  }	\
	}
	
	
cvs_rule=								\
download-$(strip $(1)) : ;						\
	@what=$(strip $(1)) && module=$(strip $(2))			\
	&& repo=$(strip $(3)) && date=$$$${what\#\#*-}			\
	&& { [ -d "$$$$what" ] && exit 0 || : ; }			\
	&& cd $(dest)							\
	&& [ ! -e $$$$module ]						\
	&& cvs -z 9 -d $$$$repo checkout -D $$$$date $$$$module		\
	&& mv $$$$module $$$$what					\
	&& { [ "$(dest)" == . ] || ln -sf $(dest)/$$$$what $(cwd) ; }

download_targets=$(addprefix download-,$(packages) )

download: $(download_targets)

$(call download_rule, $(isetup), exe ,                                        \
  http://files.jrsoftware.org/is/5/)
$(call download_rule, $(ActivePerl), zip,                                    \
  http://www.mklpg.com/files/)
  
$(call git_rule, $(oniguruma),oniguruma,                                         \
  https://github.com/kkos/oniguruma.git)
$(call git_rule, $(putty),putty,                                         \
  https://git.tartarus.org/simon/putty.git)
$(call git_rule, $(zlib),zlib,                                         \
  https://github.com/madler/zlib.git)
$(call git_rule, $(openssl),openssl,                                         \
  https://github.com/openssl/openssl.git)

# Blow away everything we do not want to ship.
tidy:
	rm -rf dosboot iso
	rm -rf .stamp-stage1 stage2 .stamp-stage2
	rm -f devices.cpio initrd
	rm -f *.tmp

# Clean up all the stuff we built so we can build again
clean: tidy
	rm -rf tftpboot
	rm -rf linuxboot.iso
	rm -rf ../install/linuxaux
todolist:
	-@for file in Makefile; do fgrep -H -e TODO -e FIXME $$file; done; true

FORCE:

# this particular makefile doesn't completely work when building in
# parallel but we want to be able to pass the -j flag to each of the
# sub-makes particularly for the kernel and glibc
.NOTPARALLEL:

.PHONY: stage1 all download $(download_targets)  \
	iso tftpboot tidy clean FORCE
