#!/usr/bin/env bash
# element_get.sh – Universal Remote Downloader
# Copyright (c) 2025 elemental-admin.com
#
# This script is released under the MIT License.
# Full license text: https://elemental-admin.com/LICENSE
set -euo pipefail

usage()
{
    echo "Usage:" >&2
    echo "  $0 list <domain>" >&2
    echo "  $0 download <domain> <name>" >&2
    echo >&2
    echo "Examples:" >&2
    echo "  $0 list example.com" >&2
    echo "  $0 download example.com \"Docker install script\"" >&2
    exit 1
}

require_cmd()
{
    local cmd="$1"

    if ! command -v "$cmd" >/dev/null 2>&1
    then
        echo "Error: required command '$cmd' not found in PATH." >&2
        exit 1
    fi
}

if [ "$#" -lt 2 ]
then
    usage
fi

require_cmd curl

mode="$1"
domain="$2"
base_url="https://$domain"
remote_list_url="$base_url/download.txt"

case "$mode" in
    list)
        # list needs exactly 2 args: script, list, domain
        if [ "$#" -ne 2 ]
        then
            usage
        fi

        # Curl the list, but do NOT save it to disk.
        # Format (same as v1 spec):
        #   line 1: name A
        #   line 2: path A
        #   line 3: name B
        #   line 4: path B
        curl -fsSL "$remote_list_url" | {
            line_number=0

            while IFS= read -r line
            do
                line_number=$((line_number + 1))

                # Odd lines = names
                if [ $((line_number % 2)) -eq 1 ]
                then
                    printf '%s\n' "$line"
                fi
            done
        }
        ;;

    download)
        # download needs: script, download, domain, name (which may contain spaces)
        if [ "$#" -lt 3 ]
        then
            usage
        fi

        # Collect all remaining arguments as the name, including spaces
        shift 2
        wanted_name="$*"

        # Find the path associated with the given name
        path="$(
            curl -fsSL "$remote_list_url" | {
                line_number=0
                found_name=0

                while IFS= read -r line
                do
                    line_number=$((line_number + 1))

                    # Odd lines: candidate names
                    if [ $((line_number % 2)) -eq 1 ]
                    then
                        if [ "$line" = "$wanted_name" ]
                        then
                            found_name=1
                        else
                            found_name=0
                        fi
                    else
                        # Even lines: value for the previous name
                        if [ "$found_name" -eq 1 ]
                        then
                            printf '%s' "$line"
                            exit 0
                        fi
                    fi
                done

                # If we get here, the name was not found
                exit 1
            }
        )" || {
            echo "Error: entry '$wanted_name' not found in $remote_list_url" >&2
            exit 1
        }

        full_url="$base_url$path"
        filename="$(basename "$full_url")"

        echo "Downloading:"
        echo "  Name : $wanted_name"
        echo "  From : $full_url"
        echo "  To   : $filename"
        echo

        # Keep the v1 behavior: let curl decide the final name (URL basename or Content-Disposition)
        # This preserves compatibility with any existing servers relying on it.
        curl -fSL -OJ "$full_url"

        echo "Done."
        ;;

    *)
        usage
        ;;
esac
