#!/bin/sh
set -eu

BASE_URL="https://talkbank.org/downloads/chatter/releases/preview-20260330T023804Z"
TMP_DIR="${TMPDIR:-/tmp}"
TARBALL="${TMP_DIR}/chatter-macos-arm64.tar.gz"
BIN_TMP="${TMP_DIR}/chatter"

echo "Chatter preview installer"

OS="$(uname -s)"
ARCH="$(uname -m)"

if [ "$OS" != "Darwin" ]; then
  echo "This preview installer currently supports macOS only." >&2
  exit 1
fi

if [ "$ARCH" != "arm64" ]; then
  echo "This preview installer currently supports Apple Silicon (arm64) only." >&2
  echo "Please check chatter.html for current platform availability." >&2
  exit 1
fi

echo "Downloading chatter preview..."
curl -fsSL "${BASE_URL}/chatter-macos-arm64.tar.gz" -o "$TARBALL"
tar -xzf "$TARBALL" -C "$TMP_DIR"

choose_default_dir() {
  old_ifs="$IFS"
  IFS=":"
  first_existing=""
  for d in $PATH; do
    [ -n "$d" ] || continue
    [ -d "$d" ] || continue
    if [ -z "$first_existing" ]; then
      first_existing="$d"
    fi
    if [ -w "$d" ]; then
      IFS="$old_ifs"
      printf "%s\n" "$d"
      return 0
    fi
  done
  IFS="$old_ifs"
  if [ -n "$first_existing" ]; then
    printf "%s\n" "$first_existing"
    return 0
  fi
  if [ -d "/usr/local/bin" ]; then
    printf "%s\n" "/usr/local/bin"
    return 0
  fi
  if [ -d "/opt/homebrew/bin" ]; then
    printf "%s\n" "/opt/homebrew/bin"
    return 0
  fi
  printf "%s\n" "/usr/local/bin"
  return 0
}

find_existing_installs() {
  old_ifs="$IFS"
  seen=""
  IFS=":"
  for d in $PATH; do
    [ -n "$d" ] || continue
    [ -d "$d" ] || continue
    p="$d/chatter"
    [ -x "$p" ] || continue
    case " $seen " in
      *" $p "*)
        ;;
      *)
        printf "%s\n" "$p"
        seen="${seen}$p "
        ;;
    esac
  done
  IFS="$old_ifs"
  for p in "/usr/local/bin/chatter" "/opt/homebrew/bin/chatter" "$HOME/.local/bin/chatter"; do
    [ -x "$p" ] || continue
    case " $seen " in
      *" $p "*)
        ;;
      *)
        printf "%s\n" "$p"
        seen="${seen}$p "
        ;;
    esac
  done
}

existing_bins="$(find_existing_installs)"
path_bin="$(command -v chatter 2>/dev/null || true)"
if [ -n "$path_bin" ]; then
  default_dst="$path_bin"
elif [ -n "$existing_bins" ]; then
  default_dst="$(printf "%s\n" "$existing_bins" | head -n 1)"
else
  default_dst="$(choose_default_dir)/chatter"
fi

install_dst="$default_dst"
if [ -n "$existing_bins" ]; then
  echo "Found existing chatter executable(s):"
  printf "%s\n" "$existing_bins" | sed 's/^/  - /'
fi
if [ -r /dev/tty ]; then
  printf "Install path [%s]: " "$default_dst" > /dev/tty
  answer=""
  read answer < /dev/tty || true
  if [ -n "$answer" ]; then
    install_dst="$answer"
  fi
fi

# Expand a leading "~/" manually.
case "$install_dst" in
  "~/"*)
    install_dst="${HOME}/${install_dst#~/}"
    ;;
esac

# If user supplied a directory-ish target, append executable name.
case "$install_dst" in
  */)
    install_dst="${install_dst}chatter"
    ;;
esac

if [ -d "$install_dst" ]; then
  install_dst="${install_dst%/}/chatter"
fi

install_dir="$(dirname "$install_dst")"
needs_sudo="0"
if [ ! -d "$install_dir" ]; then
  needs_sudo="1"
elif [ ! -w "$install_dir" ]; then
  needs_sudo="1"
fi

if [ -e "$install_dst" ] && [ -r /dev/tty ]; then
  printf "Overwrite existing file at %s? [Y/n] " "$install_dst" > /dev/tty
  answer=""
  read answer < /dev/tty || true
  case "$answer" in
    n|N|no|NO)
      echo "Install cancelled."
      exit 1
      ;;
  esac
fi

echo "Installing to ${install_dst}..."
if [ "$needs_sudo" = "1" ]; then
  sudo mkdir -p "$install_dir"
  sudo install -m 755 "$BIN_TMP" "$install_dst"
else
  mkdir -p "$install_dir"
  install -m 755 "$BIN_TMP" "$install_dst"
fi

# Quarantine flag is present for some download paths. Ask user before removal.
if [ -r /dev/tty ]; then
  printf "Remove macOS quarantine attribute from %s? [y/N] " "$install_dst" > /dev/tty
  answer=""
  read answer < /dev/tty || true
  case "$answer" in
    y|Y|yes|YES)
      if [ "$needs_sudo" = "1" ]; then
        sudo xattr -d com.apple.quarantine "$install_dst" 2>/dev/null || true
      else
        xattr -d com.apple.quarantine "$install_dst" 2>/dev/null || true
      fi
      ;;
    *)
      echo "Skipped quarantine attribute removal."
      ;;
  esac
else
  echo "No interactive terminal detected; skipped quarantine attribute removal."
fi

echo "Install complete."
resolved_path="$(command -v chatter 2>/dev/null || true)"
if [ -n "$resolved_path" ]; then
  echo "Try: chatter --help"
  if [ "$resolved_path" != "$install_dst" ]; then
    echo "Note: PATH currently resolves 'chatter' to: ${resolved_path}"
    echo "New install is at: ${install_dst}"
  fi
else
  echo "Try: ${install_dst} --help"
  echo "Note: 'chatter' is not currently on PATH; use: ${install_dst}"
fi
