#!/bin/sh
set -eu

BASE_URL='https://sendthings.app'
VERSION='14f4585'
DEFAULT_SIGNALING_URL='wss://sendthings-signaling.contact-70f.workers.dev/ws'

HAS_SESSION=0
HAS_SIGNALING=0
HAS_HOST=0
PREV=""
for ARG in "$@"; do
  if [ "$PREV" = "session" ]; then
    HAS_SESSION=1
    PREV=""
    continue
  fi
  if [ "$PREV" = "signaling" ]; then
    HAS_SIGNALING=1
    PREV=""
    continue
  fi

  case "$ARG" in
    --host)
      HAS_HOST=1
      ;;
    --session)
      PREV="session"
      ;;
    --session=*)
      HAS_SESSION=1
      ;;
    --signaling-url)
      PREV="signaling"
      ;;
    --signaling-url=*)
      HAS_SIGNALING=1
      ;;
  esac
done

if [ "$HAS_SESSION" -ne 1 ] && [ "$HAS_HOST" -ne 1 ]; then
  echo "Usage:" >&2
  echo "  curl -fsSL $BASE_URL/terminal | sh -s -- --session CODE [options]" >&2
  echo "  curl -fsSL $BASE_URL/terminal | sh -s -- --host [options]" >&2
  exit 1
fi

OS=$(uname -s)
ARCH=$(uname -m)
TARGET=""
BIN_NAME="sendthings-terminal"

case "$OS/$ARCH" in
  Linux/x86_64)
    TARGET="linux-amd64"
    ;;
  Linux/aarch64|Linux/arm64)
    TARGET="linux-arm64"
    ;;
  Darwin/x86_64)
    TARGET="darwin-amd64"
    ;;
  Darwin/arm64)
    TARGET="darwin-arm64"
    ;;
  *)
    echo "Unsupported platform: $OS/$ARCH" >&2
    echo "Windows users can download sendthings-terminal.exe directly from $BASE_URL/terminal/bin/$VERSION/windows-amd64/" >&2
    exit 1
    ;;
esac

TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t sendthings-terminal)
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM

BIN_PATH="$TMP_DIR/$BIN_NAME"
SHA_PATH="$TMP_DIR/sendthings-terminal.sha256"
BIN_URL="$BASE_URL/terminal/bin/$VERSION/$TARGET/$BIN_NAME"
SHA_URL="$BASE_URL/terminal/bin/$VERSION/$TARGET/sendthings-terminal.sha256"

download() {
  SRC="$1"
  DEST="$2"

  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$SRC" -o "$DEST"
    return 0
  fi

  if command -v wget >/dev/null 2>&1; then
    wget -qO "$DEST" "$SRC"
    return 0
  fi

  echo "Neither curl nor wget is available." >&2
  return 1
}

download "$BIN_URL" "$BIN_PATH"
download "$SHA_URL" "$SHA_PATH"

EXPECTED=$(awk '{print $1}' "$SHA_PATH")
if [ -z "$EXPECTED" ]; then
  echo "Failed to read checksum for terminal binary." >&2
  exit 1
fi

if command -v sha256sum >/dev/null 2>&1; then
  ACTUAL=$(sha256sum "$BIN_PATH" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
  ACTUAL=$(shasum -a 256 "$BIN_PATH" | awk '{print $1}')
else
  echo "No SHA-256 tool found (sha256sum or shasum required)." >&2
  exit 1
fi

if [ "$EXPECTED" != "$ACTUAL" ]; then
  echo "Checksum mismatch for downloaded terminal binary." >&2
  exit 1
fi

chmod +x "$BIN_PATH"

TTY_STDIN=""
if [ -t 0 ]; then
  TTY_STDIN="/dev/stdin"
elif [ -r /dev/tty ]; then
  TTY_STDIN="/dev/tty"
fi

run_terminal() {
  if [ "$HAS_SIGNALING" -eq 0 ] && [ -n "$DEFAULT_SIGNALING_URL" ]; then
    if [ -n "$TTY_STDIN" ]; then
      exec "$BIN_PATH" --signaling-url "$DEFAULT_SIGNALING_URL" "$@" <"$TTY_STDIN"
    fi
    exec "$BIN_PATH" --signaling-url "$DEFAULT_SIGNALING_URL" "$@" </dev/null
  fi

  if [ -n "$TTY_STDIN" ]; then
    exec "$BIN_PATH" "$@" <"$TTY_STDIN"
  fi
  exec "$BIN_PATH" "$@" </dev/null
}

if [ "$HAS_SIGNALING" -eq 0 ] && [ -n "$DEFAULT_SIGNALING_URL" ]; then
  run_terminal "$@"
fi

run_terminal "$@"
