#!/bin/sh

set -e

${MAKE:-make} histtest

test ! -f xhisttest/xhisttest1.nc || rm -f xhisttest/xhisttest1.nc
test ! -f xhisttest/xhisttest2.nc || rm -f xhisttest/xhisttest2.nc
test ! -f xhisttest/xhisttest3.nc || rm -f xhisttest/xhisttest3.nc
test ! -f xhisttest/xhisttest4.nc || rm -f xhisttest/xhisttest4.nc
test ! -f xhisttest/xhisttest5.nc || rm -f xhisttest/xhisttest5.nc
test ! -f xhisttest/xhisttest6.nc || rm -f xhisttest/xhisttest6.nc

./histtest 2> xdifs.log

cd xhisttest

###### Basic Create test (Conventions auto output) #####

echo -n "histtest [Basic Create test (Conventions auto output)] ... "

ncdump xhisttest1.nc        > xhisttest1.cdl
ncdump xhisttest1org.nc     > xhisttest1org.cdl
diff xhisttest1.cdl xhisttest1org.cdl > xhisttest1dif.cdl ||

diff xhisttest1dif.cdl - <<EOF
1c1
< netcdf xhisttest1 {
---
> netcdf xhisttest1org {
EOF

echo "okay"

rm -f xhisttest1.nc xhisttest1*.cdl xhisttest1dif.cdl

###### Auto Set Time test (gt_version manually output) #####

echo -n "histtest [Auto Set Time test (gt_version manually output)] ... "

ncdump xhisttest2.nc        > xhisttest2.cdl
ncdump xhisttest2org.nc     > xhisttest2org.cdl
diff xhisttest2.cdl xhisttest2org.cdl > xhisttest2dif.cdl ||

diff xhisttest2dif.cdl - <<EOF
1c1
< netcdf xhisttest2 {
---
> netcdf xhisttest2org {
EOF

echo "okay"

rm -f xhisttest2.nc xhisttest2*.cdl xhisttest2dif.cdl

###### Axes by GT_HISTORY_AXIS test #####

echo -n "histtest [Set Time by HistoryPut test (Conventions, gt_version manually output)] ... "

ncdump xhisttest3.nc        > xhisttest3.cdl
ncdump xhisttest3org.nc     > xhisttest3org.cdl
diff xhisttest3.cdl xhisttest3org.cdl > xhisttest3dif.cdl ||

diff xhisttest3dif.cdl - <<EOF
1c1
< netcdf xhisttest3 {
---
> netcdf xhisttest3org {
EOF

echo "okay"

rm -f xhisttest3.nc xhisttest3*.cdl xhisttest3dif.cdl


###### HistorySetTime test #####

echo -n "histtest [HistorySetTime test] ... "

ncdump xhisttest4.nc        > xhisttest4.cdl
ncdump xhisttest4org.nc     > xhisttest4org.cdl
diff xhisttest4.cdl xhisttest4org.cdl > xhisttest4dif.cdl ||

diff xhisttest4dif.cdl - <<EOF
1c1
< netcdf xhisttest4 {
---
> netcdf xhisttest4org {
EOF

echo "okay"

rm -f xhisttest4.nc xhisttest4*.cdl xhisttest4dif.cdl



###### HistorySetTime test #####

echo -n "histtest [HistoryCreate without Argument Keyword test] ... "

ncdump xhisttest5.nc        > xhisttest5.cdl
ncdump xhisttest5org.nc     > xhisttest5org.cdl
diff xhisttest5.cdl xhisttest5org.cdl > xhisttest5dif.cdl ||

diff xhisttest5dif.cdl - <<EOF
1c1
< netcdf xhisttest5 {
---
> netcdf xhisttest5org {
EOF

echo "okay"

rm -f xhisttest5.nc xhisttest5*.cdl xhisttest5dif.cdl



###### HistoryAddAttr test #####

echo -n "histtest [HistoryAddAttr test] ... "

ncdump xhisttest6.nc        > xhisttest6.cdl
ncdump xhisttest6org.nc     > xhisttest6org.cdl
diff xhisttest6.cdl xhisttest6org.cdl > xhisttest6dif.cdl ||

diff xhisttest6dif.cdl - <<EOF
1c1
< netcdf xhisttest6 {
---
> netcdf xhisttest6org {
EOF

echo "okay"

rm -f xhisttest6.nc xhisttest6*.cdl xhisttest6dif.cdl

rm -f xhisttest?.nc xhistest?*.cdl ../xdifs.log

exit 0
