#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2014-2016 IOhannes m zmölnig <zmoelnig@iem.at>
# Description: Main Debian packaging script for snd
#
# The authors hereby grant permission to use, copy, modify, distribute,
# and license this software and its documentation for any purpose.  No
# written agreement, license, or royalty fee is required.  Modifications
# to this software may be copyrighted by their authors and need not
# follow the licensing terms described here.
#
# IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
# DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
# IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
# NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# needs to be declared before including makefile.mk
FLAVORS = nox gui-jack gui-pulse

export AUTOHEADER=true

builddir=debian/build/flavor-

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/buildtools.mk
PKG_CONFIG ?= pkg-config

CONFIG = \
	--with-ladspa \
	--with-gmp \
	--prefix=/usr \
	PATH_OGGENC=oggenc PATH_OGGDEC=oggdec \
	PATH_MPG321=mpg321 \
	PATH_SPEEXENC=speexenc PATH_SPEEXDEC=speexdec  \
	PATH_FLAC=flac \
	PATH_TIMIDITY=timidity \
	PATH_WAVPACK=wavpack PATH_WVUNPACK=wvunpack \
	$(empty)

## per flavor configure options
configureflags_audio_jack  = --with-jack
# use ALSA on linux, and OSS non non-linux
ifneq (,$(findstring linux,$(DEB_HOST_ARCH_OS)))
configureflags_audio_default  = --with-alsa
else
configureflags_audio_default  = --with-oss
endif
configureflags_gui = --with-motif --with-gl --with-gl2ps

# nox (alsa/oss + jack)
CONFIG_nox       = --without-gui --with-notcurses \
	$(configureflags_audio_default) $(configureflags_audio_jack)

# gui-jack (jack + alsa[sic!]/oss)
CONFIG_gui-jack  = $(configureflags_gui) \
	$(configureflags_audio_default) $(configureflags_audio_jack)

## gui-pulse (pulseaudio)
CONFIG_gui-pulse = $(configureflags_gui) \
       	--with-pulseaudio

## do not embed __DATE__ (see debian/patches/reproducible.diff)
CPPFLAGS+=-DREPRODUCIBLE_BUILD=1

## on Debian notcurses2 is at time of writing (2020/11/23) at version 2.0.7
## so we need to enable to new API
CPPFLAGS+=-DNOTCURSES_2_0_5=1


# Use the default debhelper scripts, where possible.
%:
	dh $@

# Add configuration options:
override_dh_auto_configure: $(patsubst %,configure_%,$(FLAVORS))
configure_%:
	$(info $()   debian/rules $@)
	mkdir -p $(builddir)$*
	cd $(builddir)$* && ../../../configure $(strip $(CONFIG) $(CONFIG_$*)) || (cat config.log && exit 1)

# Remove the subdirectories generated for the flavors.
override_dh_auto_clean:
	rm -rf $(patsubst %,$(builddir)%,$(FLAVORS))
	rmdir -p $(dir $(builddir)) || echo "couldn't cleanup build-dirs...ignoring"
	rm -f $(SND_MANPAGES)

# Create doxygen documentation:
override_dh_auto_build-indep:
	:
override_dh_auto_build-arch: $(patsubst %,build_%,$(FLAVORS))

build_%:
	$(info $()  debian/rules $@)
	dh_auto_build -a --sourcedirectory=$(builddir)$* || (cat $(builddir)$*/config.log && exit 1)
	mv $(builddir)$*/snd $(builddir)$*/snd.$*


override_dh_installchangelogs:
	dh_installchangelogs debian/upstream-changelog

# build manpages
build_nox: snd.nox.1
build_gui-jack: snd.gui-jack.1
build_gui-pulse: snd.gui-pulse.1

SND_MANPAGES=snd.nox.1 snd.gui-jack.1 snd.gui-pulse.1
$(SND_MANPAGES): snd.1
	cp $^ $@

## missing sources for s7webserver/jqconsole.min.js
##  it seems like the minification was done with a different version of
##  'uglifyjs', as it doesn't produce the same output (whereas the current
##  version of jqconsole builds reproducible)
JQCONSOLE_COMMIT=96eb6c42434d2cd30c83cc9af4e3d574cd6c39c6
s7webserver/jqconsole.min.js: debian/missing-sources/jqconsole.coffee
	coffee -c -p $< | uglifyjs -m -o $@
debian/missing-sources/jqconsole.coffee:
	wget -O $@ https://raw.githubusercontent.com/replit/jq-console/$(JQCONSOLE_COMMIT)/src/jqconsole.coffee

DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
	debian/.*|pix/.*\.png

# licensecheck v1
.PHONY: licensecheck
licensecheck:
	LANG=C.UTF-8 licensecheck \
		-i "^($(DEB_COPYRIGHT_CHECK_IGNORE_REGEX))$$" \
		--check '.*' --recursive --deb-machine --lines 0 * \
		> debian/copyright_newhints
	cmp debian/copyright_hints debian/copyright_newhints \
		&& rm debian/copyright_newhints
