#! /bin/sh
# Next line is a comment for make but executed by /bin/sh \
exec make -f "$0" "$@"

PREFIX = $(shell pwd)
WGET = $(shell which wget 2>/dev/null)
CURL = $(shell which curl 2>/dev/null)
DOWNLOADER = $(if $(WGET), $(WGET) -O, $(if $(CURL), $(CURL) -o, $(error Error: No wget or curl found in PATH)))

JF_VERSION = @JELLYFISH_VERSION@
JF_URL = ftp://ftp.genome.umd.edu/pub/jellyfish/jellyfish-$(JF_VERSION).tar.gz

QUORUM_VERSION = @PACKAGE_VERSION@
QUORUM_URL = ftp://ftp.genome.umd.edu/pub/quorum/quorum-$(QUORUM_VERSION).tar.gz

all: info $(PREFIX)/bin/jellyfish $(PREFIX)/bin/quorum
	@echo ************************************************************
	@echo Installation successful
	@echo For some help see: $(PREFIX)/bin/quorum --help
	@echo ************************************************************

.PHONY: info
info:
	@echo "Installing Jellyfish and Quorum in $(PREFIX)"

jellyfish-$(JF_VERSION).tar.gz:
	$(DOWNLOADER) $@.tmp $(JF_URL)
	mv $@.tmp $@

quorum-$(QUORUM_VERSION).tar.gz:
	$(DOWNLOADER) $@.tmp $(QUORUM_URL)
	mv $@.tmp $@

%/configure: %.tar.gz
	tar zxf $<
	touch $@

$(PREFIX)/bin/jellyfish: jellyfish-$(JF_VERSION)/configure
	cd $(dir $<); ./configure --prefix=$(PREFIX)
	$(MAKE) -C $(dir $<) install

$(PREFIX)/bin/quorum: quorum-$(QUORUM_VERSION)/configure $(PREFIX)/bin/jellyfish
	cd $(dir $<); ./configure --prefix=$(PREFIX) JELLYFISH_LIBS="-L$(PREFIX)/lib -ljellyfish-2.0" JELLYFISH_CFLAGS="-I$(PREFIX)/include/jellyfish-$(JF_VERSION)" JF_LIB_PATH="$(PREFIX)/lib" JELLYFISH="$(PREFIX)/bin/jellyfish"
	$(MAKE) -C $(dir $<) install
