#!/usr/bin/make -f

DH_AUTO_OPTIONS := -v -Smakefile --parallel
DESTDIR         := $(CURDIR)/debian/tmp
TMPMAN          := debian/tmp.man
export DEBIAN_CFLAGS := -Wall -Wshadow $(shell getconf LFS_CFLAGS)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  CC := $(DEB_HOST_GNU_TYPE)-gcc
else
  CC := gcc
endif

%:
	dh "$@"

override_dh_auto_configure:
	./configure -d +fhs

override_dh_auto_build:
	dh_auto_build $(DH_AUTO_OPTIONS) -- makefiles CC=$(CC) 
	dh_auto_build $(DH_AUTO_OPTIONS) -- -C src version.h CC=$(CC)
	dh_auto_build $(DH_AUTO_OPTIONS) -- -C man2html CC=$(CC)
	dh_auto_build $(DH_AUTO_OPTIONS) -- -C debian/sources CC=$(CC)

override_dh_auto_clean:
	dh_auto_clean -v -Smakefile 
	$(MAKE) -C  debian/sources clean
	rm -rf $(TMPMAN)

override_dh_installdirs:
	for pkg in $(shell dh_listpackages); do      \
	        dh_installdirs -p$$pkg -P$(DESTDIR); \
	done
	dh_installdirs

override_dh_auto_install:	
	dh_auto_install $(DH_AUTO_OPTIONS) -- -C man2html install  install-hman
	dh_auto_install $(DH_AUTO_OPTIONS) -- -C debian/sources install
	
	chown root:www-data $(DESTDIR)/var/cache/man2html
	chmod 0775 $(DESTDIR)/var/cache/man2html

override_dh_installman:
	mkdir -p $(TMPMAN)
	
	find man man2html/locales  \( -name 'man2html.*' -o -name 'hman.*' \)                   \
	        | while read mp; do                                                             \
	        lang=`echo $$mp| sed -e 's|.*/\(..\)/[^/]*$$|\1|'` ;                            \
	        file="$(TMPMAN)/`basename $$mp`";                                               \
	        [ "$$lang" != "en"  ] || continue;                                              \
	        case $$mp in                                                                    \
	                *hman*) pkg=man2html ;;                                                 \
	                *) pkg=man2html-base ;;                                                 \
	        esac;                                                                           \
	        echo "Installing $$mp man page ($$lang) into package $$pkg" ;                   \
	        tr -d '\015' < "$$mp" > "$$file" || exit 1;                                     \
	        dh_installman --package $$pkg --language "$$lang" "$$file"  || exit 1;          \
	done
	dh_installman

override_dh_installdocs:
	dh_installdocs  --link-doc=man2html-base man2html/README man2html/TODO

override_dh_fixperms:
	dh_fixperms -X cache

