 ############################################################
 # CONSTANTS AND PARAMETER
 ############################################################

 #-------------  Pack data -------------------
pack_name="windows-by-WinRM__shinken"
pack_version="V02.00.00-RC000.11"

#-------------  Probes data  ----------------
probe_path="/var/lib/shinken-user/libexec/shinken-additional-packs/$pack_name"

#-------------  Shinken COLLECTOR data ------
shinken_collector_name="shinken-additional-packs-import"
shinken_collector_previous_name="cfg-file-additionalPacks"
shinken_collector_name_cfg="/etc/shinken/sources/$shinken_collector_name.cfg"
shinken_collector_data_cfg="definition_shinken-additional-packs-import.cfg"
shinken_collector_data_path="/etc/shinken-user/source-data/source-data-$shinken_collector_name"
shinken_collector_data_cfg_path="$shinken_collector_data_path/$shinken_collector_data_cfg"
shinken_collector_monitoring_pack_definitions_pack_name_folder="$shinken_collector_data_path/packs/$pack_name"
shinken_collector_monitoring_pack_definitions_pack_name_file="$shinken_collector_data_path/packs/$pack_name.pack"
shinken_collector_monitoring_pack_data_folder="$shinken_collector_data_path/global-data/$pack_name"


#-------------  Monitoring data files -------
shinken_globaldata_folder_name="resource.d"
shinken_globaldata_path="/etc/shinken/$shinken_globaldata_folder_name"
shinken_collector_globaldata_path="$shinken_globaldata_path/$shinken_collector_name"
shinken_pack_globaldata_path="$shinken_collector_globaldata_path/global-data/$pack_name/"


#-------------  Synchronizer configuration --
shinken_synchronizer_master_cfg_path=/etc/shinken/synchronizers/synchronizer-master.cfg

############################################################

show_help() {
    echo "Usage: $0 [--remove-collector, --help]"
    echo ""
    echo "Options:"
    echo "  --remove-collector  Remove collector configuration and data files"
    echo "  --help              Display this help message"
    echo ""
    echo "Description:"
    echo "  This script uninstalls the pack $pack_name and its associated files."
    echo "  By default, collector data is preserved unless --remove-collector is specified."
    echo ""
    echo "Example:"
    echo "  $0 --remove-collector    Deletes the pack and its collector configuration."
    echo "  $0                       Deletes only the pack data without removing the collector configuration."
    echo ""
}

# Check for arguments
REMOVE_COLLECTOR=false
for arg in "$@"; do
    case $arg in
        --remove-collector)
            REMOVE_COLLECTOR=true
            shift
            ;;
        --help)
            show_help
            exit 0
            ;;
        *)
            echo "Error: Invalid argument '$arg'"
            show_help
            exit 1
            ;;
    esac
done

safe_remove() {
    local path="$1"
    local description="$2"

    echo "        => Deleting $description"
    echo "              - Path : $path"
    rm -rf "$path"
    echo ""
}

 ############################################################é

echo ""
echo "=========================================================="
echo "UNINSTALL PACK [ $pack_name $pack_version ]"
echo "=========================================================="
echo ""

if [ "$REMOVE_COLLECTOR" = true ]; then
    echo "    -----------------------------"
    echo "    |   COLLECTOR [ $shinken_collector_name ]"
    echo "    |"
    echo "    |  Removing the collector removes all packs installed with it"
    echo "    |"
    echo "    |  /!\ Don't forget to MANUALLY remove the collector from synchronizer sources"
    echo "    |  /!\ In file : [ $shinken_synchronizer_master_cfg_path ]"
    echo "    |  /!\ Remove collector ( $shinken_collector_name ) from variable ( sources )"

    echo ""
    safe_remove "$shinken_collector_name_cfg" "collector configuration file"
    safe_remove "$shinken_collector_data_path" "collector data folder"
echo ""
fi

echo "    -----------------------------"
echo "    |   MONITORING DEFINITION:"
echo ""
safe_remove "$shinken_collector_monitoring_pack_definitions_pack_name_folder" "pack monitoring definition folder"
safe_remove "$shinken_collector_monitoring_pack_definitions_pack_name_file" "pack monitoring definition file"
safe_remove "$shinken_collector_monitoring_pack_data_folder" "pack monitoring globaldata folder"

if [ "$REMOVE_COLLECTOR" = true ]; then
    safe_remove "$shinken_collector_globaldata_path" "shinken collector globaldata folder"
else
    safe_remove "$shinken_pack_globaldata_path" "shinken pack globaldata folder"
fi
echo ""

echo "    -----------------------------"
echo "    |   PROBES:"
echo ""

# Deleting probes
safe_remove "$probe_path" "probes"
echo ""
echo "=========================================================="
echo "PACK [ $pack_name $pack_version ] SUCCESSFULLY UNINSTALLED"
echo "=========================================================="
echo ""
