#!/usr/bin/make -f

%:
	dh $@

# We divert the shared libraries (which are all internal to
# Hercules) into /usr/lib/hercules, which in turn means the plugins would
# normally go in /usr/lib/hercules/hercules.
#
# However, Hercules will actually look in ${prefix}/lib/hercules for
# plugins (and that path makes more sense anyway), so we override it
# at build/install time by changing the make arguments.
#
# Hopefully this will become unnecessary if upstream add a --with-modexecdir
# option or something. It might need changing in new upstream releases if
# they stop hard-coding ${prefix}/lib/hercules.

CONFIGURE_ARGS := --prefix=/usr --enable-optimization="$(CFLAGS) $(CPPFLAGS)" \
	--libdir=/usr/lib/hercules \
	--mandir=/usr/share/man/ --enable-cckd-bzip2 --enable-het-bzip2 \
	--enable-custom=Debian --enable-capabilities \
	--enable-external-gui --without-included-ltdl
MAKE_ARGS := modexecdir=/usr/lib/hercules

export DEB_LDFLAGS_MAINT_APPEND = -lltdl

override_dh_autoreconf:
	dh_autoreconf autoreconf -- -fvi -Im4 -Iautoconf

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIGURE_ARGS)

override_dh_auto_build:
	dh_auto_build -- $(MAKE_ARGS)
	/usr/bin/docbook-to-man debian/hercules.sgml > hercules.1

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(CURDIR)/debian/hercules $(MAKE_ARGS)
	sed -i "/dependency_libs/ s/'.*'/''/" `find $(CURDIR)/debian/hercules -name '*.la'`
	rm $(CURDIR)/debian/hercules/usr/bin/bldlvlck
	rm $(CURDIR)/debian/hercules/usr/bin/herclin
	install -m 644 hercules.1 $(CURDIR)/debian/hercules/usr/share/man/man1
	mkdir -p $(CURDIR)/debian/hercules/usr/libexec/hercules
	mv $(CURDIR)/debian/hercules/usr/bin/hercifc \
		$(CURDIR)/debian/hercules/usr/libexec/hercules/hercifc
	mkdir -p $(CURDIR)/debian/hercules/usr/share/doc/hercules
	mv -t $(CURDIR)/debian/hercules/usr/share/doc/hercules \
		$(CURDIR)/debian/hercules/usr/share/hercules/images \
		$(CURDIR)/debian/hercules/usr/share/hercules/*.css \
		$(CURDIR)/debian/hercules/usr/share/hercules/*.html

override_dh_auto_clean:
	dh_auto_clean
	rm -f hercules.1
	$(RM) -r libltdl/

override_dh_link:
	dh_link
	jdupes -rl debian/hercules/usr
