#!/usr/bin/make -f

STRIP =strip
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  STRIP =: nostrip
endif

CFLAGS =-g -O2 -Wall
LDFLAGS =
CC =gcc
BGLIBS =/usr/lib/bglibs
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS =-g -O0 -Wall
endif
ifneq (,$(findstring diet,$(DEB_BUILD_OPTIONS)))
  CC =diet -v -Os gcc -nostdinc
  BGLIBS =/usr/lib/diet/bglibs
endif

DIR =$(shell pwd)/debian/bcron

patch: deb-checkdir patch-stamp
patch-stamp:
	for i in `ls -1 debian/diff/*.diff || :`; do \
	  patch -p1 <$$i || exit 1; \
	done
	touch patch-stamp

build: deb-checkdir build-arch-stamp build-indep-stamp

build-arch: deb-checkdir build-arch-stamp
build-arch-stamp: patch-stamp
	-gcc -v
	for i in conf-*; do \
	  test -e $${i%'{orig}'}'{orig}' || cp -v $$i $$i'{orig}'; \
	done
	echo '$(CC) $(CFLAGS)' >conf-cc
	echo '$(CC) $(LDFLAGS)' >conf-ld
	echo '/usr/sbin' >conf-bin
	echo '/usr/share/man' >conf-man
	echo '$(BGLIBS)/include' >conf-bgincs
	echo '$(BGLIBS)/lib' >conf-bglibs
	$(MAKE)
	sh tests.sh
	touch build-arch-stamp

build-indep: deb-checkdir build-indep-stamp
build-indep-stamp: 
	touch build-indep-stamp

clean: deb-checkdir deb-checkuid patch-stamp
	$(MAKE) clean
	test ! -e patch-stamp || \
	  for i in `ls -1r debian/diff/*.diff || :`; do patch -p1 -R <$$i; done
	for i in `ls *'{orig}' || :`; do mv -vf $$i $${i%'{orig}'}; done
	rm -f build-arch-stamp build-indep-stamp patch-stamp
	rm -rf '$(DIR)' '$(DIR)'-run
	rm -f debian/files debian/substvars changelog

install: install-arch install-indep
install-arch: deb-checkdir deb-checkuid build-arch-stamp
	rm -rf '$(DIR)'
	install -d -m0755 '$(DIR)'/usr/bin '$(DIR)'/usr/sbin
	PATH="/usr/lib/bglibs/bin:$$PATH" make install install_prefix='$(DIR)'
	rm -f '$(DIR)'/usr/sbin/crontab
	$(STRIP) -R .comment -R .note '$(DIR)'/usr/sbin/*
	mv '$(DIR)'/usr/sbin/bcrontab '$(DIR)'/usr/bin/bcrontab
	install -d -m0755 '$(DIR)'/etc/bcron
	mv '$(DIR)'/usr/share/man/man5/crontab.5 \
	  '$(DIR)'/usr/share/man/man5/bcrontab.5
	rm -f '$(DIR)'/usr/share/man/man1/crontab.1
	gzip -9 '$(DIR)'/usr/share/man/man?/*.?
	test -r changelog || ln -s NEWS changelog

install-indep: deb-checkdir deb-checkuid build-indep-stamp
	rm -rf '$(DIR)'-run
	install -d -m0755 '$(DIR)'-run/usr/bin
	ln -s bcrontab '$(DIR)'-run/usr/bin/crontab
	install -d -m0755 '$(DIR)'-run/var/spool/cron/crontabs
	install -d -m0755 '$(DIR)'-run/var/spool/cron/tmp
	install -d -m0755 '$(DIR)'-run/etc/cron.d
	install -d -m0755 '$(DIR)'-run/etc/bcron-sched/log
	install -m0755 debian/etc/bcron-sched/run \
	  '$(DIR)'-run/etc/bcron-sched/run
	install -m0755 debian/etc/bcron-sched/log/run \
	  '$(DIR)'-run/etc/bcron-sched/log/run
	install -d -m0755 '$(DIR)'-run/etc/bcron-spool/log
	install -m0755 debian/etc/bcron-spool/run \
	  '$(DIR)'-run/etc/bcron-spool/run
	install -m0755 debian/etc/bcron-spool/log/run \
	  '$(DIR)'-run/etc/bcron-spool/log/run
	install -d -m0755 '$(DIR)'-run/etc/bcron-update/log
	install -m0755 debian/etc/bcron-update/run \
	  '$(DIR)'-run/etc/bcron-update/run
	install -m0755 debian/etc/bcron-update/log/run \
	  '$(DIR)'-run/etc/bcron-update/log/run
	install -d -m0755 '$(DIR)'-run/var/log/bcron/spool
	install -d -m0755 '$(DIR)'-run/var/log/bcron/update
	install -m0644 debian/crontab '$(DIR)'-run/etc/crontab
	install -d -m0755 '$(DIR)'-run/etc/cron.daily
	install -d -m0755 '$(DIR)'-run/etc/cron.hourly
	install -d -m0755 '$(DIR)'-run/etc/cron.monthly
	test -r changelog || ln -s NEWS changelog
	# man pages
	install -d -m0755 '$(DIR)'-run/usr/share/man/man5
	ln -s bcrontab.5.gz '$(DIR)'-run/usr/share/man/man5/crontab.5.gz
	install -d -m0755 '$(DIR)'-run/usr/share/man/man1
	ln -s bcrontab.1.gz '$(DIR)'-run/usr/share/man/man1/crontab.1.gz

binary-arch: deb-checkdir deb-checkuid install-arch bcron.deb
	test '$(CC)' != 'gcc' || \
	  dpkg-shlibdeps '$(DIR)'/usr/bin/* '$(DIR)'/usr/sbin/*
	dpkg-gencontrol -isp -pbcron -P'$(DIR)'
	dpkg -b '$(DIR)' ..

binary-indep: deb-checkdir deb-checkuid install-indep bcron-run.deb
	dpkg-gencontrol -isp -pbcron-run -P'$(DIR)'-run
	dpkg -b '$(DIR)'-run ..

binary: binary-indep binary-arch

.PHONY: patch build build-arch build-indep clean install install-arch \
install-indep binary-arch binary-indep binary

include debian/implicit
