#!/usr/bin/make -f

#export DH_VERBOSE=1


PACKAGE		= stterm
# Needed for st.1 fix up
VERSION         = 0.4.1

ORIGBIN		= st
CHANGELOG	= debian/upstream.changelog
UPSTREAM_DIR    = ../upstream.git
UPSTREAM_VCS    = http://git.suckless.org/st

BIN		= $(PACKAGE)
PKGDIR		= $(CURDIR)/debian/$(PACKAGE)
BINDIR		= $(PKGDIR)/usr/bin
SHAREROOTDIR	= $(PKGDIR)/usr/share
DOCROOTDIR	= $(SHAREROOTDIR)/doc
PKGDOCDIR	= $(DOCROOTDIR)/$(PACKAGE)
TERMINFO	= $(PKGDIR)/usr/share/terminfo
MANROOTDIR	= $(SHAREROOTDIR)/man
MAN1DIR		= $(MANROOTDIR)/man1

export DEB_BUILD_MAINT_OPTIONS	= hardening=+all
export DEB_CFLAGS_MAINT_APPEND	= -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Needed for gcc:
# warning: implicit declaration of function 'unsetenv'
# warning: implicit declaration of function 'setenv'
# warning: implicit declaration of function 'nanosleep'
# warning: implicit declaration of function 'kill'

export DEB_CFLAGS_MAINT_APPEND += -D_POSIX_C_SOURCE=200112L

# warning: implicit declaration of function 'strsep'

export DEB_CFLAGS_MAINT_APPEND += \
    -D_BSD_SOURCE -D_XOPEN_SOURCE=600

# From original sources: config.mk

CFLAGS += -std=c99 --param=ssp-buffer-size=4 \
	  -I/usr/include -I/usr/X11R6/include -DVERSION=\"0.4.1\" \
	  $$(pkg-config --cflags freetype2)

LDFLAGS += -s -L/usr/lib -lc -L/usr/X11R6/lib -lX11 -lutil -lXext -lXft -lfontconfig

get-changelog:
	[ -d $(UPSTREAM_DIR) ] || git clone $(UPSTREAM_VCS) $(UPSTREAM_DIR)

	pwd=$$(pwd); \
	cd $(UPSTREAM_DIR) && git log | head -n 100 | \
	sed 's/ *$$//' > "$$pwd/$(CHANGELOG)"

terminfo:
	install -m 755 -d $(TERMINFO)/s
	TERMINFO=$(TERMINFO) tic $(ORIGBIN).info

override_dh_auto_build:
	# Can't inject build flags to makefile; compile by hand
	cp config.def.h config.h
	$(CC) $(CFLAGS) $(CPPFLAGS) $(ORIGBIN).c -o $(BIN) $(LDFLAGS)
	rm -f config.h

override_dh_installinfo:
	# Disable; the *.info is not a info(1) but a terminfo file

override_dh_installchangelogs:
	dh_installchangelogs $(CHANGELOG)

override_dh_auto_install: terminfo
	# Upstream no good. Calls tic(1) which writes to $HOME
	install -m 755 -D $(BIN) $(BINDIR)/$(BIN)
	install -m 644 -D \
		$(ORIGBIN).info \
		$(PKGDOCDIR)/examples/$(PACKAGE).terminfo
	install -D st.1 $(MAN1DIR)/stterm.1
	sed --in-place "s/-VERSION/-$(VERSION)/" $(MAN1DIR)/stterm.1

%:
	dh $@

.PHONY: man terminfo

# End of file
