#!/bin/sh
#
# This file is part of Rheolef.
#
# Copyright (C) 2000-2009 Pierre Saramito 
#
# Rheolef is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Rheolef is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rheolef; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# -------------------------------------------------------------------------
h=$1
bamg_suffix="msh"
n_vertice_max=1000000
verbose=false

if test ! -f crack.dmn; then
  cat > crack.dmn << EOF2
EdgeDomainNames
2
crack
envelop
EOF2
  echo "! file \"crack.dmn\" created." 1>&2
fi

cat > crack-${h}.cad << EOF1
MeshVersionFormatted
  0
Dimension
  2
Vertices
  5
  0   0    1
  1   0    2
  1   1    3
 -1   1    4
 -1   0    5
Edges
  5
  1  2     1
  2  3     2
  3  4     2
  4  5     2
  5  1     2
hVertices
  ${h} ${h} ${h} ${h} ${h}
EOF1

echo "! file \"crack-${h}.cad\" created." 1>&2


$verbose && echo "! /bin/rm -f crack-${h}.${bamg_suffix}" 1>&2
/bin/rm -f crack-${h}.${bamg_suffix}

$verbose && echo "! bamg -nbv ${n_vertice_max} -g crack-${h}.cad -o crack-${h}.${bamg_suffix}" 1>&2
bamg -nbv ${n_vertice_max} -g crack-${h}.cad -o crack-${h}.${bamg_suffix} # >/dev/null 2>/dev/null

if test ! -f crack-${h}.${bamg_suffix}; then
    echo "bamg failed." 1>&2
    exit 1
fi
 
$verbose && echo "! cat crack-${h}.${bamg_suffix} crack.dmn | geo -input-bamg -upgrade -geo - > crack-${h}.geo" 1>&2
cat crack-${h}.${bamg_suffix} crack.dmn | \
  geo -input-bamg -upgrade -geo - > crack-${h}.geo 2>/dev/null
if test $? -ne 0; then
    echo "bamg2geo failed." 1>&2
    exit 1
fi

