# 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}" == "Emergency" ]; then
        echo "Initializing collection '${COLLECTION}'."

        # CS00
        # CS01
        # CS02
        # CS03
        # CS04
        # DM01
        # EQ02
        # EW01
        # EW02
        # EW03
        # GE01
        # GY01
        # IK02
        # RE00
        # RS02
        # SP04
        # SP05
        # TX01

        # DM02
        # KS03
        # SP06
        # SP07

        # PH1A
        python3 manage.py producttype create "${COLLECTION}"_Product_PH1A --traceback \
            --coverage-type "RGBNir"
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1A  --traceback \
            --red "red" \
            --green "green" \
            --blue "blue" \
            --red-range 1 2000 \
            --green-range 1 2000 \
            --blue-range 1 2000 \
            --red-nodata 0 \
            --green-nodata 0 \
            --blue-nodata 0
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1A "TRUE_COLOR" --traceback \
            --red "red" \
            --green "green" \
            --blue "blue" \
            --red-range 1 2000 \
            --green-range 1 2000 \
            --blue-range 1 2000 \
            --red-nodata 0 \
            --green-nodata 0 \
            --blue-nodata 0
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1A "FALSE_COLOR" --traceback \
            --red "nir" \
            --green "red" \
            --blue "green" \
            --red-range 1 2000 \
            --green-range 1 2000 \
            --blue-range 1 2000 \
            --red-nodata 0 \
            --green-nodata 0 \
            --blue-nodata 0
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1A "NDVI" --traceback \
            --grey "(nir-red)/(nir+red)" --grey-range -1 1

        # PH1B
        python3 manage.py producttype create "${COLLECTION}"_Product_PH1B --traceback \
            --coverage-type "RGBNir"
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1B --traceback \
            --red "red" \
            --green "green" \
            --blue "blue" \
            --red-range 1 2000 \
            --green-range 1 2000 \
            --blue-range 1 2000 \
            --red-nodata 0 \
            --green-nodata 0 \
            --blue-nodata 0
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1B "TRUE_COLOR" --traceback \
            --red "red" \
            --green "green" \
            --blue "blue" \
            --red-range 1 2000 \
            --green-range 1 2000 \
            --blue-range 1 2000 \
            --red-nodata 0 \
            --green-nodata 0 \
            --blue-nodata 0
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1B "FALSE_COLOR" --traceback \
            --red "nir" \
            --green "red" \
            --blue "green" \
            --red-range 1 2000 \
            --green-range 1 2000 \
            --blue-range 1 2000 \
            --red-nodata 0 \
            --green-nodata 0 \
            --blue-nodata 0
        python3 manage.py browsetype create "${COLLECTION}"_Product_PH1B "NDVI" --traceback \
            --grey "(nir-red)/(nir+red)" --grey-range -1 1

        python3 manage.py collectiontype create "${COLLECTION}"_Collection --traceback \
            --coverage-type "RGBNir" \
            --product-type "${COLLECTION}"_Product_PH1A \
            --product-type "${COLLECTION}"_Product_PH1B

        python3 manage.py collection create "${COLLECTION}" --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
