#!/bin/sh
# autopkgtest check for gmsh
# (C) 2014 Anton Gladky

set -e

export PRTE_MCA_plm_ssh_agent=/bin/false

SRCDIR=$PWD
WORKDIR=$(mktemp -d)
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
cd $WORKDIR

cat <<EOF > tetrahedra.geo
acc = 0.2;
Point(1) = {0.0, 0.0, 0.0, acc};
Point(2) = {2.0, 0.0, 0.0, acc};
Point(3) = {2.0, 2.0, 0.0, acc};
Point(4) = {0.0, 2.0, 0.0, acc};
Point(5) = {1.0, 1.0, 5.0, acc};
Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};
Line(5) = {1, 5};
Line(6) = {2, 5};
Line(7) = {3, 5};
Line(8) = {4, 5};
Line Loop(9) = {5, -6, -1};
Plane Surface(10) = {9};
Line Loop(11) = {6, -7, -2};
Plane Surface(12) = {11};
Line Loop(13) = {7, -8, -3};
Plane Surface(14) = {13};
Line Loop(15) = {8, -5, -4};
Plane Surface(16) = {15};
Line Loop(17) = {4, 1, 2, 3};
Plane Surface(18) = {17};
Surface Loop(19) = {12, 10, 16, 14, 18};
Volume(20) = {19};
EOF

gmsh tetrahedra.geo -2 -o tetrahedra2d.mesh
gmsh tetrahedra.geo -3 -o tetrahedra3d.mesh
gmsh tetrahedra.geo -2 -o tetrahedra2d.msh
gmsh tetrahedra.geo -3 -o tetrahedra3d.msh
gmsh tetrahedra.geo -2 -o tetrahedra2d.stl
gmsh tetrahedra.geo -3 -o tetrahedra3d.stl
gmsh tetrahedra.geo -2 -o tetrahedra2d.vtk
gmsh tetrahedra.geo -3 -o tetrahedra3d.vtk
gmsh tetrahedra.geo -2 -o tetrahedra2d.cgns
gmsh tetrahedra.geo -3 -o tetrahedra3d.cgns

# Test support for reading step files.
gmsh "$SRCDIR/examples/boolean/component8.step" -3 -o component8.msh

# Test support for ANN, see https://gitlab.onelab.info/gmsh/gmsh/-/issues/3236
cat <<EOF > ann_support.geo
cm = 1e-2;
p1 = newp; Point(p1) = {0, 0, 0};
out[] = Extrude {0, 2.5 * cm, 0} { Point{p1}; Layers{25}; };
out[] = Extrude {10 * cm, 0, 0} { Curve{out[1]}; Layers{100}; Recombine; };
out[] = Extrude {{1, 0, 0}, {0, 0, 0}, Pi / 2} { Surface{out[1]}; Layers{10}; Recombine; };
Mesh 3;
Periodic Surface{20} = {13} Translate {10 * cm, 0, 0};
Periodic Surface{22} = {5}  Rotate    {{1, 0, 0}, {0, 0, 0}, Pi / 2};
Physical Surface("wall") = {17};
Physical Surface("inlet") = {13};
Physical Surface("outlet") = {20};
Physical Surface("xy plane") = {5};
Physical Surface("xz plane") = {22};
Physical Volume("domain") = {1};
EOF
gmsh -3 ann_support.geo -o ann_support.msh

ls -ln

# Test python3-gmsh imports
python3 -c "import gmsh"

# Test python tutorials
cp $SRCDIR/tutorials/python/t*.py .
python3 t1.py -nopopup
python3 t2.py -nopopup
python3 t3.py -nopopup

echo "run: OK"
