#!/bin/sh # Installs the languid CLI, or updates it: curl -fsSL https://languid.ai/install | sh # # Needs Node 22.18 or newer, curl and tar. No git, no pnpm: the code comes as a tarball, and # the pinned pnpm runs through npx. The code goes in ~/.languid/app, a `languid` command in # ~/.local/bin. Running it again updates both. Then, on a first install from a terminal, it # signs you in (LANGUID_NO_LOGIN=1 skips that). # # The service fills in @SERVICE@ and @TARBALL@ when it serves this file. set -eu SERVICE="${LANGUID_SERVER:-@SERVICE@}" TARBALL="${LANGUID_TARBALL:-@TARBALL@}" APP="${LANGUID_APP_DIR:-$HOME/.languid/app}" BIN="${LANGUID_BIN_DIR:-$HOME/.local/bin}" say() { printf '%s\n' "$@"; } fail() { printf 'languid install: %s\n' "$*" >&2; exit 1; } sh_quote() { _sq_in=$1 _sq_out="'" while :; do case "$_sq_in" in *"'"*) _sq_out="$_sq_out${_sq_in%%"'"*}'\\''" _sq_in=${_sq_in#*"'"} ;; *) _sq_out="$_sq_out$_sq_in'" break ;; esac done printf '%s' "$_sq_out" } [ -n "$APP" ] && [ -n "$BIN" ] || fail "install paths cannot be empty" nl=' ' cr=$(printf '\r') for p in "$APP" "$BIN"; do case "$p" in *"$nl"*|*"$cr"*) fail "install paths cannot contain newlines or carriage returns" ;; esac done command -v curl >/dev/null 2>&1 || fail "needs curl" command -v tar >/dev/null 2>&1 || fail "needs tar" command -v node >/dev/null 2>&1 || fail "needs Node 22.18 or newer: https://nodejs.org" command -v npx >/dev/null 2>&1 || fail "needs npx, which comes with Node: https://nodejs.org" node -e 'const [a, b] = process.versions.node.split(".").map(Number); process.exit(a > 22 || (a === 22 && b >= 18) ? 0 : 1)' || fail "needs Node 22.18 or newer (this is $(node --version)): https://nodejs.org" mkdir -p "$(dirname "$APP")" "$BIN" && chmod 700 "$(dirname "$APP")" NEW="$APP.new.$$" OLD="$APP.old.$$" trap 'rm -rf "$NEW" "$NEW.tgz"' EXIT INT TERM say "downloading languid…" curl -fsSL "$TARBALL" -o "$NEW.tgz" || fail "could not download $TARBALL" mkdir "$NEW" tar -xzf "$NEW.tgz" -C "$NEW" --strip-components=1 || fail "could not unpack $TARBALL" [ -f "$NEW/src/cli.ts" ] || fail "$TARBALL doesn't look like languid" # Swap it in before installing: pnpm writes absolute paths into node_modules/.bin. first=yes if [ -d "$APP" ]; then first=; mv "$APP" "$OLD"; fi mv "$NEW" "$APP" say "installing its dependencies…" if ! (cd "$APP" && NPM_CONFIG_UPDATE_NOTIFIER=false npx --yes "pnpm@$(node -p 'require("./package.json").packageManager.split("@")[1]')" install --frozen-lockfile --reporter=silent); then rm -rf "$APP" [ -d "$OLD" ] && mv "$OLD" "$APP" fail "installing dependencies failed; nothing changed" fi rm -rf "$OLD" CLI_Q=$(sh_quote "$APP/src/cli.ts") printf '#!/bin/sh\nexec node %s "$@"\n' "$CLI_Q" > "$BIN/languid" chmod +x "$BIN/languid" say "installed: $BIN/languid" # A computer set up from this install keeps running the old code until its runner restarts. if [ -z "$first" ]; then if [ "$(uname)" = Darwin ]; then # Languid.app runs the runners set up with it; restarting it restarts them all. if grep -qsF -- "$APP/src/cli.ts" "$HOME/Library/Application Support/Languid/runners/"*.json && launchctl kickstart -k "gui/$(id -u)/ai.languid.app" >/dev/null 2>&1; then say "restarted Languid.app's runners" fi for label in $(launchctl list 2>/dev/null | awk '$3 ~ /^ai\.languid\.runner/ { print $3 }'); do launchctl print "gui/$(id -u)/$label" 2>/dev/null | grep -qF -- "$APP/src/cli.ts" && launchctl kickstart -k "gui/$(id -u)/$label" >/dev/null 2>&1 && say "restarted this computer's runner ($label)" done elif command -v systemctl >/dev/null 2>&1; then for unit in $(systemctl --user list-units --plain --no-legend 'languid-runner*' 2>/dev/null | awk '{ print $1 }'); do systemctl --user cat "$unit" 2>/dev/null | grep -qF -- "$APP/src/cli.ts" && systemctl --user restart "$unit" && say "restarted this computer's runner ($unit)" done # The tray (src/tray.ts) runs the old code until it restarts too. if pkill -f "$APP/src/cli.ts computer tray" 2>/dev/null && { [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; }; then sleep 1 log="${XDG_STATE_HOME:-$HOME/.local/state}/languid/tray.log" mkdir -p "$(dirname "$log")" (cd && nohup node "$APP/src/cli.ts" computer tray >>"$log" 2>&1 &) && say "restarted the Languid tray" fi fi fi # Put ~/.local/bin on PATH for new shells, once. BIN_Q=$(sh_quote "$BIN") case ":$PATH:" in *":$BIN:"*) onpath=yes ;; *) onpath= case "${SHELL:-}" in */zsh) rc="$HOME/.zshrc" ;; */bash) [ "$(uname)" = Darwin ] && rc="$HOME/.bash_profile" || rc="$HOME/.bashrc" ;; *) rc="$HOME/.profile" ;; esac line="export PATH=$BIN_Q:\"\$PATH\" # languid" if ! grep -qsF -- "$line" "$rc"; then printf '\n%s\n' "$line" >> "$rc" say "added $BIN to PATH in $rc" fi ;; esac newshell= [ -n "$onpath" ] || newshell="open a new terminal (or run: export PATH=$BIN_Q:\"\$PATH\"), then " if [ -z "$first" ]; then say "updated" elif [ -z "${LANGUID_NO_LOGIN:-}" ] && [ -t 1 ] && (: /dev/null; then say "" if "$BIN/languid" login --url "$SERVICE"