#!/bin/bash
geonature db upgrade geonature@head -x local-srid=$srid_local
geonature db autoupgrade -x local-srid=$srid_local

geonature db exec "DO 'BEGIN ASSERT EXISTS (SELECT 1 FROM taxonomie.taxref); END'" 2>/dev/null \
|| if [ "$install_bdc_statuts" = true ];
then
    geonature db upgrade ref_geo_fr_departments@head
    geonature taxref import-v18 --taxref-region=${taxref_region:-fr}
else
    geonature taxref import-v18 --skip-bdc-statuts
fi
geonature db upgrade nomenclatures_taxonomie_data@head

# Installation des données exemples
if [ "$add_sample_data" = true ];
then
    geonature db upgrade geonature-samples@head
fi

if [ "$install_sig_layers" = true ];
then
    geonature db upgrade ref_geo_fr_departments@head
    geonature db upgrade ref_geo_fr_municipalities@head
    geonature db upgrade ref_geo_fr_regions@head
fi

if [ "$install_grid_layer" = true ] || [ "$install_grid_layer_1" = true ];
then
    geonature db upgrade ref_geo_inpn_grids_1@head
fi
if [ "$install_grid_layer" = true ] || [ "$install_grid_layer_5" = true ];
then
    geonature db upgrade ref_geo_inpn_grids_5@head
fi
if [ "$install_grid_layer" = true ] || [ "$install_grid_layer_10" = true ];
then
    geonature db upgrade ref_geo_inpn_grids_10@head
fi

if [ "$install_grid_layer" = true ] || [ "$install_grid_layer_2" = true ];
then
    geonature db upgrade ref_geo_inpn_grids_2@head
fi

if [ "$install_grid_layer" = true ] || [ "$install_grid_layer_20" = true ];
then
    geonature db upgrade ref_geo_inpn_grids_20@head
fi

if [ "$install_grid_layer" = true ] || [ "$install_grid_layer_50" = true ];
then
    geonature db upgrade ref_geo_inpn_grids_50@head
fi


geonature db exec "DO 'BEGIN ASSERT EXISTS (SELECT 1 FROM gn_sensitivity.t_sensitivity_rules); END'" 2>/dev/null \
|| if [ "$install_ref_sensitivity" = true ];
then
    geonature db upgrade ref_geo_fr_departments@head
    geonature sensitivity add-referential \
    --source-name "Référentiel sensibilité TAXREF v18 20250904" \
    --url https://geonature.fr/data/inpn/sensitivity/RefSensibiliteV18_20250904.zip \
    --zipfile RefSensibiliteV18_20250904.zip \
    --csvfile RefSensibilite_18.csv  \
    --encoding=utf-8
    geonature sensitivity refresh-rules-cache
fi

if  [ "$install_default_dem" = true ];
then
    geonature db upgrade ign_bd_alti@head -x local-srid=$srid_local
    if [ "$vectorise_dem" = true ];
    then
        geonature db upgrade ign_bd_alti_vector@head
    fi
fi

geonature db autoupgrade -x local-srid=$srid_local
