#!/bin/bash
echo -e "\033[31m###### This script will try to auto-resolve the Splunk Enterprise download URL and exract Splunk to a folder of your choice #####\033[0m"
echo -e "\033[33m[$(date +%H:%M:%S)]: Attempting to autoresolve the latest version of Splunk...\033[0m"

LATEST_SPLUNK=$(curl https://www.splunk.com/en_us/download/splunk-enterprise.html | grep -i Linux-x86_64.tgz | grep -Eo "data-link=\"................................................................................................................................" | cut -d '"' -f 2)
SPLUNK_MD5=$(curl https://www.splunk.com/en_us/download/splunk-enterprise.html | grep -i Linux-x86_64.tgz | grep -Eo "data-md5=\"................................................................................................................................" | cut -d '"' -f 2)

# Sanity check what was returned from the auto-parse attempt

if [[ "$(echo $LATEST_SPLUNK | grep -c "^https:")" -eq 1 ]] && [[ "$(echo $LATEST_SPLUNK | grep -c "\.tgz$")" -eq 1 ]]; then

    echo -e "\033[33m[$(date +%H:%M:%S)]: The URL to the latest Splunk version was automatically resolved as: $LATEST_SPLUNK\033[0m"

    echo -e "\033[33m[$(date +%H:%M:%S)]: The corresponding md5 checksum file was automatically resolved as: $SPLUNK_MD5\033[0m"

    # Ask which folder to download to
    # Prompt user for extraction directory
    read -p "Enter the target download directory (default: /opt): " download_dir

    # Set default extraction directory to /opt if no input provided
    if [[ -z "$download_dir" ]]; then
        download_dir="/opt"
    fi

    # Check if the specified directory exists
    if [[ ! -d "$download_dir" ]]; then
        echo -e "\033[31mThe directory $download_dir does not exist.\033[0m"

        # Prompt the user to create the directory
        read -p "Would you like to create the directory? [y/n] \033[0m" create_dir
        if [[ "$create_dir" == "y" ]]; then
            mkdir -p "$download_dir"
            echo -e "\033[33mDirectory $download_dir created.\033[0m"
        else
            echo -e "\033[31mExtraction canceled.\033[0m"
            exit 1
        fi
    fi

    # Download file

    echo -e "\033[31m[$(date +%H:%M:%S)]: Attempting to download...\033[0m"

    wget --progress=bar:force -nc -P "$download_dir" "$LATEST_SPLUNK" # --no-clobber skips if file already exists on disk
    wget --progress=bar:force -nc -P "$download_dir" "$SPLUNK_MD5"

    cd "$download_dir" || exit 1
    echo -e "\033[31m[$(date +%H:%M:%S)]: Download succesfull - Validating md5 checksum\033[0m"

    # Extract the filenames from the directory
    tar_file=$(find $download_dir -name 'splunk?*.tgz' -type f)

    # Check if the tar.gz file was found
    if [[ -z "$tar_file" ]]; then
        echo -e "\033[33mNo .tgz file matching the pattern 'splunk?*.tgz' found in the $download_dir directory.\033[0m"
        exit 1
    fi
    # Validate MD5 checksum
    checksum_file="${tar_file}.md5"
    if [ -e $checksum_file ]; then

        if md5sum --status -c $checksum_file; then

            echo -e "\033[31m[$(date +%H:%M:%S)]: MD5 checksum validation successful! Continue with extraction..\033[0m"
            # Extracting the downloaded file
            # Prompt user for extraction directory
            read -p "Enter the directory to extract the file into (default: /opt): " extract_dir

            # Set default extraction directory to /opt if no input provided
            if [[ -z "$extract_dir" ]]; then
                extract_dir="/opt"
            fi

            # Check if the specified directory exists
            if [[ ! -d "$extract_dir" ]]; then
                echo -e "\033[33mThe directory $extract_dir does not exist.\033[0m"

                # Prompt the user to create the directory
                read -p "Would you like to create the directory? [y/n] " create_dir
                if [[ "$create_dir" == "y" ]]; then
                    mkdir -p "$extract_dir"
                    echo -e "\033[31mDirectory $extract_dir created.\033[0m"
                else
                    echo -e "\033[33mExtraction canceled.\033[0m"
                    exit 1
                fi
            fi

            echo -e "\033[31m[$(date +%H:%M:%S)]: Folder set, extracting the file\033[0m"
            # Extract the tar.gz file to the specified directory
            tar -xzvf "$tar_file" -C "$extract_dir"
            echo -e "\033[31m[$(date +%H:%M:%S)]: Extraction completed\033[0m"

        else
            echo -e "\033[33m[$(date +%H:%M:%S)]: MD5 checksum validation failed. Please try the download again. Aborting extraction.\033[0m"
            exit 1
        fi
    else
        echo -e "\033[33m[$(date +%H:%M:%S)]: MD5 checksum validation ($checksum_file) against ($tar_file) failed. Please try the download again. Aborting extraction.\033[0m"
    fi
else

    echo -e "\033[33m[$(date +%H:%M:%S)]: Unable to auto-resolve the latest Splunk version.\033[0m"

fi