# Check if collection exits in database and initialize database only if not
if python3 manage.py id check "${COLLECTION}"; then
    echo "Initialize database"

    python3 manage.py coveragetype import /rgbnir_definition.json --traceback

    if [ "${COLLECTION}" == "DEM" ]; then
        echo "Initializing collection '${COLLECTION}'."

        python3 manage.py producttype create "${COLLECTION}"_Product_COP-DEM_GLO-30-DTED --traceback \
            --coverage-type "int16_grayscale"
        python3 manage.py browsetype create "${COLLECTION}"_Product_COP-DEM_GLO-30-DTED --traceback \
            --red "gray" \
            --red-range -100 4000 \
            --red-nodata 0
        python3 manage.py producttype create "${COLLECTION}"_Product_COP-DEM_GLO-90-DTED --traceback \
            --coverage-type "int16_grayscale"
        python3 manage.py browsetype create "${COLLECTION}"_Product_COP-DEM_GLO-90-DTED --traceback \
            --red "gray" \
            --red-range -100 4000 \
            --red-nodata 0
        python3 manage.py producttype create "${COLLECTION}"_Product_COP-DEM_EEA-10-DGED --traceback \
            --coverage-type "float32_grayscale"
        python3 manage.py browsetype create "${COLLECTION}"_Product_COP-DEM_EEA-10-DGED --traceback \
            --red "gray" \
            --red-range -100 4000 \
            --red-nodata 0
        python3 manage.py producttype create "${COLLECTION}"_Product_COP-DEM_EEA-10-INSP --traceback \
            --coverage-type "float32_grayscale"
        python3 manage.py browsetype create "${COLLECTION}"_Product_COP-DEM_EEA-10-INSP --traceback \
            --red "gray" \
            --red-range -100 4000 \
            --red-nodata 0
        python3 manage.py producttype create "${COLLECTION}"_Product_COP-DEM_GLO-30-DGED --traceback \
            --coverage-type "float32_grayscale"
        python3 manage.py browsetype create "${COLLECTION}"_Product_COP-DEM_GLO-30-DGED --traceback \
            --red "gray" \
            --red-range -100 4000 \
            --red-nodata 0
        python3 manage.py producttype create "${COLLECTION}"_Product_COP-DEM_GLO-90-DGED --traceback \
            --coverage-type "float32_grayscale"
        python3 manage.py browsetype create "${COLLECTION}"_Product_COP-DEM_GLO-90-DGED --traceback \
            --red "gray" \
            --red-range -100 4000 \
            --red-nodata 0 

        python3 manage.py collectiontype create "${COLLECTION}"_Collection --traceback \
            --coverage-type "float32_grayscale" \
            --coverage-type "int16_grayscale" \
            --product-type "${COLLECTION}"_Product_COP-DEM_GLO-30-DTED \
            --product-type "${COLLECTION}"_Product_COP-DEM_GLO-90-DTED \
            --product-type "${COLLECTION}"_Product_COP-DEM_EEA-10-DGED \
            --product-type "${COLLECTION}"_Product_COP-DEM_EEA-10-INSP \
            --product-type "${COLLECTION}"_Product_COP-DEM_GLO-30-DGED \
            --product-type "${COLLECTION}"_Product_COP-DEM_GLO-90-DGED

        python3 manage.py collection create "${COLLECTION}" --type "${COLLECTION}"_Collection --traceback
        python3 manage.py collection create "${COLLECTION}_COP-DEM_EEA-10-DGED" --type "${COLLECTION}"_Collection --traceback
        python3 manage.py collection create "${COLLECTION}_COP-DEM_EEA-10-INSP" --type "${COLLECTION}"_Collection --traceback
        python3 manage.py collection create "${COLLECTION}_COP-DEM_GLO-30-DGED" --type "${COLLECTION}"_Collection --traceback
        python3 manage.py collection create "${COLLECTION}_COP-DEM_GLO-30-DTED" --type "${COLLECTION}"_Collection --traceback
        python3 manage.py collection create "${COLLECTION}_COP-DEM_GLO-90-DGED" --type "${COLLECTION}"_Collection --traceback
        python3 manage.py collection create "${COLLECTION}_COP-DEM_GLO-90-DTED" --type "${COLLECTION}"_Collection --traceback

    else
        echo "Provided collection '${COLLECTION}' not valid."
    fi

    python3 manage.py storageauth create auth-cloud-ovh "${OS_AUTH_URL_SHORT}" \
        --type keystone \
        -p auth-version "${ST_AUTH_VERSION}" \
        -p identity-api-version="${ST_AUTH_VERSION}" \
        -p username "${OS_USERNAME}" \
        -p password "${OS_PASSWORD}" \
        -p tenant-name "${OS_TENANT_NAME}" \
        -p tenant-id "${OS_TENANT_ID}" \
        -p region-name "${OS_REGION_NAME}"

    python3 manage.py storage create \
        ${UPLOAD_CONTAINER} ${UPLOAD_CONTAINER} \
        --type swift \
        --storage-auth auth-cloud-ovh


else
    echo "Using existing database"
fi
