#!/usr/bin/make -f
# -*- makefile -*-

DEB_AVIFILE_FLAG    := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' libavifile-0.7-dev))
DEB_VORBIS_FLAG     := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' libvorbis-dev))
DEB_TREMOR_FLAG     := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' libvorbisidec-dev))
DEB_MP3MAD_FLAG     := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' libmad0-dev))
DEB_SMPEG_FLAG      := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' libsmpeg-dev))
DEB_FONTCONFIG_FLAG := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' libfontconfig1-dev))
DEB_LUA_FLAG        := $(findstring ok installed,$(shell dpkg-query -W -f='$${Status}' liblua5.2-dev))

M_CFLAGS = $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS) -Wall
M_LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

CONFDEFS := -DLINUX -DUSE_CDROM
CONFINCS := $(M_CFLAGS) `sdl-config --cflags`
CONFLIBS := $(M_CFLAGS) $(M_LDFLAGS) `sdl-config --libs` -lSDL_ttf -lSDL_image -lSDL_mixer -lbz2 -ljpeg -lm
CONFEXT_OBJS :=
CONFTARGET := onscripter sardec nsadec sarconv nsaconv

# Use smpeg when it exists
ifneq (,$(DEB_SMPEG_FLAG))
CONFINCS += `smpeg-config --cflags`
CONFLIBS += `smpeg-config --libs`
CONFDEFS += -DUSE_SMPEG
else
# Use mad if it is installed
ifneq (,$(DEB_MP3MAD_FLAG))
CONFINCS += `pkg-config --cflags mad`
CONFLIBS += `pkg-config --libs mad`
CONFDEFS += -DMP3_MAD
endif
endif

# avifile is only available on i386 and amd64
ifneq (,$(DEB_AVIFILE_FLAG))
CONFINCS += `avifile-config --cflags`
CONFLIBS += `avifile-config --libs`
CONFDEFS += -DUSE_AVIFILE
CONFEXT_OBJS += "AVIWrapper.o"
CONFTARGET += "simple_aviplay"
endif

# Use tremor/vorbis if they are installed
ifneq (,$(DEB_TREMOR_FLAG))
CONFLIBS += -lvorbisidec
CONFDEFS += -DUSE_OGG_VORBIS -DINTEGER_OGG_VORBIS
else
ifneq (,$(DEB_VORBIS_FLAG))
CONFINCS += `pkg-config --cflags vorbisfile`
CONFLIBS += `pkg-config --libs vorbisfile`
CONFDEFS += -DUSE_OGG_VORBIS
endif
endif

# Use fontconfig if it is installed
ifneq (,$(DEB_FONTCONFIG_FLAG))
CONFINCS += `pkg-config --cflags fontconfig`
CONFLIBS += `pkg-config --libs fontconfig`
CONFDEFS += -DUSE_FONTCONFIG
endif

# Use lua if it is installed
ifneq (,$(DEB_LUA_FLAG))
CONFINCS += `pkg-config --cflags lua5.2`
CONFLIBS += `pkg-config --libs lua5.2`
CONFDEFS += -DUSE_LUA
CONFEXT_OBJS += LUAHandler.o
endif

%:
	dh  $@

onscripter.6: debian/onscripter.sgml
	docbook-to-man debian/onscripter.sgml > onscripter.6

nsaconv.6: debian/nsaconv.sgml
	docbook-to-man debian/nsaconv.sgml > nsaconv.6

nsadec.6: debian/nsadec.sgml
	docbook-to-man debian/nsadec.sgml > nsadec.6

sardec.6: debian/sardec.sgml
	docbook-to-man debian/sardec.sgml > sardec.6

override_dh_auto_build: onscripter.6 nsaconv.6 nsadec.6 sardec.6
	$(MAKE) DEFS="$(CONFDEFS) -DENABLE_1BYTE_CHAR -DFORCE_1BYTE_CHAR" \
		INCS="$(CONFINCS)" LIBS="$(CONFLIBS)" \
		TARGET="$(CONFTARGET)" EXT_OBJS="$(CONFEXT_OBJS)" \
		-f Makefile.Linux
	if test ! -e onscripter-1byte; then \
		cp -f onscripter onscripter-1byte; \
	fi
	$(MAKE) -f Makefile.Linux clean
	$(MAKE) DEFS="$(CONFDEFS)" INCS="$(CONFINCS)" LIBS="$(CONFLIBS)" \
		TARGET="$(CONFTARGET)" EXT_OBJS="$(CONFEXT_OBJS)" \
		-f Makefile.Linux

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) -f Makefile.Linux clean

override_dh_auto_install:
	dh_auto_install
	install -D www/* $(CURDIR)/debian/onscripter/usr/share/doc/onscripter/html
	if test -e simple_aviplay; then \
		dh_install simple_aviplay /usr/lib/games/onscripter; \
	fi