#!/usr/bin/env bash
# http://github.com/FreeCAD/FreeCAD/releases/
# debian is still on 0.16 but 0.17 is needed for current workbenches

# the ppa has calculix-ccx eigen3 freecad-daily (0.18~beta1) opencascade
# ppa:freecad-maintainers/freecad-daily

# sudo add-apt-repository ppa:freecad-maintainers/freecad-daily
# sudo apt update
#            groovy focal eoan disco cosmic bionic artful xenial trusty
# daily:              *    *                   *           *
# stable:       *     *    *                   *           *
# community:               *            *      *           *
# elmer:        *     *           *            *           *       *

# YOUR_UBUNTU_VERSION_HERE=noble # 24.04 LTS buster/sid
YOUR_UBUNTU_VERSION_HERE=mantic # 22.04 LTS buster/sid
# YOUR_UBUNTU_VERSION_HERE=jammy # 22.04 LTS buster/sid
# YOUR_UBUNTU_VERSION_HERE=focal # 20.04 LTS buster/sid

sudo bash -c "cat > /etc/apt/sources.list-available/freecad-daily.list" <<EOF
deb http://ppa.launchpad.net/freecad-maintainers/freecad-daily/ubuntu $YOUR_UBUNTU_VERSION_HERE main 
deb-src http://ppa.launchpad.net/freecad-maintainers/freecad-daily/ubuntu $YOUR_UBUNTU_VERSION_HERE main 
EOF
sudo ln -sf /etc/apt/sources.list-available/freecad-daily.list /etc/apt/sources.list.d/freecad-daily.list

# Launchpad PPA for FreeCAD maintainers
#                       1024R/83193AA3B52FF6FCF10A1BBF005EAE8119BB5BCA
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 19BB5BCA
sudo bash -c "cat > /etc/apt/sources.list-available/freecad-stable.list" <<EOF
deb http://ppa.launchpad.net/freecad-maintainers/freecad-stable/ubuntu $YOUR_UBUNTU_VERSION_HERE main 
deb-src http://ppa.launchpad.net/freecad-maintainers/freecad-stable/ubuntu $YOUR_UBUNTU_VERSION_HERE main 
EOF
sudo ln -sf /etc/apt/sources.list-available/freecad-stable.list /etc/apt/sources.list.d/freecad-stable.list

sudo bash -c "cat > /etc/apt/sources.list-available/freecad-community.list" <<EOF
deb http://ppa.launchpad.net/freecad-community/ppa/ubuntu $YOUR_UBUNTU_VERSION_HERE main 
deb-src http://ppa.launchpad.net/freecad-community/ppa/ubuntu $YOUR_UBUNTU_VERSION_HERE main
EOF
sudo ln -sf /etc/apt/sources.list-available/freecad-community.list /etc/apt/sources.list.d/freecad-community.list
#                       4096R/D2A62B5CEA1A30BBE1E44B561CDA5240F1B6CE9D
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F1B6CE9D

sudo bash -c "cat > /etc/apt/sources.list-available/elmer.list" <<EOF
deb http://ppa.launchpad.net/elmer-csc-ubuntu/elmer-csc-ppa/ubuntu $YOUR_UBUNTU_VERSION_HERE main 
deb-src http://ppa.launchpad.net/elmer-csc-ubuntu/elmer-csc-ppa/ubuntu $YOUR_UBUNTU_VERSION_HERE main
EOF
sudo ln -sf /etc/apt/sources.list-available/elmer.list /etc/apt/sources.list.d/elmer.list
#                       4096R/1FE4A88ACFEE8388A409F23A89358ABF9FB7E178
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9FB7E178

sudo apt update

# If building, use cmake gui for configuring the build.
sudo apt install -y cmake-qt-gui python3-numpy vtk9 python3-vtk9 z88

sudo apt install -y freecad-daily calculix-ccx elmerfem-csc

### Netgen
##sudo ln -s /etc/apt/sources.list-available/devuan-ceres.list /etc/apt/sources.list.d/devuan-ceres.list
##sudo apt update
sudo apt install -y netgen python3-netgen
#
#cd /usr/src
##sudo apt source netgen
##http://http.debian.net/debian/pool/main/n/netgen/netgen_4.9.13.dfsg-11.dsc
##http://http.debian.net/debian/pool/main/n/netgen/netgen_4.9.13.dfsg.orig.tar.gz
##http://http.debian.net/debian/pool/main/n/netgen/netgen_4.9.13.dfsg-11.debian.tar.xz
#
#sudo gbp import-dsc -v --allow-unauthenticated http://http.debian.net/debian/pool/main/n/netgen/netgen_4.9.13.dfsg-11.dsc
#
#cd netgen
#sudo gbp dch --snapshot --auto
#
#sudo gbp buildpackage --git-pbuilder --git-arch=amd64 --git-dist=ascii --git-ignore-branch
##sudo gbp buildpackage --git-pbuilder --git-arch=amd64 --git-dist=ascii --no-pristine-tar
##sudo gbp import-orig --upstream-version=0.1.6 --pristine-tar https://sigrok.org/download/source/sigrok-firmware-fx2lafw/sigrok-firmware-fx2lafw-0.1.6.tar.gz
##sudo gbp buildpackage --git-pbuilder --git-arch=amd64 --git-dist=ascii --othermirror="deb [trusted=yes] file:///srv/pbuilder/deps ./" --distribution=ascii --architecture=amd64
##
## Builds the folloeing:
##libnglib-4.9.13_4.9.13.dfsg-11_amd64.deb
##libnglib-4.9.13-dbgsym_4.9.13.dfsg-11_amd64.deb
##libnglib-dev_4.9.13.dfsg-11_amd64.deb
##netgen_4.9.13.dfsg-11_amd64.deb
##netgen-dbgsym_4.9.13.dfsg-11_amd64.deb
##netgen-doc_4.9.13.dfsg-11_all.deb
##netgen-headers_4.9.13.dfsg-11_amd64.deb
#
## The would be installed by apt install -f later but let be explicite.
#sudo apt install \
#  tix \
#  liboce-ocaf-lite10 \
#  liboce-ocaf10 \
#  liboce-visualization10 \
#  libtogl1
#
#
#sudo dpkg -i ../libnglib-4.9.13_4.9.13.dfsg-11_amd64.deb &&  dpkg -i ../netgen_4.9.13.dfsg-11_amd64.deb
#
#sudo apt install -y -f

