#!/bin/bash

# --- DEPENDENCY CHECK ---
if ! command -v curl &> /dev/null; then
    echo "Curl is missing. Installing it now..."
    sudo dnf install -y curl || sudo apt install -y curl
fi

# Define the base URL
BASE_URL="https://htm.wpdevdaemon.com/wp-content/uploads/2026/03"

FILES=(
    "htm214-lab-instructions.pdf"
    "htm214-example-pen-test-report.pdf"
    "htm214-example-nessus-scan.xlsx"
    "master-setup.sh"
    "uninstall-apps.sh"
    "phishing-sample.eml"
)

echo "--- STARTING MEDICAL LAB FILE DOWNLOAD ---"

for FILE in "${FILES[@]}"; do
    echo "Downloading $FILE..."
    curl -sL "$BASE_URL/$FILE" -o "$HOME/$FILE"
    
    if [ $? -eq 0 ]; then
        echo "Successfully downloaded $FILE"
        # Fix Windows line endings immediately if it's a shell script
        if [[ $FILE == *.sh ]]; then
            sed -i 's/\r$//' "$HOME/$FILE"
            chmod +x "$HOME/$FILE"
            echo "  -> Sanitized and set executable: $FILE"
        fi
    else
        echo "ERROR: Failed to download $FILE"
    fi
done

echo "------------------------------------------"
echo "LAB SETUP COMPLETE"
echo "Files are located in: $HOME"
echo "------------------------------------------"