# @configure_input@
include ../../include/config.Makefile
# we want to ignore the --datarootdir setting, whatever that means, so here we we mention it: @datarootdir@
VPATH = @srcdir@
TIDY = yes
CHOWN = chown
prefixtail=$(shell echo @prefix@ | sed 's,^/,,')
TAROPTIONS = --owner=0 --group=0 --mode=a+rX,og-ws
.PHONY: prepare

# see https://www.debian.org/doc/debian-policy/ch-controlfields.html, section 5.6.12, description of "Version"
# use it this way, for example:
#    make DEBIAN_REVISION=1
ifeq ($(DEBIAN_REVISION),)
DIST_VERSION = @PACKAGE_VERSION@
else
DIST_VERSION = @PACKAGE_VERSION@-$(DEBIAN_REVISION)
endif

############################## main targets

DEBARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH || echo none)

ifeq ($(DEBARCH),)
$(error dpkg-architecture failed)
endif

PKG_DEB = @PACKAGE_TARNAME@-$(DIST_VERSION)-$(DEBARCH)-@OS@-@ISSUE@.deb
PKG_COM_DEB = @PACKAGE_TARNAME@-$(DIST_VERSION)-common.deb

all: chk-opts macaulay2/control.prelim tidy_before ../../$(PKG_DEB) ../../$(PKG_COM_DEB) announce maybe-tidy

ifeq ($(SIGN),yes)
all: sign
else
all: no-sign
endif
sign: ../../$(PKG_DEB).sig  ../../$(PKG_COM_DEB).sig 
no-sign:; @ echo 'You may have wanted to sign the *.deb files with the command  make SIGN=yes'
%.sig:
%.sig: %; gpg --local-user Macaulay2 --detach-sign $^

chk-opts: check-DEBARCH check-prefix-is-slash-usr check-debug-disabled check-optimize-enabled 
# Debian prefers stripped binaries, but we don't:
# chk-opts: check-strip-enabled
check-DEBARCH:; if [ "$(DEBARCH)" = none ] ; then echo "error: unrecognized *.deb architecture: @ARCH@"; exit 1; fi
check-prefix-is-slash-usr:; [ "${prefix}" = /usr ]
check-debug-disabled:; [ @DEBUG@ = no ]
check-optimize-enabled:; [ @OPTIMIZE@ = yes ]
check-strip-enabled:; [ @ENABLE_STRIP@ = yes ]
macaulay2/control.prelim : @srcdir@/macaulay2/control.prelim.in Makefile
	rm -f $@
	<$< >$@ sed -e 's/[@]DEBARCH[@]/$(DEBARCH)/g' -e 's/[@]PACKAGE_VERSION[@]/$(DIST_VERSION)/g'
	! egrep -nH '@[A-Za-z_]+@' <$@
macaulay2-common/control.prelim : @srcdir@/macaulay2-common/control.prelim.in Makefile
	rm -f $@
	<$< >$@ sed -e 's/[@]DEBARCH[@]/$(DEBARCH)/g' -e 's/[@]PACKAGE_VERSION[@]/$(DIST_VERSION)/g'
	! egrep -nH '@[A-Za-z_]+@' <$@
announce:
	@ echo "============================================================================="
	@ echo "package files prepared:"
	@ echo "        $(PKG_COM_DEB)"
	@ echo "        $(PKG_DEB)"
	@ echo "    installation commands:"
	@ echo "        sudo dpkg --install $(shell cd ../..; pwd)/$(PKG_COM_DEB)"
	@ echo "        sudo dpkg --install $(shell cd ../..; pwd)/$(PKG_DEB)"
	@ echo "============================================================================="
ifeq ($(TIDY),yes)
maybe-tidy: tidy_after
else
maybe-tidy:
endif
#################################
../../$(PKG_DEB) : @srcdir@/macaulay2/debian-binary macaulay2/control.tar.gz macaulay2/data.tar.gz
	rm -f $@
	ar rc $@.tmp $^
	sed -e 's=\(debian-binary\|control.tar.gz\|data.tar.gz\)/\( *[0-9]* *\)[0-9 ]\{12\}=\1 \20     0     =g' <$@.tmp >$@
	rm $@.tmp