#sudo rm /etc/apt/sources.list.d/devuan-ceres.list && apt update

# opencascade in experimental
# https://ftp-master.debian.org/new/opencascade_7.2.0+dfsg1-1.html

# http://github.com/qingfengxia/FenicsSolver

# Available as addon in freecad interface
# http://github.com/jaheyns/CfdFoam

sudo apt install -y python3-gmsh gmsh
sudo apt install -y gnuplot paraview python3-paraview openfoam kicad-packages3d

# Openfoam CFD
# http://github.com/qingfengxia/Cfd

# https://forum.freecadweb.org/viewtopic.php?t=21576
# https://opensimsa.github.io/training.htmlGnuplot, Paraview and OpenFOAM
# http://github.com/jaheyns/CfdOF
# Requirements:
# *   Latest release version of FreeCAD (0.18) or latest development version (0.19 prerelease)
# **  or latest development version (0.18 prerelease; requires git commit 12539 or later)
# *   OpenFOAM (versions 4.x and 5.x tested; other versions may work.) ascii has 4.1+dfsg1-1
# *   OpenFOAM Foundation versions 5-8 or ESI-OpenCFD versions 1706-2006
# *   Paraview - ascii has 5.1.2+dfsg1-2
# *   GMSH (version 2.13 or later) ascii has 2.15
# *   cfMesh (version 1.1.2 updated to compile with OpenFOAM v5.x)
# *   FreeCAD plot workbench
# *   HiSA (High Speed Aerodynamic Solver)
# ** https://gitlab.io
# ** https://gitlab.com/hisa/hisa

#NOW=`date +"%Y-%m-%d"`
#cd /tmp
#wget -c https://downloads.sourceforge.net/project/cfmesh-cfdof/cfmesh-cfdof.zip
#wget -c -O cfmesh-cfdof.zip https://downloads.sourceforge.net/project/cfmesh-cfdof/cfmesh-cfdof.zip?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Fcfmesh-cfdof%2Ffiles%2Fcfmesh-cfdof.zip%2Fdownload&ts=1603110024
#unzip -d ./cfmesh-cfdof-${NOW} cfmesh-cfdof.zip
#
# If sourceforge doesn't work: https://develop.openfoam.com/Community/integration-cfmesh
#git clone https://git.code.sf.net/p/cfmesh-cfdof/cfmesh cfmesh-cfdof-cfmesh cfmesh-cfdof-${NOW}
#cd cfmesh-cfdof-${NOW}/ && \
#	sudo checkinstall \
#	./Allwmake 2>&1 | tee ./make.log \
#	wmake libso meshLibrary \
#	wmake all executables \
#	wmake all utilities

# Build elmer from git
apt update -o Acquire::CompressionTypes::Order::=gz && apt upgrade -y && apt install -y \
	build-essential \
	cmake \
	git \
	libblas-dev \
	liblapack-dev \
	libmumps-dev \
	libparmetis-dev \
	mpich \
	sudo \
	less

# Clone the ElmerIce source code and compile Elmer/Ice
#RUN git clone git://www.github.com/ElmerCSC/elmerfem -b elmerice elmerice \
#	&& mkdir elmerice/builddir \
#	&& cd elmerice/builddir \
#	&& cmake /home/elmerice \
#		-DCMAKE_INSTALL_PREFIX=/usr/local/Elmer-devel \
#		-DCMAKE_C_COMPILER=/usr/bin/gcc \
#		-DCMAKE_Fortran_COMPILER=/usr/bin/gfortran \
#		-DWITH_MPI:BOOL=TRUE -DWITH_Mumps:BOOL=TRUE \
#		-DWITH_Hypre:BOOL=FALSE -DWITH_Trilinos:BOOL=FALSE \
#		-DWITH_ELMERGUI:BOOL=FALSE -DWITH_ElmerIce:BOOL=TRUE \
#	&& make \
#	&& make install \
#	&& rm -R /home/elmerice

# Mystran
MYSTRAN_BUILD=/var/tmp/mystran_build
mkdir -p ${MYSTRAN_BUILD}

pushd ${MYSTRAN_BUILD} || exit
git clone https://github.com/MYSTRANsolver/MYSTRAN.git mystran
cd mystran || exit
cmake .
make -j"$(nproc)"
#sudo make install  # or;
# sudo checkinstall make install
popd || exit

# pynastran
# pip3 install pynastran
pipx install pynastran
