#!/usr/bin/make -f

CFLAGS =-Wall -Drpl_malloc=malloc
STRIP =strip

DEB_HOST_GNU_TYPE ?=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH ?=$(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS +=-g
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS +=-O0
else
  CFLAGS +=-O2
endif
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  STRIP =: nostrip
endif

DIR =$(shell pwd)/debian/integrit

config: deb-checkdir config.status
config.status: configure
	CC='$(CC)' CFLAGS='$(CFLAGS)' sh ./configure \
	  --host='$(DEB_HOST_GNU_TYPE)' \
	  --build='$(DEB_BUILD_GNU_TYPE)' \
	  --prefix=/usr \
	  --mandir='$${prefix}/share/man' \
	  --infodir='$${prefix}/share/info'

build: deb-checkdir build-stamp
build-stamp: config.status
	-gcc -v
	$(MAKE)
	$(MAKE) utils
	$(MAKE) -Cdoc html
	touch build-stamp

clean: deb-checkdir deb-checkuid
	-$(MAKE) distclean
	rm -f build-stamp
	rm -rf '$(DIR)'
	rm -f debian/files debian/substvars changelog

install: deb-checkdir deb-checkuid build-stamp
	rm -rf '$(DIR)'
	# bin sbin
	install -d -m0755 '$(DIR)'/usr/bin
	install -d -m0755 '$(DIR)'/usr/sbin
	install -m0500 integrit utils/i-viewdb '$(DIR)'/usr/sbin/
	install -m0755 utils/i-ls '$(DIR)'/usr/bin/
	$(STRIP) -R .note -R .comment '$(DIR)'/usr/bin/* '$(DIR)'/usr/sbin/*
	# lib
	install -d -m0755 '$(DIR)'/var/lib/integrit/
	# etc
	install -d -m0755 '$(DIR)'/etc/integrit
	install -m0600 -oroot -groot debian/integrit.conf \
	  debian/integrit.debian.conf '$(DIR)'/etc/integrit/
	# cron
	install -d -m0755 '$(DIR)'/etc/cron.daily
	install -m0755 debian/integrit.cron.daily \
	  '$(DIR)'/etc/cron.daily/integrit
	# man
	install -d -m0755 '$(DIR)'/usr/share/man/man1
	install -m0644 doc/*.1 '$(DIR)'/usr/share/man/man1/
	gzip -9n '$(DIR)'/usr/share/man/man1/*.1
	# info
	install -d -m0755 '$(DIR)'/usr/share/info
	install -m0644 doc/integrit.info '$(DIR)'/usr/share/info/
	gzip -9n '$(DIR)'/usr/share/info/*.info
	# lintian overrides
	install -d -m0755 '$(DIR)'/usr/share/lintian/overrides
	install -m0644 debian/integrit.lintian \
	  $(DIR)/usr/share/lintian/overrides/integrit
	# upstream changelog
	rm -f changelog && ln -s Changes changelog
	# additional docs
	install -m0755 -d '$(DIR)'/usr/share/doc/integrit/etc
	install -m0644 debian/etc/*.conf '$(DIR)'/usr/share/doc/integrit/etc/

binary-indep:

binary-arch: deb-checkdir deb-checkuid install integrit.deb
	dpkg-gencontrol -P'$(DIR)'
	dpkg -b '$(DIR)' ..

binary: binary-indep binary-arch

.PHONY: config build clean binary-indep binary-arch binary install

include debian/implicit
