#!/bin/bash
# Quick download script using wget
# Usage: ./download_directory.sh <URL> [OUTPUT_DIR] [USERNAME] [PASSWORD]

URL="${1:-}"
OUTPUT_DIR="${2:-./downloads}"
USERNAME="${3:-}"
PASSWORD="${4:-}"

if [ -z "$URL" ]; then
    echo "Usage: $0 <URL> [OUTPUT_DIR] [USERNAME] [PASSWORD]"
    echo ""
    echo "Examples:"
    echo "  $0 https://your-golf-club.com/files/ ./downloads"
    echo "  $0 https://your-golf-club.com/files/ ./downloads myuser mypass"
    exit 1
fi

# Ensure URL ends with /
if [[ ! "$URL" =~ /$ ]]; then
    URL="${URL}/"
fi

echo "Downloading from: $URL"
echo "Output directory: $OUTPUT_DIR"
echo ""

# Create output directory
mkdir -p "$OUTPUT_DIR"

# Build wget command
WGET_CMD="wget -r -np -nH --cut-dirs=0 -P \"$OUTPUT_DIR\""

# Add authentication if provided
if [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then
    WGET_CMD="$WGET_CMD --user=\"$USERNAME\" --password=\"$PASSWORD\""
    echo "Using authentication for user: $USERNAME"
fi

# Add other useful options
WGET_CMD="$WGET_CMD --no-check-certificate"  # If SSL issues
WGET_CMD="$WGET_CMD --continue"              # Resume interrupted downloads
WGET_CMD="$WGET_CMD --progress=bar"          # Show progress

# Execute
echo "Starting download..."
eval $WGET_CMD "$URL"

if [ $? -eq 0 ]; then
    echo ""
    echo "✅ Download complete!"
    echo "Files saved to: $(realpath "$OUTPUT_DIR")"
else
    echo ""
    echo "✗ Download failed. Check the error messages above."
    exit 1
fi




