#!/usr/bin/env bash
set -x

sudo apt install -y \
	devscripts

USER="ElmerCSC"
PROJECT="elmerfem"
LICENSE="gpl2"
SECTION="science"
PRIORITY="optional"
MAINTAINER="Junani Kataja <juhmat@gmail.com"
BUILD_DEPENDS="cmake, build-essential, liblapack-dev, libblas-dev, libblas3, libopenmpi-dev, openmpi-bin, libmumps-dev, libhypre-dev, gfortran, gcc, g++, libparmetis-dev, libmetis-dev"
DEPENDS=" libmumps-5.1.2, libhypre-2.15.1, openmpi-bin, libblas3, liblapack3, libparmetis4.0, libmetis5, libgfortran4, libopenblas-base"
HOMEPAGE="http://www.elmerfem.org/"
BUILD_HOME="/var/tmp/elmerfem-git_build"
#VERSION="2.1.0"
PACKAGE="elmerfem-csc"
ARCHITECTURE="any"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
#VERSION=${VERSION:1} # strip the preceding 'v'
#BRANCH=devel
#BRANCH=tensor_reluctivity
BRANCH=next_release
RELEASE=8.5
DEBEMAIL="juhmat@gmail.com"
DEBFULLNAME="Junani Kataja"
DESCRIPTION="Elmer multiphysics FEA package."
LONG_DESCRIPTION=" The Elmer open source mutiphysics simulation package includes physical models of fluid dynamics, structural mechanics, electromagnetics, heat transfer and acoustics, among others. These are described by partial differential equations which Elmer solves by the Finite Element Method (FEM).
 
 Elmer includes physical models of fluid dynamics, structural mechanics, electromagnetics, heat transfer and acoustics."

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
echo ${RSS_FEED}

# Make the BUILD_HOME directory and clone upstream
mkdir -p ${BUILD_HOME}
cd ${BUILD_HOME} || exit

git clone -b ${BRANCH} http://github.com/${USER}/${PROJECT}.git ${PACKAGE}-${RELEASE}

cd ${PACKAGE}-${RELEASE} || exit

mkdir -p m4

cat <<EOF >.git/gbp.conf
[DEFAULT]
# this is the upstream-branch:
upstream-branch=${BRANCH}
EOF

# debianize the BUILD_HOME directory
echo ${DEBFULLNAME}
dh_make -p ${PACKAGE}-${RELEASE} \
	--createorig \
	--single \
	--native \
	--copyright ${LICENSE} \
	--yes

# Customize the debian directory values
#sed -i 's,^\(Description: \).*,\1'${DESCRIPTION}',' debian/control
sed -i "s,^\(Description: \).*,\1${DESCRIPTION}," debian/control
sed -i 's,^\(Section: \).*,\1'${SECTION}',' debian/control
sed -i 's,^\(Priority: \).*,\1'${PRIORITY}',' debian/control
sed -i "s,^\(Maintainer: \).*,\1'${DEBFULLNAME}\ \<${DEBEMAIL}\>'," debian/control
sed -i 's,^\(Homepage: \).*,\1'${HOMEPAGE}',' debian/control
sed -i 's,^\(Architecture: \).*,\1'${ARCHITECTURE}',' debian/control
sed -i 's,^\(Build-Depends: \).*,\1'${BUILD_DEPENDS}',' debian/control
sed -i 's,^\(Depends: \).*,\1'${DEPENDS}',' debian/control
sed -i "s/.*insert\ long.*/${LONG_DESCRIPTION}/" debian/control

echo ****************
cat debian/control
echo ****************

cat <<EOF >debian/autoreconf

EOF
cat <<EOF >debian/autoreconf
eio matc elmerparam hutiter meshgen2d fem front elmergrid post
EOF

cat <<EOF >debian/libelmsolver-${RELEASE}.prerm
#!/bin/sh -e

# Remove alternatives links
if [ "$1" = "remove" ]; then
  update-alternatives --remove elmersolver /usr/lib/elmersolver-${RELEASE}
fi

#DEBHELPER#
EOF

cat <<EOF >debian/libelmsolver-${RELEASE}.postinst
#!/bin/sh -e

# Make alternatives links
if [ "$1" = "configure" ]; then
  update-alternatives --install /usr/lib/elmersolver elmersolver \
    /usr/lib/elmersolver-${RELEASE} 6100
fi

#DEBHELPER#
EOF

cat <<EOF >debian/buildopts_debian.cmake
SET(WITH_MPI TRUE CACHE BOOL "")
SET(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "")
SET(WITH_MATC TRUE CACHE BOOL "")
SET(WITH_Mumps TRUE CACHE BOOL "")
SET(WITH_Hypre TRUE CACHE BOOL "")
SET(HYPRE_INCLUDE_DIR "/usr/include/hypre" CACHE PATH "")
SET(HYPREROOT "/usr" CACHE PATH "")
SET(WITH_ElmerIce TRUE CACHE BOOL "")

SET(ELMER_SOLVER_HOME "/usr/share/elmersolver" CACHE PATH "")

# ElmerGUI related.
SET(WITH_ELMERGUI FALSE CACHE BOOL "")
SET(WITH_OCC TRUE CACHE BOOL "")
SET(WITH_VTK TRUE CACHE BOOL "")
SET(WITH_PARAVIEW TRUE CACHE BOOL "")
SET(WITH_QWT TRUE CACHE BOOL "")
SET(WITH_LUA TRUE CACHE BOOL "")
EOF

cat <<EOF >debian/rules
#!/usr/bin/make -f

BUILDDIR = build_dir

# secondly called by launchpad
build:
	mkdir $(BUILDDIR);
	cd $(BUILDDIR); cmake -C ../debian/buildopts_debian.cmake -DCMAKE_INSTALL_PREFIX=../debian/tmp/usr ..
	make -j4 -C $(BUILDDIR)

# thirdly called by launchpad
binary: binary-indep binary-arch

binary-indep:
	# nothing to be done

binary-arch:
	cd $(BUILDDIR); cmake -P cmake_install.cmake
	mkdir debian/tmp/DEBIAN
	dpkg-gencontrol -pelmerfem-csc
	dpkg --build debian/tmp ..

# firstly called by launchpad
clean:
	rm -f build
	rm -rf $(BUILDDIR)

.PHONY: binary binary-arch binary-indep clean
EOF

cat <<EOF >debian/watch
version=4
opts=filenamemangle=s/.+\/v?(\d\S+)\.tar\.gz/${PROJECT}-$1\.tar\.gz/ \
  http://github.com/${USER}/${PROJECT}/tags .*/v?(\d\S+)\.tar\.gz
  http://github.com/ElmerCSC/elmerfem/tags
  http://github.com/ElmerCSC/elmerfem/archive/scc20.tar.gz
EOF

sudo mk-build-deps --install debian/control
#sudo dpkg -i ${PACKAGE}-build-deps_${RELEASE}_all.deb

sudo rm debian/*ex debian/*EX
echo ""

sudo dpkg-buildpackage -us -uc -b

echo ${PACKAGE}_${VERSION}_amd64.deb

sudo dpkg -i ../${PACKAGE}_${VERSION}_amd64.deb
