DOWNLOAD_CMD ?= wget
UNZIP ?= bsdtar
MINGW_BINDIR ?= C:\\\\MinGW\\\\bin  # Used for InnoSetup script

define BSDTAR
	mkdir -p $(DEST_DIR) && bsdtar -x -C$(DEST_DIR) -f $^
endef
define SEVENZIP
	7z x -o$(DEST_DIR) $^
endef
define REAL_UNZIP
	unzip -d$(DEST_DIR) $^
endef

ifeq ($(UNZIP), bsdtar)
	UNZIP_CMD = $(BSDTAR)	
else ifeq ($(UNZIP), 7z)
	UNZIP_CMD = $(SEVENZIP)	
else ifeq ($(UNZIP), unzip)
	UNZIP_CMD = $(REAL_UNZIP)	
endif


PREFIX ?= c:/chicken-iup
NATIVE_PREFIX = $(subst /,\\\\,$(PREFIX))  # Used for InnoSetup script

CHICKEN_URL = http://code.call-cc.org/releases
ifndef CHICKEN_VERSION
	CHICKEN_VERSION = 4.12.0
	CHICKEN_PATCHLEVEL = 
endif
CHICKEN_PKGNAME = chicken-$(CHICKEN_VERSION)$(CHICKEN_PATCHLEVEL)
CHICKEN_DIR = $(CHICKEN_PKGNAME)
CHICKEN_PKG = $(CHICKEN_PKGNAME).tar.gz
CHICKEN_PLATFORM ?= mingw-msys
CHICKEN_PREFIX ?= $(PREFIX)

EGGS = apropos trace regex-case regex-literals format big-chicken pdf sql-de-lite \
coops tinyclos test regex slime utf8 matchable apropos base64 regex-literals format "regex-case" "test" \
	coops trace csv dot-locking posix-utils posix-extras directory-utils \
	tcp rpc csv-xml fmt json awful spiffy uri-common intarweb \
	spiffy-request-vars s spiffy-directory-listing ssax sxml-serializer \
	sxml-modifications logpro z3 call-with-environment-variables \
	pathname-expand typed-records simple-exceptions numbers srfi-42 \
	alist-lib ansi-escape-sequences args basic-sequences bindings chicken-doc \
	cock condition-utils define-record-and-printer \
	expand-full filepath foof-loop ini-file irc lalr lazy-seq \
	loops low-level-macros procedural-macros rfc3339 scsh-process \
	sexp-diff shell slice srfi-101 \
	srfi-29 srfi-37 srfi-78 
	 


define GET_FFCALL_CVS
	cvs -z3 -d:pserver:anonymous@cvs.savannah.gnu.org:/sources/libffcall co ffcall
endef
define GET_FFCALL_PKG
	wget http://www.haible.de/bruno/gnu/$(FFCALL_PKG)
	tar zxvf $(FFCALL_PKG)
endef

FFCALL_VERSION ?= cvs
FFCALL_PREFIX ?= $(PREFIX)
ifeq ($(FFCALL_VERSION), cvs)
	FFCALL_DIR = ffcall
	FFCALL_PKG =
	GET_FFCALL = $(GET_FFCALL_CVS)
else
	FFCALL_DIR = ffcall-$(FFCALL_VERSION)
	FFCALL_PKG = ffcall-$(FFCALL_VERSION).tar.gz
	GET_FFCALL = $(GET_FFCALL_PKG)
endif


IUP_URL = http://sourceforge.net/projects
IUP_PLATFORM ?= Win32_dllw4
IUP_LIBDIR ?= $(PREFIX)/lib

IM_LONGNAME = imtoolkit
IM_VERSION ?= 3.12
IM_PKG = im-$(IM_VERSION)_$(IUP_PLATFORM)_lib.zip

CD_LONGNAME = canvasdraw
CD_VERSION ?= 5.9
CD_PKG = cd-$(CD_VERSION)_$(IUP_PLATFORM)_lib.zip

IUP_LONGNAME = iup
IUP_VERSION ?= 3.22
IUP_PKG = iup-$(IUP_VERSION)_$(IUP_PLATFORM)_lib.zip

SRC_PKGS = $(CHICKEN_PKG) $(FFCALL_PKG)
BIN_PKGS = $(IM_PKG) $(CD_PKG) $(IUP_PKG)
SRC_DIRS = $(CHICKEN_DIR) $(FFCALL_DIR)


.PHONY : all pkgs.prep srcpkgs.get binpkgs.get srcpkgs.verify binpkgs.verify srcpkgs.extract chicken.compile binpkgs.install iup_all.install im.install cd.install iup.install srcpkgs.compile install base.install binpkg.prep strip-lua eggs clean distclean lzma-kludge

all : install binpkg.prep

pkgs.prep : srcpkgs.extract binpkgs.verify

srcpkgs.get : $(CHICKEN_PKG)

$(CHICKEN_PKG) :
	$(DOWNLOAD_CMD) $(CHICKEN_URL)/$(CHICKEN_VERSION)/$(CHICKEN_PKG)
	# tar zxvf $(CHICKEN_PKG)

binpkgs.get : $(IM_PKG) $(CD_PKG) $(IUP_PKG)	

