
all:

DESTDIR=
PREFIX=$(DESTDIR)/usr
BINDIR=$(PREFIX)/bin
MANDIR=$(PREFIX)/share/man
LOCALSTATEDIR=$(DESTDIR)/var
SYSCONFDIR=$(DESTDIR)/etc
UNITDIR=$(DESTDIR)/lib/systemd/system
# SYSTEM_METHOD is either SYSTEMD or ANACRON
# SYSTEMD shoudl work on Fedora 21 or REHL7/CentOS7 or later
SYSTEM_METHOD=SYSTEMD
PROGRAMS=\
	babarchive_check_all \
	babarchive_check_one \
	babarchive_prep_directories \
	babarchive_prep_one \
	babarchive_prep_topshasum
MAN1PAGES=\
	babarchive_check_all.1 \
	babarchive_check_one.1 \
	babarchive_prep_directories.1 \
	babarchive_prep_one.1 \
	babarchive_prep_topshasum.1
MAN8PAGES=babarchive.8

%.1: %.md
	pandoc --standalone --to man $< -o $@

%.8: %.md
	pandoc --standalone --to man $< -o $@

all: $(MANPAGES)

install: install_programs install_manpages

install_programs:
	-mkdir -p $(BINDIR)
	cp $(PROGRAMS) $(BINDIR)/$(PROGRAM)
	for p in $(PROGRAMS); do chmod u=rwx,go-rx $(BINDIR)/$$p; done
	-mkdir -p $(LOCALSTATEDIR)/spool/babarchive

install_manpages:  $(MANPAGES)
	-mkdir -p $(MANDIR)/man1
	for i in $(MAN1PAGES); do gzip < $$i >$(MANDIR)/man1/$$i.gz; done
	-mkdir -p $(MANDIR)/man8
	for i in $(MAN8PAGES); do gzip < $$i >$(MANDIR)/man8/$$i.gz; done

install_anacron:
	-mkdir -p $(SYSCONFDIR)/cron.daily
	cp babarchive.cron $(SYSCONFDIR)/cron.daily
	chmod u=rwx,go-rx $(SYSCONFDIR)/cron.daily

install_systemd:
	-mkdir -p $(UNITDIR)
	for p in babarchive.service babarchive.timer; do cp $$p $(UNITDIR); chmod u=rw,go=r $(UNITDIR)/$$p; done




#
# our simple test suite
#
# these tests are overly strict:
# We complain about dates and directories sizes that change
# when both happen through "normal" actions.
#
# Need to improve.
#
# Omit test_check because of fails on subdir and index.html dates for this reason.
test: test_prep

normalize_sample_out_dates:
	find sample.out -print | xargs touch -t '199701121200.00'

rebuild_sample_out:
	test -d sample.out && rm -rf sample.out
	-mkdir sample.out
	cp -rp sample.in/. sample.out/.
	find sample.out -print | xargs touch -t '199701121200.00'
	./babarchive_prep_one sample.out


test_check: normalize_sample_out_dates
	./babarchive_check_one sample.out && echo "babarchive_check_one works"

# Note that the ls-lR doesn't match, so we remove it from the diff.
test_prep:
	test -d sample.trial && rm -rf sample.trial || true
	mkdir sample.trial
	cp -rp sample.in/. sample.trial/.
	./babarchive_prep_one sample.trial
	test -d sample.out_clean && rm -rf sample.out_clean || true
	mkdir sample.out_clean
	cp -rp sample.out/. sample.out_clean/.
	rm sample.out_clean/ls-lR sample.trial/ls-lR
	for f in sample.out_clean/.shasum.fsdb sample.trial/.shasum.fsdb sample.out_clean/.shasum sample.trial/.shasum; do  <$$f grep -v ls-lR >$$f+; mv $$f+ $$f; done
	diff -u -r sample.out_clean sample.trial && echo "babarchive_prep_one works"



#
# packaging
#

RPM_DIST=$(shell rpm --eval '%{dist}')
VERSION=$(shell cat VERSION)

