#!/bin/sh
# 看看客户端一键安装（Linux / macOS）· Looklook client one-click installer
#
#   curl -fsSL https://looklook.dev/install.sh | sh          安装给当前用户（推荐）/ install for the current user
#   curl -fsSL https://looklook.dev/install.sh | sudo sh     安装为系统服务 / install as a system service (Linux)
#   curl -fsSL https://looklook.dev/install.sh | sh -s -- --key LLK-…   装完用安装密钥登录（云服务器、开机脚本；在网页“电脑”页生成）
#                                                            / sign in with an install key (cloud servers, startup scripts)
#
# 下载本机系统对应的最新版本，校验 SHA-256 后运行安装包里的 install.sh。
# 再次运行即升级到最新版本；终端里正在运行的任务不受影响。
# Downloads the latest release for this system, verifies its SHA-256 and runs the bundled install.sh.
# Run it again to upgrade; tasks running in your terminals keep running.
#
# 本文件由看看服务端按当前发布的版本生成 / Generated by the Looklook server from the current releases.
set -eu

ORIGIN='https://looklook.dev'
# 目标|版本|下载地址|SHA-256（每个平台所选渠道的最新版）/ target|version|url|sha256 (latest in the chosen channel per platform)
PACKAGES='linux-x86_64|1.58.0|https://looklook.dev/downloads/1.58.0/looklook-1.58.0-linux-x86_64.tar.gz|11e4ba2fc2dfe3d6ae6bc5acd203da6603b1b2f5dd74e034e58f9c28b7c42f86
linux-aarch64|1.58.0|https://looklook.dev/downloads/1.58.0/looklook-1.58.0-linux-aarch64.tar.gz|1792d14518882477592e10f1ba5f3c05615cf885c05115ec8ad047c7747fa2f5
darwin-aarch64|1.58.0|https://looklook.dev/downloads/1.58.0/looklook-1.58.0-darwin-aarch64.tar.gz|d37f95b7a685c7aadd21241e0e24fe0b609a60a2f8bb4a5559769029d9cf0388'

say() { printf '%s\n' "$*"; }
die() { printf '✗ %s\n' "$*" >&2; exit 1; }
usage() { die "用法 / Usage: curl -fsSL $ORIGIN/install.sh | sh [-s -- --key LLK-…]"; }

# 安装密钥也可以用环境变量 LOOKLOOK_INSTALL_KEY 给（sudo 默认不传环境变量，所以推荐 --key）
KEY=${LOOKLOOK_INSTALL_KEY:-}
while [ $# -gt 0 ]; do
  case $1 in
    --key) [ $# -ge 2 ] || usage; KEY=$2; shift ;;
    --key=*) KEY=${1#--key=} ;;
    -h | --help) usage ;;
    *) die "未知选项 / Unknown option: $1" ;;
  esac
  shift
done
case $KEY in '' | LLK-*) ;; *) die "安装密钥以 LLK- 开头 / Install keys start with LLK-" ;; esac

case "$(uname -s)-$(uname -m)" in
  Linux-x86_64 | Linux-amd64) target=linux-x86_64 ;;
  Linux-aarch64 | Linux-arm64) target=linux-aarch64 ;;
  Darwin-arm64) target=darwin-aarch64 ;;
  Darwin-x86_64) target=darwin-x86_64 ;;
  *) die "暂不支持这个系统 / Unsupported system: $(uname -s) $(uname -m)（请到 $ORIGIN/download 查看 / see $ORIGIN/download）" ;;
esac

line=$(printf '%s\n' "$PACKAGES" | grep "^$target|" | head -n 1 || true)
[ -n "$line" ] || die "$target 暂时没有可下载的版本 / No release available for $target yet（$ORIGIN/download）"
version=$(printf '%s' "$line" | cut -d'|' -f2)
url=$(printf '%s' "$line" | cut -d'|' -f3)
sha=$(printf '%s' "$line" | cut -d'|' -f4)

for c in tar gzip; do command -v "$c" >/dev/null 2>&1 || die "缺少 $c / $c is required"; done
if command -v curl >/dev/null 2>&1; then fetch() { curl -fL --retry 3 --connect-timeout 20 --progress-bar -o "$2" "$1"; }
elif command -v wget >/dev/null 2>&1; then fetch() { wget -q --show-progress -O "$2" "$1"; }
else die "需要 curl 或 wget / curl or wget is required"; fi
if command -v sha256sum >/dev/null 2>&1; then sha256() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then sha256() { shasum -a 256 "$1" | cut -d' ' -f1; }
else die "需要 sha256sum 或 shasum / sha256sum or shasum is required"; fi

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

say "==> 下载看看 ${version}（${target}）/ Downloading Looklook $version ($target)"
fetch "$url" "$tmp/looklook.tar.gz"
[ "$(sha256 "$tmp/looklook.tar.gz")" = "$sha" ] || die "文件校验失败，请重试 / Checksum mismatch, please try again"
tar -xzf "$tmp/looklook.tar.gz" -C "$tmp"
[ -f "$tmp/looklook/install.sh" ] || die "安装包不完整 / Incomplete package"

# Linux 上以 root 运行且没有 tmux 时顺便安装（没有它，关掉终端页面后任务会结束）。
# On Linux as root, install tmux if missing (without it, tasks stop when the terminal page is closed).
if [ "$target" != "${target#linux}" ] && [ "$(id -u)" = 0 ] && ! command -v tmux >/dev/null 2>&1; then
  say "==> 安装 tmux / Installing tmux"
  if command -v apt-get >/dev/null 2>&1; then apt-get install -y -q tmux >/dev/null 2>&1 || { apt-get update -q >/dev/null 2>&1 && apt-get install -y -q tmux >/dev/null 2>&1; } || true
  elif command -v dnf >/dev/null 2>&1; then dnf install -y -q tmux >/dev/null 2>&1 || true
  elif command -v yum >/dev/null 2>&1; then yum install -y -q tmux >/dev/null 2>&1 || true
  elif command -v pacman >/dev/null 2>&1; then pacman -S --noconfirm tmux >/dev/null 2>&1 || true
  elif command -v apk >/dev/null 2>&1; then apk add -q tmux >/dev/null 2>&1 || true
  elif command -v zypper >/dev/null 2>&1; then zypper -q install -y tmux >/dev/null 2>&1 || true
  fi
fi

say "==> 安装 / Installing"
# 安装包里的 install.sh 启动服务后用 LOOKLOOK_INSTALL_KEY 登录
if [ -n "$KEY" ]; then LOOKLOOK_INSTALL_KEY=$KEY sh "$tmp/looklook/install.sh"; else sh "$tmp/looklook/install.sh"; fi
say ""
say "使用说明 / Guide: $ORIGIN/docs"