../../$(PKG_COM_DEB) : @srcdir@/macaulay2-common/debian-binary macaulay2-common/control.tar.gz macaulay2-common/data.tar.gz
	rm -f $@
	ar rc $@.tmp $^
	sed -e 's=\(debian-binary\|control.tar.gz\|data.tar.gz\)/\( *[0-9]* *\)[0-9 ]\{12\}=\1 \20     0     =g' <$@.tmp >$@
	rm $@.tmp

macaulay2/control.tar.gz : macaulay2/control macaulay2/postinst macaulay2/prerm
	cd macaulay2 && chmod 0644 control
	cd macaulay2 && chmod 0755 postinst prerm
	@TAR@ cfz $@ $(TAROPTIONS) -C macaulay2 control postinst prerm

macaulay2-common/control.tar.gz : macaulay2-common/control macaulay2-common/postinst macaulay2-common/prerm
	cd macaulay2-common && chmod 0644 control
	cd macaulay2-common && chmod 0755 postinst prerm
	@TAR@ cfz $@ $(TAROPTIONS) -C macaulay2-common control postinst prerm

T=@TAR@ --create --mode=a+rX,og-ws --exclude-from=@srcdir@/../tar-exclusions --file=-
prepare: .prepared
tmp@bindir@/M2@EXE@ .prepared: 
	: ensure change log is up to date...
	fgrep 'macaulay2 (@PACKAGE_VERSION@) ' <@srcdir@/changelog
	rm -rf tmp@prefix@ tmp-common@prefix@
	$(MKDIR_P) tmp@prefix@
	$(MKDIR_P) tmp-common@prefix@
	$(T) -C @pre_exec_prefix@ . | @TAR@ xfp - -C tmp@prefix@
	$(T) -C @pre_prefix@      . | @TAR@ xfp - -C tmp-common@prefix@
	if [ -d tmp-common@prefix@/info ] ; \
	then $(MKDIR_P) tmp-common@prefix@/share/info && mv tmp-common@prefix@/info/* tmp-common@prefix@/share/info && rmdir tmp-common@prefix@/info; \
	fi
	$(MKDIR_P) tmp@prefix@/@tail_mandir@
	mv -v tmp-common@prefix@/@tail_mandir@/* tmp@prefix@/@tail_mandir@
	rmdir tmp-common@prefix@/@tail_mandir@
	$(MKDIR_P) tmp@prefix@/share/doc/macaulay2
	$(MKDIR_P) tmp-common@prefix@/share/doc/macaulay2-common
	cp tmp-common@docdir@/README tmp@prefix@/share/doc/macaulay2/copyright
	cp tmp-common@docdir@/README tmp-common@prefix@/share/doc/macaulay2-common/copyright
	gzip -9 <@srcdir@/changelog >tmp@prefix@/share/doc/macaulay2/changelog.Debian.gz
	gzip -9 <@srcdir@/changelog >tmp-common@prefix@/share/doc/macaulay2-common/changelog.Debian.gz
	@FIND@ tmp-common -name \*.info | while read x ; do gzip --best "$$x" ; done
	@FIND@ tmp tmp-common -name man -type d | while read x ; do @FIND@ $$x -type f -not -name \*.gz | while read y ; do gzip --best "$$y" ; done ; done
	@FIND@ tmp -name \*.so -o -name \*.so.\* -o -name \*.la | xargs chmod a-x Makefile
	@FIND@ tmp tmp-common -depth -mindepth 1 -type d -empty -delete
	touch $@
macaulay2/data.tar.gz        : prepare; (cd tmp        && @TAR@ cfz - $(TAROPTIONS) *) >$@
macaulay2-common/data.tar.gz : prepare;	(cd tmp-common && @TAR@ cfz - $(TAROPTIONS) *) >$@
macaulay2/md5sums            : prepare; (cd tmp        && @FIND@ * -type f | xargs md5sum) >$@
macaulay2-common/md5sums     : prepare;	(cd tmp-common && @FIND@ * -type f | xargs md5sum) >$@
ifneq (@LINTIAN@,)
SUP = fgrep -v 
check: check1 check2
check1:; - @LINTIAN@ -I ../../$(PKG_DEB)
check2:; - @LINTIAN@ -I ../../$(PKG_COM_DEB)
else
check:
	@echo "--warning: 'lintian' not installed, not checking *.deb packages" >&2
endif
tidy_before:
	rm -rf .prepared tmp tmp-common
	rm -rf macaulay2/control.tar.gz macaulay2/data.tar.gz macaulay2/md5sums
	rm -rf macaulay2-common/control.tar.gz macaulay2-common/data.tar.gz macaulay2-common/md5sums
tidy tidy_after :
	rm -rf .prepared tmp tmp-common libraries-really-used
	rm -rf macaulay2/control.tar.gz macaulay2/data.tar.gz macaulay2/md5sums
	rm -rf macaulay2-common/control.tar.gz macaulay2-common/data.tar.gz macaulay2-common/md5sums
clean:: tidy
	rm -rf ../../$(PKG_DEB) ../../$(PKG_COM_DEB)
libraries-used-directly: .prepared tmp@bindir@/M2@EXE@
	objdump -x tmp@bindir@/M2@EXE@ | grep NEEDED | sed 's/ *NEEDED *//' | sort >$@