tar.gz: VERSION babarchive.spec.in $(MAN1PAGES) $(MAN8PAGES)
	ln -s . babarchive-$(VERSION)
	tar cvzf babarchive-$(VERSION).tar.gz \
		babarchive-$(VERSION)/README \
		babarchive-$(VERSION)/CHANGES.md \
		babarchive-$(VERSION)/COPYING \
		babarchive-$(VERSION)/VERSION \
		babarchive-$(VERSION)/Makefile \
		babarchive-$(VERSION)/babarchive_check_all \
		babarchive-$(VERSION)/babarchive_check_one \
		babarchive-$(VERSION)/babarchive_prep_directories \
		babarchive-$(VERSION)/babarchive_prep_one \
		babarchive-$(VERSION)/babarchive_prep_topshasum \
		babarchive-$(VERSION)/babarchive.cron \
		babarchive-$(VERSION)/babarchive.service \
		babarchive-$(VERSION)/babarchive.timer \
		babarchive-$(VERSION)/babarchive.spec.in \
		babarchive-$(VERSION)/sample.in \
		babarchive-$(VERSION)/sample.out \
		babarchive-$(VERSION)/babarchive_check_all.1 \
		babarchive-$(VERSION)/babarchive_check_one.1 \
		babarchive-$(VERSION)/babarchive_prep_directories.1 \
		babarchive-$(VERSION)/babarchive_prep_one.1 \
		babarchive-$(VERSION)/babarchive_prep_topshasum.1 \
		babarchive-$(VERSION)/babarchive.8
	rm babarchive-$(VERSION)

#
# fedora packaging
#

babarchive.spec: babarchive.spec.in VERSION
	<babarchive.spec.in sed -e 's/@VERSION@/'$(VERSION)'/g' -e 's/^@@'$(SYSTEM_METHOD)'://' -e '/^@@/d' >$@

babarchive_anacron.spec: babarchive.spec.in VERSION
	<babarchive.spec.in sed -e 's/@VERSION@/'$(VERSION)'/g' -e 's/^@@ANACRON://' -e '/^@@/d' >$@

rpms: babarchive.spec VERSION
	cp babarchive-$(VERSION).tar.gz $$HOME/rpmbuild/SOURCES
	cp babarchive.spec $$HOME/rpmbuild/SPECS
	( cd $$HOME/rpmbuild; rpmbuild --sign -ba SPECS/babarchive.spec; )
	cp $$HOME/rpmbuild/RPMS/noarch/babarchive-$(VERSION)-1$(RPM_DIST).noarch.rpm .
	cp $$HOME/rpmbuild/SRPMS/babarchive-$(VERSION)-1$(RPM_DIST).src.rpm .

rpmlint:
	rpmlint babarchive-$(VERSION)-1$(RPM_DIST).noarch.rpm babarchive-$(VERSION)-1$(RPM_DIST).src.rpm

release_man:
	for i in $(MAN1PAGES); do pandoc  -t html -o $(INTERNAL_WEBSITE)/$$i.html `basename $$i .1`.md; done
	for i in $(MAN8PAGES); do pandoc  -t html -o $(INTERNAL_WEBSITE)/$$i.html `basename $$i .8`.md; done


INTERNAL_WEBSITE=../../../../ANT/WWW/ant_2015/software/babarchive
release:
	@echo "You should have done: \"make tar.gz; make rpms; make rpmlint\""
	cp babarchive-$(VERSION).tar.gz babarchive-$(VERSION)-1$(RPM_DIST).src.rpm $(INTERNAL_WEBSITE)
	( cd $(INTERNAL_WEBSITE); git add babarchive-$(VERSION).tar.gz babarchive-$(VERSION)-1$(RPM_DIST).src.rpm; )
	@echo "now (edit _data/software.yaml); git tag -s -u 0x4c6cd6f2 -m 'babarchive-$(VERSION) release' babarchive-$(VERSION); cd $(INTERNAL_WEBSITE); git commit -m 'release $(VERSION)'; git push;"

