##
 # Makefile
 # Copyright (C) 2015-2016 Peter Belkner <pbelkner@snafu.de>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
 # version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # Lesser General Public License for more details.
 #
 # You should have received a copy of the GNU Lesser General Public
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301  USA
 ##
include config.mak
include $(SRCDIR)/out_yasapi/version.mak

.PHONY: all install release-bin release-src

UX2DOS=$(shell echo '$(1)' | sed -e 's,^/\(.\)/,\1:/,g')

###############################################################################
NSIS_HOME=/d/Programme/NSIS/nsis-2.46
OUT_YASAPI=out_yasapi-$(OUT_YASAPI_VERSION)
OUT_YASAPI_ALL+=build/$(OUT_YASAPI)/out_yasapi
all: $(OUT_YASAPI_ALL)
all: build/$(OUT_YASAPI)/out_yasapi.nsi
OUT_YASAPI_INSTALL+=$(BINDIR)/out_yasapi
install: $(OUT_YASAPI_INSTALL)

###############################################################################
OUT_YASAPI_OPTS+=--prefix=$(PREFIX)
OUT_YASAPI_OPTS+=--bindir=$(BINDIR)
$(OUT_YASAPI_INSTALL): $(OUT_YASAPI_ALL) FORCE
	@echo 'MAKE	install'
	@cd $(<D) && $(MAKE) install
$(OUT_YASAPI_ALL): build/$(OUT_YASAPI)/Makefile FORCE
	@echo 'MAKE	all'
	@cd $(<D) && $(MAKE) all
build/$(OUT_YASAPI)/Makefile: $(SRCDIR)/out_yasapi/Makefile
build/$(OUT_YASAPI)/Makefile: $(SRCDIR)/out_yasapi/configure
	@echo CONFIGURE
	@mkdir -p $(@D)
	@cd $(@D) && $< $(OUT_YASAPI_OPTS)
	@touch $@

###############################################################################
RELEASE=out_yasapi-$(OUT_YASAPI_VERSION)
RELEASE_SSE2=out_yasapi-sse2-$(OUT_YASAPI_VERSION)
RELEASE_DEBUG=out_yasapi-debug-$(OUT_YASAPI_VERSION)
RELEASE_DEBUG_SSE2=out_yasapi-debug-sse2-$(OUT_YASAPI_VERSION)
RELDIR=release
RELPFX=$(RELDIR)/$(RELEASE)/$(RELEASE)
RELPFX_SSE2=$(RELDIR)/$(RELEASE)/$(RELEASE_SSE2)
RELPFX_DEBUG=$(RELDIR)/$(RELEASE)/$(RELEASE_DEBUG)
RELPFX_DEBUG_SSE2=$(RELDIR)/$(RELEASE)/$(RELEASE_DEBUG_SSE2)

RELEASE_BIN+=-C $(SRCDIR)/out_yasapi
RELEASE_BIN+=doc
RELEASE_BIN+=-C $(PWD)/build/$(OUT_YASAPI)
RELEASE_BIN+=out_yasapi.dll

RELEASE_DEBUG_BIN+=-C $(SRCDIR)/out_yasapi
RELEASE_DEBUG_BIN+=doc
RELEASE_DEBUG_BIN+=-C $(PWD)/build/$(OUT_YASAPI)
RELEASE_DEBUG_BIN+=out_yasapi-debug.dll

RELEASE_SSE2_BIN+=-C $(SRCDIR)/out_yasapi
RELEASE_SSE2_BIN+=doc
RELEASE_SSE2_BIN+=-C $(PWD)/build/$(OUT_YASAPI)
RELEASE_SSE2_BIN+=out_yasapi-sse2.dll

RELEASE_DEBUG_SSE2_BIN+=-C $(SRCDIR)/out_yasapi
RELEASE_DEBUG_SSE2_BIN+=doc
RELEASE_DEBUG_SSE2_BIN+=-C $(PWD)/build/$(OUT_YASAPI)
RELEASE_DEBUG_SSE2_BIN+=out_yasapi-debug-sse2.dll

release-bin: $(OUT_YASAPI_ALL)
release-bin: $(RELPFX).exe
release-bin: $(RELPFX).7z
release-bin: $(RELPFX_SSE2).7z
release-bin: $(RELPFX_DEBUG).7z
release-bin: $(RELPFX_DEBUG_SSE2).7z

RELEASE_SRC+=-C $(SRCDIR)
RELEASE_SRC+=COPYING
RELEASE_SRC+=bits
RELEASE_SRC+=configure
RELEASE_SRC+=Makefile
RELEASE_SRC+=out_yasapi
release-src: $(RELPFX)-src.tar.gz

%.7z: %.tar.bz2
	rm -rf $@ tmp
	mkdir -p tmp
	tar xfvj $< -C tmp
	cd tmp && 7za a $@ *
	rm -rf tmp $<
$(RELPFX).exe: FORCE
	mkdir -p $(@D)
	cd build/$(OUT_YASAPI) && $(NSIS_HOME)/makensis out_yasapi.nsi
$(RELPFX).tar.bz2: FORCE
	mkdir -p $(@D)
	tar cfvj $@ $(RELEASE_BIN) '--transform=s,^,$(RELEASE)/,g'
$(RELPFX_SSE2).tar.bz2: FORCE
	mkdir -p $(@D)
	tar cfvj $@ $(RELEASE_SSE2_BIN) '--transform=s,^,$(RELEASE)/,g'
$(RELPFX_DEBUG).tar.bz2: FORCE
	mkdir -p $(@D)
	tar cfvj $@ $(RELEASE_DEBUG_BIN) '--transform=s,^,$(RELEASE)/,g'
$(RELPFX_DEBUG_SSE2).tar.bz2: FORCE
	mkdir -p $(@D)
	tar cfvj $@ $(RELEASE_DEBUG_SSE2_BIN) '--transform=s,^,$(RELEASE)/,g'
$(RELPFX)-src.tar.gz: FORCE
	mkdir -p $(@D)
	tar cfvz $@ $(RELEASE_SRC) '--transform=s,^,$(RELEASE)/,g'

###############################################################################
NSIOPT+=-e 's,@@VERSION@@,$(OUT_YASAPI_VERSION),g'
NSIOPT+=-e 's,@@RELPFX@@,$(call UX2DOS,$(RELPFX)),g'

build/$(OUT_YASAPI)/out_yasapi.nsi: $(SRCDIR)/out_yasapi/out_yasapi.nsi FORCE
	@echo 'SED	$(@F)'
	@mkdir -p $(@D)
	@sed $(NSIOPT) $< > $@

###############################################################################
.PHONY: FORCE
FORCE:
