#!/bin/bash

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

#-------------  Colors  ---------------------
COLOR_YELLOW="\e[33m"
COLOR_RESET="\e[0m"

#-------------  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_path="$shinken_collector_data_path/packs"
shinken_collector_monitoring_pack_definitions_pack_name_folder="$shinken_collector_data_path/packs/$pack_name"
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_pack_globaldata_path="$shinken_globaldata_path/$shinken_collector_name"

#-------------  Installation Data -----------
user="shinken"

print_help() {
    echo "NAME"
    echo "    install.sh - installer of a shinken pack"
    echo ""
    echo "SYNOPSIS"
    echo "    install.sh [option]"
    echo ""
    echo "DESCRIPTION"
    echo "    Install the pack where the install.sh script is located"
    echo ""
    echo "PREREQUISITES"
    echo "    The pack must be uninstalled before installation. The script will check"
    echo "    for existing installations and abort if found."
    echo ""
    echo "OPTIONS"
    echo "    Running with no arguments will install the pack."
    echo ""
    echo "    --info"
    echo "        Prints essential info of the pack such as it's name, version. Does not install the pack"
    echo "    --version"
    echo "        Only prints the pack version, in a format easy to parse. Does not install the pack"
    echo "    --name"
    echo "        Only prints the pack name, in a format easy to parse. Does not install the pack"
    echo "    --help"
    echo "        Display this help message"
    echo ""
}

print_pack_name() {
    echo $pack_name
}

print_pack_version() {
    echo $pack_version
}

print_info() {
    echo "Name:"
    print_pack_name
    echo ""
    echo "Version:"
    print_pack_version
    echo ""
}

check_pack_uninstalled() {
    local COLOR_RED="\e[31m"
    local COLOR_RESET="\e[0m"

    local installation_detected=false
    local error_messages=""
    
    # Vérifier les chemins spécifiques au pack
    local paths_to_check=(
        "$probe_path"
        "$shinken_collector_monitoring_pack_definitions_pack_name_folder"
        "$shinken_collector_monitoring_pack_data_folder"
        "$shinken_pack_globaldata_path/global-data/$pack_name"
    )
    
    for path in "${paths_to_check[@]}"; do
        if [ -e "$path" ]; then
            installation_detected=true
            error_messages="$error_messages              - $path\n"
        fi
    done
    
    if [ "$installation_detected" = true ]; then
        echo -e "${COLOR_RED}ERROR: Previous installation detected!${COLOR_RESET}"
        echo ""
        echo "The following files/folders from a previous installation were found:"
        echo -e "$error_messages"
        echo "Please run './uninstall.sh' before installing the pack."
        echo ""
        echo "Installation ABORTED."
        echo ""
        echo -e "${COLOR_YELLOW}WARNING: Uninstallation will remove all installed config files ( .cfg )."
        echo "This step is required for installing this new pack version."
        echo -e "All manual changes to the present config files will be lost. This doesn't affect shinken Staging or Production environment.${COLOR_RESET}"
        echo ""
        return 1
    else
        return 0
    fi
}

FIRST_ARG=$1

if [[ ${FIRST_ARG} == "" ]]; then
    : # Does nothing. Fall after the "if cascade" and install the pack
elif [[ ${FIRST_ARG} == "--info" ]]; then
    print_info
    exit $?
elif [[ ${FIRST_ARG} == "--name" ]]; then
    print_pack_name
    exit $?
elif [[ ${FIRST_ARG} == "--version" ]]; then
    print_pack_version
    exit $?
elif [[ ${FIRST_ARG} == "--help" ]]; then
    print_help
    exit $?
else
    echo "Unkown argument."
    echo ""
    print_help
    exit $?
fi

# The install script should call the uninstall script of the previous installed versions,
# not the current one.
# ./uninstall.sh > /dev/null

############################################################
# Pre-installation checks
############################################################

echo "========================================================"
echo "PRE-INSTALLATION CHECKS"
echo "========================================================"
echo ""
if ! check_pack_uninstalled; then
    exit 1
else
    echo "No previous installation detected. Proceeding with installation..."
fi

############################################################
# Installing process
############################################################
echo ""
echo "========================================================"
echo "INSTALL PACK [ $pack_name $pack_version ]"
echo "========================================================"
echo ""
echo "    -----------------------------"
echo "    |   COLLECTOR [ $shinken_collector_name ]"
echo ""
if [ -e "$shinken_collector_name_cfg" ]
then
    echo "        => Collector cfg file already AVAILABLE"
    echo "              - Path : $shinken_collector_name_cfg"
    echo ""
else
    echo "        => Adding Collector cfg file"
    echo "              - Path : $shinken_collector_name_cfg"
    cp "./shinken-source-modules/collectors/$shinken_collector_name/$shinken_collector_name.cfg" "$shinken_collector_name_cfg"
    echo ""
fi

if [ -e "$shinken_collector_data_path" ]
then
    echo "        => Collector DATA space already AVAILABLE"
    echo "              - Path : $shinken_collector_data_path"
    echo ""
else
    echo "        => Creating Collector DATA space"
    echo "              - Path : $shinken_collector_data_path"
    mkdir --parents $shinken_collector_data_path
    echo ""
fi

if [ -e $shinken_collector_data_cfg_path ]
then
    echo "        => Definition File already AVAILABLE [ $shinken_collector_data_cfg ]"
    echo "              - Path : $shinken_collector_data_cfg_path"
    echo ""
else
    echo "        => Adding Definition File [ $shinken_collector_data_cfg ]"
    echo "              - Path : $shinken_collector_data_cfg_path"
    cp "./shinken-source-modules/collectors/$shinken_collector_name/$shinken_collector_data_cfg" "$shinken_collector_data_path/."
    echo ""
fi

echo "        => Applying permissions"
echo "              - Path : $shinken_collector_data_path"
echo "              - Setting owner ( to \"shinken\" user )"
chown -R "$user:$user" $shinken_collector_data_path
echo "              - Setting rights"
chmod -R 744 $shinken_collector_data_path


echo ""
echo "    -----------------------------"
echo "    |   MONITORING DEFINITION:"
echo ""
echo "        => Installing definitions file in source-data folder ( Collector => $shinken_collector_name )"
echo "              - Path : $shinken_collector_data_path"
cp -r "./monitoring-definitions/"* $shinken_collector_data_path

echo "              - Setting owner ( to \"shinken\" user )"
chown -R "$user:$user" $shinken_collector_data_path
echo "              - Setting rights"
chmod -R 744 $shinken_collector_data_path
echo ""

echo "        => Installing global data definitions file in SHINKEN directory ( => resource.d )"
echo "              - Path : $shinken_pack_globaldata_path"
if [ ! -e $shinken_pack_globaldata_path ]
then
    mkdir --parents $shinken_pack_globaldata_path
fi
cp -r "./monitoring-definitions/global-data" "$shinken_pack_globaldata_path"

echo "              - Setting owner ( to \"shinken\" user )"
chown -R "$user:$user" "$shinken_pack_globaldata_path"
echo "              - Setting rights"
chmod -R 744 "$shinken_pack_globaldata_path"


echo ""
echo "    -----------------------------"
echo "    |   PROBES:"
echo ""
if [ -d "$probe_path" ]
then
    echo "        => Deleting previous probes"
    echo "              - Path : $probe_path"
    rm -r "$probe_path"
    echo ""
fi
if [ ! -e $probe_path ]
then
echo "        => Installing probes"
echo "              - Path : $probe_path"
    mkdir --parents $probe_path
fi
cp -r ./probes/* $probe_path
echo "              - Setting owner ( to \"shinken\" user )"
chown -R shinken:shinken $probe_path
echo "              - Setting rights"
chmod -R 750 $probe_path
echo ""
echo "========================================================"