`#!/bin/bash

show_help() {
    echo "Usage: tdl [FILE]"
    echo "Download a list of URLs from a file using tiddl."
    echo ""
    echo "Arguments:"
    echo "  FILE        Path to a text file containing one URL per line."
    echo ""
    echo "Options:"
    echo "  --help      Display this help message and exit."
}

if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]]; then
    show_help
    exit 0
fi

INPUT_FILE="$1"

if [[ ! -f "$INPUT_FILE" ]]; then
    echo "Error: File '$INPUT_FILE' not found."
    exit 1
fi

while IFS= read -r url || [[ -n "$url" ]]; do
    # Skip empty lines and comments
    [[ -z "$url" || "$url" =~ ^[[:space:]]*# ]] && continue
    # Trim whitespace
    url=$(echo "$url" | xargs)
    tiddl download url "$url"
done < "$INPUT_FILE"
