#!/bin/sh
# invideo CLI installer
#
#   curl -fsSL https://agents.invideo.io | sh
#   curl -fsSL https://agents.invideo.io | sh -s -- <installer args>
#
# Fetches the upstream installer from the invideoio/cli repository and runs it.
# Everything lives inside main(), which is called on the last line, so a
# download that stops halfway never runs a partial script.

main() {
  set -eu

  upstream="https://raw.githubusercontent.com/invideoio/cli/refs/heads/main/install.sh"

  fail() {
    printf 'invideo install: %s\n' "$1" >&2
    exit 1
  }

  command -v curl >/dev/null 2>&1 || fail "curl is required but was not found."

  tmp=$(mktemp -d 2>/dev/null || mktemp -d -t invideo-install)
  trap 'rm -rf "$tmp"' EXIT INT TERM

  # Download in full before running anything.
  curl -fsSL --proto '=https' --tlsv1.2 \
    "$upstream" -o "$tmp/install.sh" \
    || fail "couldn't download the installer from $upstream."

  [ -s "$tmp/install.sh" ] || fail "the downloaded installer is empty."

  sh "$tmp/install.sh" "$@"
}

main "$@"