$(IM_PKG) :
	$(DOWNLOAD_CMD) $(IUP_URL)/$(IM_LONGNAME)/files/$(IM_VERSION)/Windows%20Libraries/Dynamic/$(IM_PKG)

$(CD_PKG) :
	$(DOWNLOAD_CMD) $(IUP_URL)/$(CD_LONGNAME)/files/$(CD_VERSION)/Windows%20Libraries/Dynamic/$(CD_PKG)

$(IUP_PKG) :
	$(DOWNLOAD_CMD) $(IUP_URL)/$(IUP_LONGNAME)/files/$(IUP_VERSION)/Windows%20Libraries/Dynamic/$(IUP_PKG)

srcpkgs.verify : srcpkgs.get

chicken.verify : $(CHICKEN_PKG)

ffcall.verify :

binpkgs.verify : binpkgs.get

srcpkgs.extract : srcpkgs.verify $(CHICKEN_DIR) $(FFCALL_DIR)

$(CHICKEN_DIR) : chicken.verify
	tar zxvf $(CHICKEN_PKG)
	touch $(CHICKEN_DIR)

$(FFCALL_DIR) :
	$(GET_FFCALL)
	touch $(FFCALL_DIR)

binpkgs.install : binpkgs.verify iup_all.install

iup_all.install : lzma-kludge im.install cd.install iup.install

im.install : DEST_DIR = $(IUP_LIBDIR)
im.install : $(IM_PKG)
	$(UNZIP_CMD)

cd.install : DEST_DIR = $(IUP_LIBDIR)
cd.install : $(CD_PKG)
	$(UNZIP_CMD)

iup.install : DEST_DIR = $(IUP_LIBDIR)
iup.install : $(IUP_PKG)
	$(UNZIP_CMD)

lzma-kludge :
	if [ "${UNZIP}" = "bsdtar" ]; then \
		cd /bin && \
		if [ ! -f msys-lzma-1.dll ]; then \
			ln -s msys-lzma-5.dll msys-lzma-1.dll; \
		fi \
	fi

srcpkgs.compile : chicken.compile ffcall.compile

chicken.compile : $(CHICKEN_DIR)
	cd $(CHICKEN_DIR) && \
	$(MAKE) PLATFORM=$(CHICKEN_PLATFORM) PREFIX=$(CHICKEN_PREFIX)	

ffcall.compile : $(FFCALL_DIR)
	cd $(FFCALL_DIR) && \
	./configure --prefix=$(FFCALL_PREFIX) && \
	$(MAKE)

base.install :  srcpkgs.compile chicken.install ffcall.install binpkgs.install

# chicken.install : chicken.compile
chicken.install :
	cd $(CHICKEN_DIR) && \
	$(MAKE) PLATFORM=$(CHICKEN_PLATFORM) PREFIX=$(CHICKEN_PREFIX) install

ffcall.install : 
	cd $(FFCALL_DIR) && \
	$(MAKE) install

install : base.install eggs

eggs :
	CSC_OPTIONS="-I$(PREFIX)/include/ -I$(PREFIX)/lib/include -L$(PREFIX)/lib" $(PREFIX)/bin/chicken-install -D disable-iup-web -D disable-iup-pplot iup:1.7.0
	CSC_OPTIONS="-I$(PREFIX)/include/ -I$(PREFIX)/lib/include -L$(PREFIX)/lib" $(PREFIX)/bin/chicken-install -D disable-canvas-draw-native canvas-draw
#	#$(foreach egg,$(EGGS),CSC_OPTIONS="-I$(PREFIX)/include/ -I$(PREFIX)/lib/include -L$(PREFIX)/lib" $(PREFIX)/bin/chicken-install -keep-installed $(egg);)
	CSC_OPTIONS="-I$(PREFIX)/include/ -I$(PREFIX)/lib/include -L$(PREFIX)/lib" $(PREFIX)/bin/chicken-install -keep-installed $(EGGS)

# CAREFUL: this rule removes the entire $PREFIX directory tree!
binpkg.prep : chicken-iup.iss strip-lua
	mkdir -p binpkg && \
	mv $(PREFIX)/* binpkg && rmdir $(PREFIX)

chicken-iup.iss : chicken-iup.iss.in
	sed "s#{%CIUPPrefix%}#$(strip $(NATIVE_PREFIX))#g" $< \
	| sed "s#{%ChickenVersion%}#$(strip $(CHICKEN_VERSION)$(CHICKEN_PATCHLEVEL))#g" \
	| sed "s#{%MinGWBindir%}#$(strip $(MINGW_BINDIR))#g" > $@ 

strip-lua :
	rm -f $(IUP_LIBDIR)/*lua* $(IUP_LIBDIR)/include/*lua*

clean : chicken.clean ffcall.clean

chicken.clean :
	cd $(CHICKEN_DIR) && \
	$(MAKE) PLATFORM=$(CHICKEN_PLATFORM) clean

ffcall.clean :
	cd $(FFCALL_DIR) && \
	$(MAKE) clean

distclean :
	# rm -rf $(SRC_PKGS) $(BIN_PKGS) $(SRC_DIRS) binpkg Output chicken-iup.iss
	# Matt W requested installer in repo, so Output is omitted
	rm -rf $(SRC_PKGS) $(BIN_PKGS) $(SRC_DIRS) binpkg chicken-iup.iss

