#!/usr/bin/make -f

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
XXBITS ?= $(shell getconf LONG_BIT)
HOSTNAME ?= $(shell hostname)

PYVERS := $(shell pyversions -vr debian/control)


STA_VERSION=STA10.6
PTA_VERSION=STA10.6_mpi
ASTER_VERSION=10.6.0
ASTER_VERSION_FULL=$(ASTER_VERSION)-3

DEB_SOURCE_PACKAGE := aster
DEB_UPSTREAM_VERSION := $(ASTER_VERSION)-1

ASTER_ROOT=$(shell echo `grep "export ASTER_ROOT=" /etc/codeaster/profile.sh | sed 's/export ASTER_ROOT=//'`)
export ASTER_ROOT
export DEB_DESTDIR=$(CURDIR)/debian/tmp

ASTOUT_EXPORT=$(STA_VERSION)/astout.export

build: build-stamp
build-stamp: 
	@echo "ASTER_ROOT=$(ASTER_ROOT)"
	mkdir -p $(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION)/dtag
	cp -r $(STA_VERSION) $(DEB_DESTDIR)/$(ASTER_ROOT)
	mkdir -p $(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION)/dtag
	cp -r $(STA_VERSION)/* $(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION)
# Export file replacements for running tests 
	sed -i 's:?ASTER_ROOT?:$(ASTER_ROOT):' $(DEB_DESTDIR)/$(ASTER_ROOT)/$(ASTOUT_EXPORT)
	sed -i 's:?ASTER_VERSION?:$(STA_VERSION):' $(DEB_DESTDIR)/$(ASTER_ROOT)/$(ASTOUT_EXPORT)
# Use the short test list without homard by default
	sed -i '/liste_internet/ s/^/#/' $(DEB_DESTDIR)/$(ASTER_ROOT)/$(ASTOUT_EXPORT)
	sed -i 's:#\(.*liste_short.*\):#\1\n\1:' $(DEB_DESTDIR)/$(ASTER_ROOT)/$(ASTOUT_EXPORT)
	sed -i '/^F/ s:liste_short:&_without_homard:' $(DEB_DESTDIR)/$(ASTER_ROOT)/$(ASTOUT_EXPORT)
# Results will be in /tmp for writing permission
	sed -i '/resu_test/ s:$(ASTER_ROOT)/$(STA_VERSION):/tmp:' $(DEB_DESTDIR)/$(ASTER_ROOT)/$(ASTOUT_EXPORT)

# Remove extra license file
	rm -f $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/LICENSE.TERMS
	rm -f $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/GPL.txt

# Remove UMAT binaries from test cases
	rm -f $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/astest/umat001a.44
	rm -f $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/astest/umat001b.44
	rm -f $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/astest/umat002a.44
	rm -f $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/astest/umat002a.46

# Add as_test.sh 
	mkdir -p $(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION)/astest
	cp debian/as_test.sh $(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION)/astest
	chmod 755 $(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION)/astest/as_test.sh
	touch $@


clean:
	dh_testdir
	dh_testroot
# Default cleaning
	rm -f setup.cache setup.dbg* setup.log*
	rm -f variables.init cfg___main__ cfg_aster
	-find . -name "*.pyc" -exec rm {} \;
	-rm -rf *-stamp
# Removing build and install tree
	rm -rf $(DEB_DESTDIR)
	dh_clean

install: build
	dh_testdir
	dh_testroot
#
	HOME=$(DEB_DESTDIR); \
	set -ex; \
	for v in $(PYVERS); do \
	   cp -f debian/profile.sh $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/profile_py$$v.sh; \
	   perl -pi -e "s|HOSTNAME|$(HOSTNAME)|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|PYVER|$$v|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/profile_py$$v.sh; \
	   cp -f debian/config_$(XXBITS).txt  $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|PYVER|$$v|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|DEB_DESTDIR|$(DEB_DESTDIR)|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|ASTER_ROOT|$(ASTER_ROOT)|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   python$$v -c 'import sys, asrun ; from asrun.main import main,start; del sys.argv[0]; main()' /usr/bin/as_run --make --vers=$(STA_VERSION) --destdir=$(DEB_DESTDIR) --config=$(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   find $(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION) -name \*.o | xargs rm; \
	   perl -pi -e "s|$(DEB_DESTDIR)||g" $(DEB_DESTDIR)/$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|$(HOSTNAME)|HOSTNAME|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	done

	mkdir -p $(DEB_DESTDIR)/$(ASTER_ROOT)/outils
	ln -sf /usr/bin/onmetis $(DEB_DESTDIR)/$(ASTER_ROOT)/outils
	ln -sf /usr/bin/onmetis.exe $(DEB_DESTDIR)/$(ASTER_ROOT)/outils

#
# For MPI version
	HOME=$(DEB_DESTDIR); \
	set -ex; \
	for v in $(PYVERS); do \
	   cp -f debian/profile.sh $(DEB_DESTDIR)$(ASTER_ROOT)/$(PTA_VERSION)/profile_py$$v.sh; \
	   perl -pi -e "s|HOSTNAME|$(HOSTNAME)|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|PYVER|$$v|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(PTA_VERSION)/profile_py$$v.sh; \
	   cp -f debian/config-mpi_$(XXBITS).txt  $(DEB_DESTDIR)$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|PYVER|$$v|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|DEB_DESTDIR|$(DEB_DESTDIR)|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|ASTER_ROOT|$(ASTER_ROOT)|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   python$$v -c 'import sys, asrun ; from asrun.main import main,start; del sys.argv[0]; main()' /usr/bin/as_run --make clean --vers=$(PTA_VERSION) --destdir=$(DEB_DESTDIR) --config=$(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   mkdir -p $(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION)/dtag; \
	   python$$v -c 'import sys, asrun ; from asrun.main import main,start; del sys.argv[0]; main()' /usr/bin/as_run --make --vers=$(PTA_VERSION) --destdir=$(DEB_DESTDIR) --config=$(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   find $(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION) -name \*.o | xargs rm; \
	   perl -pi -e "s|$(DEB_DESTDIR)||g" $(DEB_DESTDIR)/$(ASTER_ROOT)/$(PTA_VERSION)/config-py$$v.txt; \
	   perl -pi -e "s|$(HOSTNAME)|HOSTNAME|g" $(DEB_DESTDIR)$(ASTER_ROOT)/$(STA_VERSION)/config-py$$v.txt; \
	done
#
# 4. change mumps_scotch to mumps_ptscotch?
# 5. disable openmp?

#
	find . -name "*.pyc" -exec rm {} \;
	find . -type d -empty -delete
	dh_install --sourcedir=debian/tmp --list-missing

# Build architecture-independent files here.
binary-indep: build
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_installchangelogs -i

#install aster-test
	dh_install --sourcedir=debian/tmp -pcode-aster-test
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_installchangelogs -a
	dh_strip -pcode-aster-engine --dbg-package=code-aster-engine-dbg
	dh_strip -pcode-aster-mpi-engine --dbg-package=code-aster-mpi-engine-dbg
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_pysupport -pcode-aster-engine
	dh_pysupport -pcode-aster-mpi-engine
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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

get-orig-source:
# Retreive aster-full-src-$(ASTER_VERSION_FULL).noarch.tar.gz
	mkdir -p tmp
	cd tmp && wget "http://www.code-aster.org/FICHIERS/aster-full-src-$(ASTER_VERSION_FULL).noarch.tar.gz"

# Go to SRC directory and retreive aster source
	cd tmp && tar xzf aster-full-src-$(ASTER_VERSION_FULL).noarch.tar.gz
	cd tmp/aster-full-src-$(ASTER_VERSION)/SRC && tar zxvf $(DEB_SOURCE_PACKAGE)-src-$(DEB_UPSTREAM_VERSION).noarch.tar.gz
	mkdir -p tmp/aster-full-src-$(ASTER_VERSION)/SRC/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)
	cd tmp/aster-full-src-$(ASTER_VERSION)/SRC && mv $(STA_VERSION) $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)
	cd tmp/aster-full-src-$(ASTER_VERSION)/SRC && tar zcvf $(CURDIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)

	rm -rf tmp
