#!/bin/sh
# Copyright 2024 SPC-EPFL
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------

curdir=`pwd`
if [ $# -lt 1 ]
then
  chease_path=/home/codes/bin
else
  chease_path=$1
fi
echo $chease_path

export CHEASE_ROOT=$PWD  #this is the directory in which you have src-f90, WK, etc
if [ $# -ge 2 ]
then
  CHEASE_ROOT=$2
fi

export NIDEAL10=$CHEASE_ROOT'/WK/TESTCASES/NIDEAL10'
export PATH=$CHEASE_ROOT/scripts_for_bin:$chease_path:$PATH # to add path for o.chease_to_cols and official chease release at SPC

mkdir -p /tmp/$USER/WKnideal10
cp -pr $NIDEAL10/input/* /tmp/$USER/WKnideal10/
cd /tmp/$USER/WKnideal10

export TEST_FILE='splus'    #can also be tcv.snd1

cp EXPEQ.$TEST_FILE EXPEQ
cp i.$TEST_FILE chease_namelist
cp i.$TEST_FILE o.i.$TEST_FILE
chease >> o.i.$TEST_FILE
echo $CHEASE_ROOT

which o.chease_to_cols
which chease
o.chease_to_cols o.i.$TEST_FILE o.i.$TEST_FILE.cols
mv hamada.dat hamada.$TEST_FILE.dat
mv neoart.dat neoart.$TEST_FILE.dat
ls -al o.i.$TEST_FILE o.i.$TEST_FILE.cols
echo " "
q0=0.79332
qedge=3.8735
echo "expects q0 = $q0 and qedge = $qedge"
egrep Q_ZERO\|Q_EDGE o.i.$TEST_FILE | cut -c -25
echo " "
Q_ZERO=`grep Q_ZERO o.i.splus | cut -c -18`
Q_EDGE=`grep Q_EDGE o.i.splus | cut -c -18`
err0=`perl -e "print ($q0-$Q_ZERO)"`
erredge=`perl -e "print ($qedge-$Q_EDGE)"`
err_ok=`perl -e "print (abs($err0*$erredge)<1e-8)"`
echo "$err_ok, $err0, $erredge"
if [ $err_ok -eq '1' ]
then
  CODE=0
else
  CODE=1
fi

#echo "addpath $CHEASE_ROOT/WK/TESTCASES/NIDEAL10/matlab/" > test_$TEST_FILE.m
#echo "[H1,H2,Hdiff,N1,N2,Ndiff]=chease_diff(1e-4,'$TEST_FILE','$CHEASE_ROOT/WK/TESTCASES/NIDEAL10/output_ref/','$TEST_FILE','./');" >> test_$TEST_FILE.m
#echo "disp('type quit when ok to continue')" >> test_$TEST_FILE.m

#matlab -nodesktop -r "test_$TEST_FILE"

#CODE=$?
echo "exit with CODE" $CODE
exit $CODE
