#!/bin/sh -x
# Bootstrap minimum test data on Thredds.
# To run testsuite in https://github.com/Ouranosinc/PAVICS-e2e-workflow-tests.
#
# Need to open temporary Thredds "testdata/secure/" on PAVICS production to anonymous group.
#
# To be run locally on the host running Thredds.  Will populate test data files
# under DATASET_ROOT, customizable by env var.
#
# Need write-access to DATASET_ROOT (/data/datasets/).


if [ -z "$DATASET_ROOT" ]; then
    DATASET_ROOT="${DATA_PERSIST_ROOT:-/data}/datasets"
fi

FROM_SERVER="https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/fileServer/birdhouse"

FILE_LIST="
nrcan/nrcan_canada_daily/tasmin/nrcan_canada_daily_tasmin_2013.nc
testdata/flyingpigeon/cmip3/pr.sresa2.miub_echo_g.run1.atm.da.nc
testdata/flyingpigeon/cmip3/tasmin.sresa2.miub_echo_g.run1.atm.da.nc
testdata/flyingpigeon/cmip5/tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200601-200612.nc
testdata/flyingpigeon/cmip5/tasmax_Amon_MPI-ESM-MR_rcp45_r2i1p1_200601-200612.nc
testdata/secure/tasmax_Amon_MPI-ESM-MR_rcp45_r2i1p1_200601-200612.nc
testdata/ta_Amon_MRI-CGCM3_decadal1980_r1i1p1_199101-200012.nc
ets/Watersheds_5797_cfcompliant.nc
testdata/xclim/cmip6/sic_SImon_CCCma-CanESM5_ssp245_r13i1p2f1_2020.nc
cccma/CanESM2/historical/day/atmos/r1i1p1/tasmin/tasmin_day_CanESM2_historical_r1i1p1_18500101-20051231.nc
cccma/CanESM2/historical/fx/atmos/r0i0p0/sftlf/sftlf_fx_CanESM2_historical_r0i0p0.nc
testdata/xclim/NRCANdaily/nrcan_canada_daily_tasmin_1990.nc
"

cd "$DATASET_ROOT"

for afile in $FILE_LIST; do
    if [ ! -f "$afile" ]; then
        PARENT_DIRS="`dirname "$afile"`"
        if [ ! -d "$PARENT_DIRS" ]; then
            mkdir -p "$PARENT_DIRS"
        fi
        curl "$FROM_SERVER/$afile" --output "$afile"
    fi
done
