#!/bin/sh # Cerebro Desktop - Linux installer # # Install / upgrade / repair: curl -fsSL https://cerebro.life/install.sh | sh # Uninstall: curl -fsSL https://cerebro.life/install.sh | sh -s -- --uninstall # # What it does (no sudo, ever - everything lives in your home folder): # 1. Downloads the latest Cerebro-Installer.AppImage from GitHub # (github.com/Professor-Low/Cerebro-Installer - the same file the website's # Download button serves). # 2. Verifies its SHA-512 against latest-linux.yml from the same release. # A mismatch deletes the file and aborts. # 3. Installs it to ~/.local/share/cerebro/Cerebro.AppImage (user-writable, so # the app's built-in updater can replace it in place) and adds a # ~/.local/bin/cerebro launcher. The launcher starts Cerebro detached from # the terminal (its own session, output to ~/.cache/cerebro/cerebro.log), # so closing the terminal never closes Cerebro. `cerebro --foreground` # keeps it attached for debugging. # 4. Adds Cerebro to your app menu: ~/.local/share/applications/cerebro.desktop # + the cerebro-desktop icon. These are the same files the app writes on its # own first launch, so you never get a duplicate menu entry. # 5. Puts a Cerebro shortcut on your desktop (if you have a Desktop folder) # and opens Cerebro, detached, when a graphical session is available. # # Options (after `sh -s --`): --no-shortcut skip the desktop icon # --no-launch don't open Cerebro at the end # # Re-running it upgrades or repairs. It never touches your Cerebro data or # settings, sends nothing anywhere, and license keys / updates stay in the app. set -eu RELEASE_URL="https://github.com/Professor-Low/Cerebro-Installer/releases/latest/download" ASSET="Cerebro-Installer.AppImage" # Paths are fixed to ~/.local/share (not $XDG_DATA_HOME) on purpose: the app's # first-launch desktop integration hardcodes them, and both must agree on one # cerebro.desktop or the menu shows Cerebro twice. SHARE_DIR="$HOME/.local/share" INSTALL_DIR="$SHARE_DIR/cerebro" APP="$INSTALL_DIR/Cerebro.AppImage" BIN_DIR="$HOME/.local/bin" BIN="$BIN_DIR/cerebro" DESKTOP_DIR="$SHARE_DIR/applications" DESKTOP_FILE="$DESKTOP_DIR/cerebro.desktop" ICON_ROOT="$SHARE_DIR/icons/hicolor" LOG_DIR="$HOME/.cache/cerebro" LAUNCHER_MARK="# cerebro-launcher: written by cerebro.life/install.sh" WANT_SHORTCUT=1 WANT_LAUNCH=1 TMP_DIR="" if [ -t 1 ]; then B="$(printf '\033[1m')"; DIM="$(printf '\033[2m')"; RED="$(printf '\033[31m')" GRN="$(printf '\033[32m')"; YEL="$(printf '\033[33m')"; R="$(printf '\033[0m')" else B=""; DIM=""; RED=""; GRN=""; YEL=""; R="" fi say() { printf '%s\n' "$*"; } step() { printf '%s==>%s %s\n' "$B" "$R" "$*"; } ok() { printf '%s ok%s %s\n' "$GRN" "$R" "$*"; } warn() { printf '%s !%s %s\n' "$YEL" "$R" "$*" >&2; } die() { printf '%sError:%s %s\n' "$RED$B" "$R" "$*" >&2; exit 1; } cleanup() { [ -n "$TMP_DIR" ] && rm -rf "$TMP_DIR" rm -f "$APP.download" # Aborted before anything was installed: don't leave an empty folder behind. rmdir "$INSTALL_DIR" 2>/dev/null || true } have() { command -v "$1" >/dev/null 2>&1; } # fetch URL DEST [progress] fetch() { if have curl; then if [ "${3:-}" = progress ] && [ -t 2 ]; then curl -fL --retry 3 --progress-bar -o "$2" "$1" else curl -fsSL --retry 3 -o "$2" "$1" fi else if [ "${3:-}" = progress ] && [ -t 2 ]; then wget -q --show-progress -O "$2" "$1" || wget -O "$2" "$1" else wget -q -O "$2" "$1" fi fi } # Hex SHA-512 of a file. sha512_hex() { sha512sum "$1" | cut -d ' ' -f 1; } # electron-builder publishes base64 digests; sha512sum prints hex. b64_to_hex() { printf '%s' "$1" | base64 -d | od -An -v -tx1 | tr -d ' \n'; } usage() { say "Cerebro Desktop installer for Linux" say "" say " curl -fsSL https://cerebro.life/install.sh | sh install, upgrade or repair" say " curl -fsSL https://cerebro.life/install.sh | sh -s -- --uninstall remove the app (keeps your data)" say " curl -fsSL https://cerebro.life/install.sh | sh -s -- --no-shortcut skip the desktop icon" say " curl -fsSL https://cerebro.life/install.sh | sh -s -- --no-launch don't open Cerebro when done" } # The user's Desktop folder (localized names included), or nothing if they # don't have one - we never create a Desktop folder that wasn't there. desktop_folder() { d="" if have xdg-user-dir; then d="$(xdg-user-dir DESKTOP 2>/dev/null || true)"; fi # xdg-user-dir answers $HOME when DESKTOP is unset - that's not a desktop. if [ -z "$d" ] || [ "$d" = "$HOME" ] || [ "$d" = "$HOME/" ]; then d="$HOME/Desktop"; fi [ -d "$d" ] && printf '%s' "$d" } # True if the file is the launcher this script wrote (never someone else's). is_our_launcher() { [ -f "$1" ] && [ ! -L "$1" ] && grep -qF "$LAUNCHER_MARK" "$1" } # ~/.local/bin/cerebro: start Cerebro in its own session, stdin/out/err # detached from the terminal, so closing the terminal (SIGHUP) can't kill it # and the shell prompt comes straight back. write_launcher() { mkdir -p "$BIN_DIR" # Pre-launcher installs symlinked this path to the AppImage; replace that. if [ -L "$BIN" ] && [ "$(readlink "$BIN")" = "$APP" ]; then rm -f "$BIN"; fi if [ -e "$BIN" ] && ! is_our_launcher "$BIN"; then warn "$BIN already exists and isn't Cerebro's launcher - left it alone. Start Cerebro from the app menu." return 0 fi cat > "$BIN.tmp" <&2; exit 1; } # Same display hint as the app-menu entry (read by Electron before JS runs). export ELECTRON_OZONE_PLATFORM_HINT="\${ELECTRON_OZONE_PLATFORM_HINT:-wayland}" case "\${1:-}" in --foreground|-f) shift; exec "\$APP" "\$@" ;; esac if [ -n "\${CEREBRO_FOREGROUND:-}" ]; then exec "\$APP" "\$@"; fi mkdir -p "\$LOG_DIR" LOG="\$LOG_DIR/cerebro.log" # Append, so a second launch while Cerebro runs never rotates the live log # out from under it; past 5 MB the old log moves to .1 to cap growth. [ "\$(wc -c 2>/dev/null <"\$LOG" || echo 0)" -ge 5242880 ] && mv -f "\$LOG" "\$LOG.1" 2>/dev/null echo "----- \$(date -u +%Y-%m-%dT%H:%M:%SZ) Cerebro starting (cerebro launcher) -----" >>"\$LOG" export CEREBRO_DETACHED=1 if command -v setsid >/dev/null 2>&1; then setsid -f "\$APP" "\$@" >"\$LOG" 2>&1 else nohup "\$APP" "\$@" >"\$LOG" 2>&1 & fi echo "Cerebro is starting in the background - you can close this terminal." echo "Log: \$LOG (run 'cerebro --foreground' to debug)" EOF chmod +x "$BIN.tmp" mv -f "$BIN.tmp" "$BIN" ok "Added the 'cerebro' terminal command (runs in the background)" } desktop_shortcut() { [ "$WANT_SHORTCUT" = 1 ] || return 0 dt="$(desktop_folder)" if [ -z "$dt" ]; then say "${DIM} (no Desktop folder found - skipped the desktop icon)${R}" return 0 fi sc="$dt/cerebro.desktop" cp -f "$DESKTOP_FILE" "$sc" # KDE/Xfce/Cinnamon/MATE launch an executable .desktop on the desktop; # GNOME (Desktop Icons NG) additionally wants it marked trusted. chmod +x "$sc" have gio && gio set "$sc" metadata::trusted true >/dev/null 2>&1 || true ok "Added a Cerebro shortcut to your desktop" } # Open Cerebro detached via the launcher, only with a graphical session. launch_now() { [ "$WANT_LAUNCH" = 1 ] || return 1 [ -n "${WAYLAND_DISPLAY:-}" ] || [ -n "${DISPLAY:-}" ] || return 1 if is_our_launcher "$BIN"; then "$BIN" >/dev/null 2>&1 || return 1 else mkdir -p "$LOG_DIR" ELECTRON_OZONE_PLATFORM_HINT="${ELECTRON_OZONE_PLATFORM_HINT:-wayland}" CEREBRO_DETACHED=1 \ nohup "$APP" >"$LOG_DIR/cerebro.log" 2>&1 & fi return 0 } check_platform() { os="$(uname -s)" [ "$os" = Linux ] || die "this installer is for Linux. On $os, download Cerebro from https://cerebro.life/dashboard" arch="$(uname -m)" case "$arch" in x86_64|amd64) ;; aarch64|arm64|armv7l|armv6l|armhf) die "No ARM Linux build yet ($arch). Cerebro for Linux is x86_64 (64-bit Intel/AMD) only for now." ;; *) die "unsupported CPU architecture: $arch. Cerebro for Linux is x86_64 (64-bit Intel/AMD) only." ;; esac if [ "$(id -u)" = 0 ]; then die "don't run this with sudo or as root. It installs into your own home folder - run it as your normal user." fi [ -n "${HOME:-}" ] && [ -d "$HOME" ] || die "\$HOME is not set to a directory." } check_tools() { have curl || have wget || die "need curl or wget to download Cerebro. Install one with your package manager (e.g. sudo apt install curl)." for t in sha512sum base64 od; do have "$t" || die "need '$t' to verify the download (part of coreutils). Install coreutils with your package manager." done } desktop_integrate() { mkdir -p "$DESKTOP_DIR" # The icon ships inside the AppImage (usr/share/icons/hicolor//apps/; # the root cerebro-desktop.png is only a symlink into it). The static type2 # runtime extracts single files without FUSE. icons=0 if (cd "$TMP_DIR" && "$APP" --appimage-extract 'usr/share/icons/hicolor/*' >/dev/null 2>&1); then for f in "$TMP_DIR"/squashfs-root/usr/share/icons/hicolor/*/apps/cerebro-desktop.png; do [ -f "$f" ] || continue size_dir="$(basename "$(dirname "$(dirname "$f")")")" mkdir -p "$ICON_ROOT/$size_dir/apps" cp "$f" "$ICON_ROOT/$size_dir/apps/cerebro-desktop.png" icons=$((icons + 1)) done fi # Stale bare-name icon loses to the Papirus theme's unrelated "cerebro" icon. rm -f "$ICON_ROOT/256x256/apps/cerebro.png" [ "$icons" -gt 0 ] || warn "couldn't extract the app icon - Cerebro will add it on first launch." # Byte-for-byte what electron/main.js installDesktopIntegration() writes, so # the app's own first-launch integration rewrites the same file, not a twin. cat > "$DESKTOP_FILE" </dev/null 2>&1 || true have gtk-update-icon-cache && gtk-update-icon-cache -f -t "$ICON_ROOT" >/dev/null 2>&1 || true have xdg-desktop-menu && xdg-desktop-menu forceupdate >/dev/null 2>&1 || true ok "Added Cerebro to your app menu" } install_cerebro() { check_platform check_tools say "" say "${B}Cerebro Desktop${R} ${DIM}- Linux installer${R}" say "" TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/cerebro-install.XXXXXX")" trap cleanup EXIT trap 'exit 130' INT TERM step "Checking the latest release" fetch "$RELEASE_URL/latest-linux.yml" "$TMP_DIR/latest-linux.yml" \ || die "couldn't reach GitHub to read the latest release. Check your connection and try again." version="$(sed -n "s/^version:[[:space:]]*//p" "$TMP_DIR/latest-linux.yml" | tr -d "\"'\r" | head -n 1)" # The digest listed under the AppImage's files[] entry (top-level sha512 as fallback). sha_b64="$(awk -v f="$ASSET" ' /^[[:space:]]*-[[:space:]]*url:/ { u = $NF; gsub(/["\047\r]/, "", u); next } /^[[:space:]]+sha512:/ && u == f { s = $2; gsub(/["\047\r]/, "", s); print s; exit } ' "$TMP_DIR/latest-linux.yml")" if [ -z "$sha_b64" ]; then sha_b64="$(sed -n "s/^sha512:[[:space:]]*//p" "$TMP_DIR/latest-linux.yml" | tr -d "\"'\r" | head -n 1)" fi [ -n "$version" ] && [ -n "$sha_b64" ] || die "latest-linux.yml from the release is missing the version or checksum. Nothing was installed." expected="$(b64_to_hex "$sha_b64")" [ "${#expected}" -eq 128 ] || die "latest-linux.yml has a malformed SHA-512. Nothing was installed." ok "Latest is v$version" mkdir -p "$INSTALL_DIR" if [ -f "$APP" ] && [ "$(sha512_hex "$APP")" = "$expected" ]; then ok "v$version is already installed - repairing shortcuts only" else if [ -f "$APP" ]; then step "Updating the installed copy to v$version"; else step "Downloading Cerebro v$version"; fi # Download next to the target (same filesystem) so the final mv is atomic - # a running Cerebro keeps its open inode and is never left half-written. fetch "$RELEASE_URL/$ASSET" "$APP.download" progress \ || die "download failed. Check your connection and run the command again." step "Verifying checksum" actual="$(sha512_hex "$APP.download")" if [ "$actual" != "$expected" ]; then rm -f "$APP.download" say "" >&2 say "${RED}${B} CHECKSUM MISMATCH - the download was deleted and nothing was installed.${R}" >&2 say " expected sha512 $expected" >&2 say " got sha512 $actual" >&2 say "" >&2 die "the file from GitHub doesn't match the published checksum. If a release was published in the last few minutes, wait and retry; otherwise report it at https://github.com/Professor-Low/Cerebro-Installer/issues" fi ok "SHA-512 matches latest-linux.yml" chmod +x "$APP.download" mv -f "$APP.download" "$APP" ok "Installed to $APP" fi chmod +x "$APP" write_launcher desktop_integrate desktop_shortcut if [ -x /opt/Cerebro/cerebro-desktop ]; then warn "the Cerebro .deb is also installed (/opt/Cerebro). Remove it to avoid two Cerebro entries: sudo apt remove cerebro-desktop" fi say "" if launch_now; then say "${GRN}${B}Cerebro v$version is installed and opening now.${R} It runs on its own - you can close this terminal." else say "${GRN}${B}Cerebro v$version is installed.${R} Open Cerebro from your app menu or the desktop icon." fi case ":${PATH:-}:" in *":$BIN_DIR:"*) say "${DIM}Or run it from a terminal (it detaches, closing the terminal is safe):${R} cerebro" ;; *) say "${DIM}Terminal command:${R} $BIN" warn "$BIN_DIR isn't on your PATH, so plain 'cerebro' won't work in a terminal yet. Add it in your shell's config (bash/zsh: export PATH=\"\$HOME/.local/bin:\$PATH\" / fish: fish_add_path ~/.local/bin)." ;; esac say "${DIM}Updates install themselves from inside the app. To uninstall:${R}" say "${DIM} curl -fsSL https://cerebro.life/install.sh | sh -s -- --uninstall${R}" } uninstall_cerebro() { say "" step "Removing Cerebro Desktop" removed=0 # Only remove the launcher link / menu entry if they point at our AppImage - # never someone else's "cerebro". if { [ -L "$BIN" ] && [ "$(readlink "$BIN")" = "$APP" ]; } || is_our_launcher "$BIN"; then rm -f "$BIN"; ok "Removed ${BIN}"; removed=1 fi dt="$(desktop_folder || true)" if [ -n "$dt" ] && [ -f "$dt/cerebro.desktop" ] && grep -qF "\"$APP\"" "$dt/cerebro.desktop"; then rm -f "$dt/cerebro.desktop"; ok "Removed the desktop shortcut"; removed=1 fi if [ -f "$DESKTOP_FILE" ] && grep -qF "\"$APP\"" "$DESKTOP_FILE"; then for f in "$ICON_ROOT"/*/apps/cerebro-desktop.png; do [ -f "$f" ] || continue rm -f "$f" rmdir "$(dirname "$f")" "$(dirname "$(dirname "$f")")" 2>/dev/null || true done rm -f "$DESKTOP_FILE" ok "Removed the app menu entry and icon"; removed=1 have update-desktop-database && update-desktop-database "$DESKTOP_DIR" >/dev/null 2>&1 || true have gtk-update-icon-cache && gtk-update-icon-cache -f -t "$ICON_ROOT" >/dev/null 2>&1 || true have xdg-desktop-menu && xdg-desktop-menu forceupdate >/dev/null 2>&1 || true fi if [ -f "$APP" ] || [ -f "$APP.download" ]; then rm -f "$APP" "$APP.download"; ok "Removed $APP"; removed=1 fi rmdir "$INSTALL_DIR" 2>/dev/null || true say "" if [ "$removed" = 1 ]; then say "${GRN}${B}Cerebro is uninstalled.${R} Your conversations, settings and memory were left in place." else say "Nothing to remove - Cerebro wasn't installed by this script." fi } main() { action=install for a in "$@"; do case "$a" in --uninstall|uninstall) action=uninstall ;; --no-shortcut) WANT_SHORTCUT=0 ;; --no-launch) WANT_LAUNCH=0 ;; -h|--help|help) usage; return 0 ;; *) usage >&2; exit 2 ;; esac done if [ "$action" = uninstall ]; then uninstall_cerebro; else install_cerebro; fi } # Everything above only defines functions, so a truncated `curl | sh` download # runs nothing at all. main "$@"