101 lines
4.8 KiB
Shell
101 lines
4.8 KiB
Shell
#!/bin/sh
|
|
# Installer of the aio command (Interweb All in One AI) for Linux and macOS. Generated by the docs build.
|
|
# Usage: curl -fsSL https://docs.interweb.ao/cli/install.sh | sh
|
|
# curl -fsSL https://docs.interweb.ao/cli/install.sh | sh -s -- --uninstall
|
|
# Never asks for sudo: installs in ~/.local/bin/aio (or in the AIO_INSTALL_DIR folder).
|
|
# The body is inside main(), so nothing runs if the download is cut short.
|
|
|
|
main() {
|
|
set -eu
|
|
|
|
AIO_FROM="https://docs.interweb.ao/cli"
|
|
AIO_VERSION="1.0.1"
|
|
DEST_DIR="${AIO_INSTALL_DIR:-$HOME/.local/bin}"
|
|
DEST="$DEST_DIR/aio"
|
|
|
|
case "${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}" in
|
|
pt*|PT*) PT=1 ;;
|
|
*) PT=0 ;;
|
|
esac
|
|
|
|
say() { if [ "$PT" = 1 ]; then printf '%s\n' "$1"; else printf '%s\n' "$2"; fi; }
|
|
fail() { if [ "$PT" = 1 ]; then printf 'Erro: %s\n' "$1" >&2; else printf 'Error: %s\n' "$2" >&2; fi; exit 1; }
|
|
|
|
if [ "${1:-}" = "--uninstall" ]; then
|
|
if [ -f "$DEST" ]; then
|
|
rm -f "$DEST"
|
|
say "O aio foi removido de $DEST." "aio was removed from $DEST."
|
|
else
|
|
say "O aio não está instalado em $DEST." "aio is not installed in $DEST."
|
|
fi
|
|
say "A configuração com a chave fica em ~/.config/aio. Para a apagar: rm -rf ~/.config/aio" \
|
|
"Your settings and key stay in ~/.config/aio. To delete them: rm -rf ~/.config/aio"
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "${1:-}" ]; then
|
|
fail "opção desconhecida: $1 (use --uninstall ou nenhuma opção)" "unknown option: $1 (use --uninstall or nothing)"
|
|
fi
|
|
|
|
say "A instalar o comando aio $AIO_VERSION (Interweb All in One AI)..." \
|
|
"Installing the aio command $AIO_VERSION (Interweb All in One AI)..."
|
|
|
|
# 1. Node.js 18 or newer
|
|
if ! command -v node >/dev/null 2>&1; then
|
|
fail "o aio precisa do Node.js 18 ou mais recente, e o Node.js não foi encontrado. Instale-o em https://nodejs.org e volte a correr este comando." \
|
|
"aio needs Node.js 18 or later, and Node.js was not found. Install it from https://nodejs.org and run this command again."
|
|
fi
|
|
NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)
|
|
case "$NODE_MAJOR" in
|
|
''|*[!0-9]*) NODE_MAJOR=0 ;;
|
|
esac
|
|
if [ "$NODE_MAJOR" -lt 18 ]; then
|
|
fail "o aio precisa do Node.js 18 ou mais recente, e este computador tem o $(node --version 2>/dev/null || echo '?'). Actualize-o em https://nodejs.org e volte a correr este comando." \
|
|
"aio needs Node.js 18 or later, and this computer has $(node --version 2>/dev/null || echo '?'). Update it from https://nodejs.org and run this command again."
|
|
fi
|
|
|
|
# 2. Download over https
|
|
if command -v curl >/dev/null 2>&1; then
|
|
fetch() { curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1"; }
|
|
elif command -v wget >/dev/null 2>&1; then
|
|
fetch() { wget -q --https-only -O "$2" "$1"; }
|
|
else
|
|
fail "é preciso o curl ou o wget para descarregar o aio." "curl or wget is needed to download aio."
|
|
fi
|
|
|
|
TMP=$(mktemp -d 2>/dev/null || mktemp -d -t aio)
|
|
trap 'rm -rf "$TMP"' EXIT INT TERM
|
|
|
|
fetch "$AIO_FROM/aio.js" "$TMP/aio.js" || fail "não foi possível descarregar $AIO_FROM/aio.js." "could not download $AIO_FROM/aio.js."
|
|
fetch "$AIO_FROM/SHA256SUMS" "$TMP/SHA256SUMS" || fail "não foi possível descarregar $AIO_FROM/SHA256SUMS." "could not download $AIO_FROM/SHA256SUMS."
|
|
|
|
# 3. Check the SHA256 (with Node itself, which exists on every system where aio runs)
|
|
EXPECTED=$(awk '$2 == "aio.js" || $2 == "*aio.js" { print $1 }' "$TMP/SHA256SUMS")
|
|
ACTUAL=$(node -e 'const c=require("crypto"),f=require("fs");process.stdout.write(c.createHash("sha256").update(f.readFileSync(process.argv[1])).digest("hex"))' "$TMP/aio.js")
|
|
if [ -z "$EXPECTED" ] || [ "$EXPECTED" != "$ACTUAL" ]; then
|
|
fail "o SHA256 do ficheiro descarregado não confere. Nada foi instalado. Tente de novo dentro de alguns minutos." \
|
|
"the SHA256 of the downloaded file does not match. Nothing was installed. Try again in a few minutes."
|
|
fi
|
|
|
|
# 4. Install in ~/.local/bin/aio
|
|
mkdir -p "$DEST_DIR" || fail "não foi possível criar a pasta $DEST_DIR." "could not create the folder $DEST_DIR."
|
|
cp "$TMP/aio.js" "$TMP/aio.new"
|
|
chmod 755 "$TMP/aio.new"
|
|
mv -f "$TMP/aio.new" "$DEST" || fail "não foi possível escrever em $DEST." "could not write to $DEST."
|
|
|
|
say "Pronto: o aio ficou instalado em $DEST." "Done: aio is installed in $DEST."
|
|
|
|
case ":$PATH:" in
|
|
*":$DEST_DIR:"*)
|
|
say "Para começar: aio auth login" "To start: aio auth login"
|
|
;;
|
|
*)
|
|
say "A pasta $DEST_DIR não está no PATH. Junte esta linha ao ~/.bashrc ou ao ~/.zshrc e abra um terminal novo:" \
|
|
"The folder $DEST_DIR is not in your PATH. Add this line to ~/.bashrc or ~/.zshrc and open a new terminal:"
|
|
printf ' export PATH="%s:$PATH"\n' "$DEST_DIR"
|
|
say "Depois: aio auth login" "Then: aio auth login"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@"
|