clean::; rm -f libraries-used-directly
libraries-used: .prepared tmp@bindir@/M2@EXE@
	for i in tmp@bindir@/M2@EXE@ tmp@programsdir@/* ;\
	do if [ -f $$i ] ; then ( set -x ; ldd $$i ) ; fi ; \
	done \
	| sed -e 's/.* => //' -e 's/^	//' -e 's/ .*//' \
	| egrep '^/' \
	| sort | uniq \
	> $@ || true
clean::; rm -f libraries-used
libraries-really-used: libraries-used libraries-used-directly
	egrep `echo \`cat libraries-used-directly\` | sed -e 's=^=.*/=' -e 's= =|.*/=g'` <$< >$@
files-used: Makefile
	for i in @FILE_PREREQS@ ; do echo $$i ; done >$@
packages-used: libraries-really-used files-used
	cat $^ |								\
	while read x ;								\
	x=`readlink -f "$$x"` ;							\
	do @FIND@ /var/lib/dpkg/info -name \*.list | xargs egrep -l "^$$x";	\
	done | sort | uniq | sed -r -e 's=.*/==' -e 's=(:.*)?\.list$$==' >$@
clean::; rm -f packages-used
macaulay2/control : macaulay2/control.prelim macaulay2/data.tar.gz packages-used
	(cat $< &&								\
	 size=`gunzip<macaulay2/data.tar.gz|wc -c` &&				\
	 size=`echo $$size | sed 's/^\([0-9][0-9]*\)[0-9][0-9][0-9].*/\1/'` &&	\
	 echo Installed-Size: $$size &&						\
	 echo -n "Depends: macaulay2-common (= $(DIST_VERSION))" &&		\
	 cat packages-used | while read x ; do echo -n ", $$x" ; done &&	\
	 echo									\
	) >$@
clean::; rm -f macaulay2/control
macaulay2-common/control : macaulay2-common/control.prelim macaulay2-common/data.tar.gz
	(cat $< &&								\
	 size=`gunzip<macaulay2-common/data.tar.gz|wc -c` &&			\
	 size=`echo $$size | sed 's/^\([0-9][0-9]*\)[0-9][0-9][0-9].*/\1/'` &&	\
	 echo Installed-Size: $$size						\
	) >$@
clean::; rm -f macaulay2-common/control
distclean: clean; rm -f Makefile control macaulay2/control.prelim macaulay2-common/control.prelim
Makefile : Makefile.in; cd ../..; ./config.status distributions/deb/$@

# Local Variables:
# compile-command: "make -C $M2BUILDDIR/distributions/deb "
# End:
.NOTPARALLEL:
