#!/bin/sh
# Install the ix CLI.
# Usage: curl -fsSL https://ix.dev/install.sh | sh
#
# Respects IX_INSTALL_DIR to override the install location.
# Supports bash, zsh, fish, and nushell.
set -eu

BASE_URL="https://ix.dev/cli"
INSTALL_DIR="${IX_INSTALL_DIR:-$HOME/.local/bin}"

main() {
  platform="$(detect_platform)"
  download "$platform"
  configure_path
  echo ""
  echo "ix installed successfully. Run 'ix' to get started."
}

detect_platform() {
  os="$(uname -s)"
  arch="$(uname -m)"

  case "$os" in
  Linux) os="linux" ;;
  Darwin) os="darwin" ;;
  *) error "unsupported OS: $os (expected Linux or macOS)" ;;
  esac

  case "$arch" in
  x86_64 | amd64) arch="x86_64" ;;
  arm64 | aarch64) arch="arm64" ;;
  *) error "unsupported architecture: $arch (expected x86_64 or arm64)" ;;
  esac

  platform="${os}-${arch}"

  case "$platform" in
  linux-x86_64 | darwin-arm64) ;;
  linux-arm64) error "linux-arm64 is not yet supported" ;;
  darwin-x86_64) error "Intel Macs (darwin-x86_64) are not supported" ;;
  *) error "unsupported platform: $platform" ;;
  esac

  echo "$platform"
}

download() {
  platform="$1"
  url="${BASE_URL}/${platform}/ix"
  tmp="$(mktemp)"
  trap 'rm -f "$tmp"' 0

  echo "Downloading ix for ${platform} to ${INSTALL_DIR}..."

  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$url" -o "$tmp"
  elif command -v wget >/dev/null 2>&1; then
    wget -qO "$tmp" "$url"
  else
    error "curl or wget is required"
  fi

  mkdir -p "$INSTALL_DIR"
  chmod +x "$tmp"
  mv -f "$tmp" "$INSTALL_DIR/ix"
  trap - 0
}

configure_path() {
  case ":$PATH:" in
  *":$INSTALL_DIR:"*) return ;;
  esac

  shell_name="$(basename "${SHELL:-bash}")"

  case "$shell_name" in
  bash)
    rc_file="$HOME/.bashrc"
    if [ ! -f "$rc_file" ]; then
      rc_file="$HOME/.bash_profile"
    fi
    append_path_line "$rc_file" "export PATH=\"$INSTALL_DIR:\$PATH\""
    ;;
  zsh)
    rc_file="$HOME/.zshrc"
    append_path_line "$rc_file" "export PATH=\"$INSTALL_DIR:\$PATH\""
    ;;
  fish)
    rc_file="$HOME/.config/fish/config.fish"
    mkdir -p "$(dirname "$rc_file")"
    append_path_line "$rc_file" "fish_add_path $INSTALL_DIR"
    ;;
  nu | nushell)
    rc_file="$HOME/.config/nushell/env.nu"
    if [ -f "$rc_file" ]; then
      append_path_line "$rc_file" "\$env.PATH = (\$env.PATH | prepend \"$INSTALL_DIR\")"
    else
      echo "Add to your nushell config: \$env.PATH = (\$env.PATH | prepend \"$INSTALL_DIR\")"
      return
    fi
    ;;
  *)
    echo "Add $INSTALL_DIR to your PATH."
    return
    ;;
  esac

  echo "Restart your shell or run:"
  case "$shell_name" in
  fish) echo "  fish_add_path $INSTALL_DIR" ;;
  nu | nushell) echo "  \$env.PATH = (\$env.PATH | prepend \"$INSTALL_DIR\")" ;;
  *) echo "  export PATH=\"$INSTALL_DIR:\$PATH\"" ;;
  esac
}

append_path_line() {
  file="$1"
  line="$2"
  if [ -f "$file" ] && grep -qF "$INSTALL_DIR" "$file" 2>/dev/null; then
    return
  fi
  printf '\n# ix CLI\n%s\n' "$line" >>"$file"
  echo "Updated $file"
}

error() {
  echo "Error: $1" >&2
  exit 1
}

main
