#!/bin/bash
echo //////////////////////////////////////////////////
echo                Mesh generation
echo //////////////////////////////////////////////////
echo ' '

divameshlog='./output/divamesh.log'
echo ////////////////////////////////////////////////// > $divameshlog
echo                Mesh generation >> $divameshlog
echo ////////////////////////////////////////////////// >> $divameshlog
echo ' ' >> $divameshlog

if [ -f ./input/coast.cont ] 
then
cp ./input/coast.cont ./meshgenwork/fort.10
else
echo Need to provide coast.cont in ./input !
echo Need to provide coast.cont in ./input ! >> $divameshlog
fi
isdens=0
if [ -f ./input/coast.cont.dens ] 
then
echo There is a density file for the mesh >> $divameshlog
cp -v ./input/coast.cont.dens ./meshgenwork/fort.12
isdens=1
fi
##echo $isdens
if [ -f ./input/param.par ] 
then
echo Reading parameters >> $divameshlog
else
echo Need to provide param.par in ./input/ >> $divameshlog
echo Need to provide param.par in ./input/
fi
Filepar=./input/param.par
{
read linecomment
read lc
read linecomment
read icoordchange
read linecomment
read ispec
read linecomment
read ireg
read linecomment
read xori
read linecomment
read yori
read linecomment
read dx
read linecomment
read dy
read linecomment
read nx
read linecomment
read ny
read linecomment
read valex
read linecomment
read snr
read linecomment
read varbak
} < $Filepar
#param.par
#creation of files for mesh generation
#creation of file fort.11 for gener.a
#rm fort.11
#exec 11<>fort.11
#echo 0 > ./meshgenwork/fort.11
#echo $lc >> ./meshgenwork/fort.11
#echo 1.5 >> ./meshgenwork/fort.11
#echo 5 >> ./meshgenwork/fort.11
#exec 11>&-

rm -f fort.11

##replaced by Awk command
#
#echo $isdens $lc | ../bin/lcelem.a
#mv fort.11 ./meshgenwork/fort.11
#echo 1.5 >> ./meshgenwork/fort.11
#echo 5 >> ./meshgenwork/fort.11

echo $isdens > ./meshgenwork/fort.11
echo $(echo $lc | awk '{print ($1/5.)}') >> ./meshgenwork/fort.11
echo 1.5 >> ./meshgenwork/fort.11
echo 5 >> ./meshgenwork/fort.11


#exec 11>&-
#generation of the mesh
cd ./meshgenwork

echo ////////////////////////////////////////// >> ../output/diva.log
echo '           Mesh generation (generopt)' >> ../output/diva.log
echo ////////////////////////////////////////// >> ../output/diva.log
echo ' '  >> ../output/diva.log
echo $icoordchange | ../../bin/generopt.a >> ../output/diva.log
if [ $? -ne 0 ];then
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo          divamesh      generopt.a
echo Check execution track
echo --------------------------------------------
echo ' ' >> $divameshlog
echo -------------------------------------------- >> $divameshlog
echo A problem was encountered during execution ! >> $divameshlog
echo          divamesh      generopt.a >> $divameshlog
echo Check execution track >> $divameshlog
echo -------------------------------------------- >> $divameshlog
echo ' ' >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo A problem was encountered during execution ! >> ../output/diva.log
echo          divamesh      generopt.a  >> ../output/diva.log
echo Check execution track >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
fi
File23=./fort.23
{
read nnt1
read nnint
read nelt
} < $File23
#output of info for mesh visualisation with meshvisu.m
echo ' '
echo Copying output files for visualisation >> ../$divameshlog
echo in directory ./output/meshvisu/  >> ../$divameshlog
cp fort.10 ../output/meshvisu/
cp fort.22 ../output/meshvisu/
cp fort.23 ../output/meshvisu/
 
#exec 23<>fort.23
#read  nnt1 nnint nelt <&3
#exec 23>&-
cd ..
echo ' '
echo ---------------
echo Mesh is created
echo ---------------
echo ' '
echo ' '  >> $divameshlog
echo ---------------  >> $divameshlog
echo Mesh is created  >> $divameshlog
echo ---------------  >> $divameshlog
echo ' '  >> $divameshlog

