#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

VERSDIR=$(shell pwd)
MIDVERS=13SEP
export MIDVERS
MIDASHOME=$(shell pwd)
export MIDASHOME
MID_HOME=$(MIDASHOME)/$(MIDVERS)
MID_HOME0=/usr/lib/eso-midas/$(MIDVERS)
export MID_HOME0

MID_INSTALL=$(MIDASHOME)/$(MIDVERS)/install/unix
MID_SYS=$(MIDASHOME)/$(MIDVERS)/system/unix/
MID_WORK=$(MIDASHOME)/midwork

%:
	dh  $@

override_dh_auto_configure:
	mkdir -p $(MIDVERS)
	for i in `ls -d [a-z]* | fgrep -v debian` ; do \
	  cp -alr $$i $(MIDVERS) ; \
	done
	(cd $(MID_HOME); $(MID_INSTALL)/select all)
	(cd $(MID_HOME); $(MID_INSTALL)/preinstall -a)

override_dh_auto_build:
	(cd $(MID_HOME); $(MID_INSTALL)/install1 -a CFLAGS=\"$(CFLAGS)\" )
	test -x $(MID_HOME)/monit/midasgo.exe
	(cd $(MID_HOME)/monit ; make syskeys.unix )
	$(MID_SYS)/inmidas -m $(MID_WORK) -j "@ compile.all"
	$(MID_SYS)/inmidas -m $(MID_WORK) -j "@ ascii_bin no ; bye"
	(cd $(MID_HOME) ; yes | $(MID_SYS)/cleanmidas -a)
	sed -e "s:^MIDVERS0=.*:MIDVERS0=$(MIDVERS):" \
	    -e "s:^MIDASHOME0=.*:MIDASHOME0=/usr/lib/eso-midas:" \
	    -i $(MID_HOME)/system/unix/inmidas \
	       $(MID_HOME)/system/unix/helpmidas \
	       $(MID_HOME)/system/unix/drs

override_dh_install:
	dh_install -X.mod -XCOPYING -Xsetup~ \
	           -X${MIDVERS}/test -X${MIDVERS}/install \
	           -X${MIDVERS}/system/unix/man -X${MIDVERS}/libsrc/readline

override_dh_fixperms:
	dh_fixperms
	chmod 0644 debian/eso-midas/$(MID_HOME0)/contrib/baches/*/*.fit \
	           debian/eso-midas/$(MID_HOME0)/contrib/baches/*/*.fmt \
	           debian/eso-midas/$(MID_HOME0)/contrib/baches/*/*.datorg \
	           debian/eso-midas/$(MID_HOME0)/contrib/baches/*/*.prg \
	           debian/eso-midas/$(MID_HOME0)/contrib/baches/*/*.README \
	           debian/eso-midas/$(MID_HOME0)/system/unix/makefile
	find debian/eso-midas/$(MID_HOME0)/ -name \*.sh | xargs chmod 0755 
	chmod 0755 debian/eso-midas/$(MID_HOME0)/util/bench/brun

override_dh_makeshlibs:
	# Avoid Lintian complaint: we don't have public shared libs

override_dh_clean:
	dh_clean
	rm -rf $(MIDVERS) $(MID_WORK)
