#!/bin/sh # Installatore di aKode. # # curl -fsSL https://git.difonzo.net/andrea/aKode/raw/branch/main/install.sh | sh # # Scarica l'ultima release (oppure quella indicata con AKODE_VERSION=x.y.z), ne verifica lo # SHA-256 e la installa in ~/.local/share/akode/package, con il comando in ~/.local/bin/akode. # Non usa sudo e non contatta altri host oltre a git.difonzo.net. Si può rieseguire senza danni. set -eu REPO_HOST="https://git.difonzo.net" REPO_PATH="andrea/aKode" API="$REPO_HOST/api/v1/repos/$REPO_PATH" DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/akode" PACKAGE_DIR="$DATA_DIR/package" BIN_DIR="$HOME/.local/bin" LINK="$BIN_DIR/akode" say() { printf '%s\n' "$*"; } die() { printf 'akode install: %s\n' "$*" >&2; exit 1; } need() { command -v "$1" >/dev/null 2>&1 || die "manca il comando '$1'"; } need curl need tar need uname need mktemp os=$(uname -s) arch=$(uname -m) case "$os/$arch" in Darwin/arm64 | Darwin/aarch64) target="aarch64-apple-darwin" ;; *) die "piattaforma non supportata: $os/$arch (per ora solo macOS Apple Silicon; su altri sistemi compila da sorgente)" ;; esac if command -v shasum >/dev/null 2>&1; then sha256() { shasum -a 256 "$1" | awk '{print $1}'; } elif command -v sha256sum >/dev/null 2>&1; then sha256() { sha256sum "$1" | awk '{print $1}'; } else die "serve 'shasum' o 'sha256sum' per verificare il download" fi tmp=$(mktemp -d "${TMPDIR:-/tmp}/akode-install.XXXXXX") trap 'rm -rf "$tmp"' EXIT trap 'exit 1' INT TERM fetch() { curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1"; } if [ -n "${AKODE_VERSION:-}" ]; then version=${AKODE_VERSION#v} else fetch "$API/releases/latest" "$tmp/latest.json" || die "impossibile leggere l'ultima release da $REPO_HOST" tag=$(tr -d '\n' < "$tmp/latest.json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') [ -n "$tag" ] || die "risposta inattesa dall'API delle release" version=${tag#v} fi case "$version" in *[!0-9A-Za-z.+-]* | "") die "versione non valida: $version" ;; esac installed="" if [ -f "$PACKAGE_DIR/akode-package.json" ]; then installed=$(tr -d '\n' < "$PACKAGE_DIR/akode-package.json" | sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') fi if [ "$installed" = "$version" ] && [ -x "$PACKAGE_DIR/bin/akode" ]; then say "aKode $version è già installato in $PACKAGE_DIR" else asset="akode-$version-$target.tar.gz" base="$REPO_HOST/$REPO_PATH/releases/download/v$version" say "Scarico aKode $version ($target)..." fetch "$base/$asset" "$tmp/$asset" || die "impossibile scaricare $asset" fetch "$base/$asset.sha256" "$tmp/$asset.sha256" || die "impossibile scaricare $asset.sha256" expected=$(awk -v f="$asset" '$2 == f || $2 == "*" f || NF == 1 {print $1; exit}' "$tmp/$asset.sha256") actual=$(sha256 "$tmp/$asset") [ -n "$expected" ] || die "il file $asset.sha256 non contiene l'hash di $asset" [ "$expected" = "$actual" ] || die "SHA-256 non corrispondente per $asset (atteso $expected, ottenuto $actual)" say "SHA-256 verificato." mkdir -p "$DATA_DIR" stage=$(mktemp -d "$DATA_DIR/.install.XXXXXX") if ! tar -xzf "$tmp/$asset" -C "$stage"; then rm -rf "$stage" die "archivio non valido: $asset" fi new="$stage/akode-$version-$target" if [ ! -f "$new/akode-package.json" ] || [ ! -x "$new/bin/akode" ]; then rm -rf "$stage" die "l'archivio non contiene un pacchetto aKode completo" fi rm -rf "$PACKAGE_DIR.old" if [ -e "$PACKAGE_DIR" ]; then mv "$PACKAGE_DIR" "$PACKAGE_DIR.old" fi if ! mv "$new" "$PACKAGE_DIR"; then [ -e "$PACKAGE_DIR.old" ] && mv "$PACKAGE_DIR.old" "$PACKAGE_DIR" rm -rf "$stage" die "impossibile installare in $PACKAGE_DIR" fi rm -rf "$stage" "$PACKAGE_DIR.old" say "Installato aKode $version in $PACKAGE_DIR" fi mkdir -p "$BIN_DIR" if [ -e "$LINK" ] && [ ! -L "$LINK" ]; then die "$LINK esiste e non è un collegamento di aKode: rimuovilo e rilancia l'installatore" fi ln -sfn "$PACKAGE_DIR/bin/akode" "$LINK" say "Comando: $LINK" case ":${PATH:-}:" in *":$BIN_DIR:"*) ;; *) say "" say "$BIN_DIR non è nel PATH. Aggiungilo, per esempio con:" say " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> ~/.zshrc && exec zsh" ;; esac if ! command -v rg >/dev/null 2>&1; then say "" say "Consigliato: brew install ripgrep (ricerche più veloci)" fi say "" say "Avvia con: akode (aggiornamenti: akode update)"