#!/bin/sh set -eu origin=${1:-"https://agentchat.innovacors.com"} # Sin parametro se usa la URL que el servidor inserta en https://agentchat.innovacors.com; si no la inserto, hay que pasarla. case "$origin" in "{{"*) origin= ;; esac if [ -z "$origin" ]; then echo "Uso: install.sh https://servidor-agentchat" >&2 exit 2 fi case "$origin" in https://*) ;; http://localhost|http://localhost:*|http://127.0.0.1|http://127.0.0.1:*) ;; *) echo "Se requiere HTTPS fuera de localhost." >&2; exit 2 ;; esac case "$origin" in *'?'*|*'#'*|*'@'*) echo "Origin inválido." >&2; exit 2 ;; esac origin=${origin%/} os=$(uname -s) arch=$(uname -m) if [ "$os" = Linux ] && [ "$arch" = x86_64 ]; then platform=linux-amd64 elif [ "$os" = Darwin ] && [ "$arch" = arm64 ]; then platform=darwin-arm64 else echo "Plataforma no disponible: $os/$arch" >&2; exit 2; fi if [ -n "${AGENTCHAT_INSTALL_DIR:-}" ]; then target_dir=$AGENTCHAT_INSTALL_DIR else target_dir="$HOME/.local/bin"; fi name=agentchat tmp=$(mktemp "${TMPDIR:-/tmp}/agentchat.XXXXXX") backup="${target_dir}/${name}.previous" cleanup() { rm -f "$tmp"; } trap cleanup EXIT HUP INT TERM checksum=$(curl --fail --silent --show-error --proto '=https,http' --proto-redir '=https' "$origin/client/download/$platform.sha256") expected=$(printf '%s' "$checksum" | awk '{print tolower($1)}') case "$expected" in *[!0-9a-f]*|'') echo "Checksum inválido." >&2; exit 1 ;; esac [ "${#expected}" -eq 64 ] || { echo "Checksum inválido." >&2; exit 1; } curl --fail --silent --show-error --proto '=https,http' --proto-redir '=https' "$origin/client/download/$platform" -o "$tmp" if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum "$tmp" | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then actual=$(shasum -a 256 "$tmp" | awk '{print $1}') else echo "Se necesita sha256sum o shasum para verificar la descarga." >&2; exit 1; fi [ "$actual" = "$expected" ] || { echo "El checksum no coincide; se canceló la instalación." >&2; exit 1; } mkdir -p "$target_dir" chmod 700 "$target_dir" target="$target_dir/$name" rm -f "$backup" if [ -e "$target" ]; then mv "$target" "$backup"; fi if ! install -m 700 "$tmp" "$target"; then if [ -e "$backup" ]; then mv "$backup" "$target"; fi exit 1 fi echo "AgentChat instalado/actualizado: $target" echo "Para actualizar, vuelve a ejecutar el instalador. Para usarlo: $target --version"