#!/bin/bash # # Purpose : Install Federated Connector for Pop!_OS, or update it (`update` # runs this with --update). # # curl -fsSL https://gitea.federated.computer/Federated_Computer/connector-popos/raw/branch/main/install.sh | bash # # Pop!_OS doesn't come with git, so this downloads an archive of the latest # commit from Gitea, puts it in ~/.local/share/federated-connector/connector, # and records the commit. The place never changes, which matters: the # backup, email and network storage services point into it. An update swaps # the new files in at once. # # It also installs the few packages configure itself needs, puts `configure` # and `update` in ~/.local/bin, and that on your PATH, then offers to run # configure. # # Usage: install.sh [--update] # # FEDERATED_CONNECTOR_SOURCE= installs from that folder instead of # Gitea (the smoketest uses it to install the working copy). # Piped into bash, the script is read as it runs; main at the end runs it # only once bash has read it all. Questions are read from the terminal, as # stdin is the script. set -o pipefail REPO_URL="https://gitea.federated.computer/Federated_Computer/connector-popos" API_URL="https://gitea.federated.computer/api/v1/repos/Federated_Computer/connector-popos" BRANCH=main BASE="$HOME/.local/share/federated-connector" CONNECTOR="$BASE/connector" VERSION_FILE="$CONNECTOR/.version" BIN="$HOME/.local/bin" LOG_DIR="$HOME/.local/state/federated-connector" UNITS="$HOME/.config/systemd/user" BASHRC="$HOME/.bashrc" BLOCK_START="# >>> Federated Connector commands >>>" BLOCK_END="# <<< Federated Connector commands <<<" # What configure needs before any item: JSON, the keyring, Python's GObject # bindings (Online Accounts, the email watcher) and notifications PACKAGES=(curl jq libsecret-tools python3-gi gir1.2-secret-1 libnotify-bin) RED=$'\033[31m' GREEN=$'\033[32m' YELLOW=$'\033[33m' BOLD=$'\033[1m' RESET=$'\033[0m' say() { echo "$BOLD$*$RESET" } fail() { echo "$RED$*$RESET" exit 1 } # Questions come from the terminal; without one (e.g. in a script), the # answer is no has_tty() { { : < /dev/tty; } 2> /dev/null } confirm_yes() { local answer has_tty || return 1 read -rp "$1 [Y/n] " answer < /dev/tty [[ "$answer" != [nN]* ]] } os_field() { (. /etc/os-release 2> /dev/null && eval "echo \"\${$1}\"") } # What can't be fixed from here, checked before anything changes preflight() { [ "$(uname -s)" = Linux ] || fail "This is for Pop!_OS. On a Mac, use Federated Connector for macOS." [ "$(id -u)" -ne 0 ] || fail "Run this as yourself, not as root. It asks for your password when it needs it." command -v apt-get > /dev/null || fail "This is for Pop!_OS (or another Ubuntu-based system); this one has no apt." if [ "$(os_field ID)" != pop ]; then echo "${YELLOW}This is made for Pop!_OS with COSMIC; this computer runs $(os_field PRETTY_NAME)." echo "Most of it works on other Ubuntu-based systems too.$RESET" fi if ! id -Gn | tr ' ' '\n' | grep -qxE 'sudo|admin'; then echo "${YELLOW}Your account isn't an administrator, so configure can't install what it needs" echo "or change system settings (mounts, printers, Tailscale).$RESET" fi if [ -z "$FEDERATED_CONNECTOR_SOURCE" ] && ! curl -fsS --max-time 15 -o /dev/null "$API_URL"; then fail "Couldn't reach Gitea ($REPO_URL). Connect to the internet and run this again." fi } installed_version() { cat "$VERSION_FILE" 2> /dev/null } # The commit main is at now latest_version() { if [ -n "$FEDERATED_CONNECTOR_SOURCE" ]; then echo local else curl -fsS --max-time 30 "$API_URL/branches/$BRANCH" \ | python3 -I -c 'import json, sys; print(json.load(sys.stdin)["commit"]["id"])' 2> /dev/null fi } # The commits between the installed version and the new one: "short subject" changes_since() { [ -n "$1" ] && [ "$1" != local ] && [ "$2" != local ] || return 0 curl -fsS --max-time 30 "$API_URL/commits?sha=$2&limit=50&stat=false&verification=false&files=false" \ | python3 -I -c ' import json, sys for commit in json.load(sys.stdin): if commit["sha"] == sys.argv[1]: break print(commit["sha"][:7], commit["commit"]["message"].split("\n")[0])' "$1" 2> /dev/null } # The packages configure needs, if any are missing install_packages() { local pkg missing=() for pkg in "${PACKAGES[@]}"; do [ "$(dpkg-query -W -f '${db:Status-Status}' "$pkg" 2> /dev/null)" = installed ] || missing+=("$pkg") done [ ${#missing[@]} -eq 0 ] && return 0 echo "configure needs: ${missing[*]}" if ! has_tty && ! sudo -n true 2> /dev/null; then echo "${YELLOW}Install them with: sudo apt install ${missing[*]}$RESET" return 1 fi # sudo asks for your password on the terminal itself; stdin is this script sudo apt-get install -y "${missing[@]}" < /dev/null \ || { sudo apt-get update < /dev/null && sudo apt-get install -y "${missing[@]}" < /dev/null; } } # Put version $1 in a new folder next to CONNECTOR, then swap it in. A folder # next to it is on the same disk, so the swap is two renames. fetch_and_swap() { local version="$1" staging="$BASE/.connector-new-$$" old="$BASE/.connector-old-$$" mkdir -p "$BASE" || return 1 rm -rf "$staging" mkdir "$staging" || return 1 if [ -n "$FEDERATED_CONNECTOR_SOURCE" ]; then (cd "$FEDERATED_CONNECTOR_SOURCE" && tar --exclude ./.git --exclude .claude \ --exclude ./smoketest/results -cf - .) | tar -xf - -C "$staging" || { rm -rf "$staging"; return 1; } else echo "Downloading ${version:0:7} from $REPO_URL" curl -fsSL --max-time 300 "$REPO_URL/archive/$version.tar.gz" \ | tar -xzf - -C "$staging" --strip-components 1 || { rm -rf "$staging"; return 1; } fi [ -x "$staging/configure" ] || { echo "The download doesn't look right (no configure in it)."; rm -rf "$staging"; return 1; } echo "$version" > "$staging/.version" if [ -d "$CONNECTOR" ]; then mv "$CONNECTOR" "$old" || { rm -rf "$staging"; return 1; } fi if ! mv "$staging" "$CONNECTOR"; then [ -d "$old" ] && mv "$old" "$CONNECTOR" rm -rf "$staging" return 1 fi rm -rf "$old" } # configure and update in ~/.local/bin, linked so they follow updates link_commands() { local name mkdir -p "$BIN" || return 1 for name in configure update; do ln -sfn "$CONNECTOR/$name" "$BIN/$name" || return 1 done } path_block() { cat << EOF $BLOCK_START # configure and update: see $REPO_URL case ":\$PATH:" in *":\$HOME/.local/bin:"*) ;; *) export PATH="\$HOME/.local/bin:\$PATH" ;; esac $BLOCK_END EOF } # Ubuntu's ~/.profile puts ~/.local/bin on PATH at login, but only if it was # there already, so on a new computer the commands wouldn't be found until # you log in again. The block goes at the top of ~/.bashrc, so what comes # later in it, e.g. the agent sandbox's line that configure agents adds, # still comes first on PATH. put_on_path() { local tmp grep -qxF "$BLOCK_START" "$BASHRC" 2> /dev/null && return 0 tmp="$(mktemp)" || return 1 { path_block; echo; cat "$BASHRC" 2> /dev/null || true; } > "$tmp" && cat "$tmp" > "$BASHRC" rm -f "$tmp" echo "Added ~/.local/bin to your PATH (in ~/.bashrc)." } # The services configure set up run from copies of system/systemd's units; # bring those up to date, and restart the email watcher on the new version refresh_units() { local unit changed=0 for unit in "$CONNECTOR"/system/systemd/*; do [ -f "$UNITS/$(basename "$unit")" ] || continue cmp -s "$unit" "$UNITS/$(basename "$unit")" && continue cp "$unit" "$UNITS/" && changed=1 done [ $changed -eq 1 ] && systemctl --user daemon-reload 2> /dev/null if systemctl --user is-active -q federated-connector-email-watch.service 2> /dev/null; then systemctl --user restart federated-connector-email-watch.service 2> /dev/null fi return 0 } main() { local update=0 installed latest log [ "$1" = --update ] && update=1 mkdir -p "$LOG_DIR" && log="$LOG_DIR/$([ $update -eq 1 ] && echo update || echo install)-$(date +%Y-%m-%d-%H%M%S).log" [ -n "$log" ] && exec > >(tee -a "$log") 2>&1 if [ $update -eq 0 ]; then echo say "Federated Connector for Pop!_OS" echo "Sets up this computer for your Federated Core: email, calendars, contacts," echo "files, backups, printers, shared folders and AI agents." echo fi preflight installed="$(installed_version)" latest="$(latest_version)" || fail "Couldn't find the latest version on Gitea." [ -n "$latest" ] || fail "Couldn't find the latest version on Gitea." if [ $update -eq 1 ] && [ "$installed" = "$latest" ] && [ "$latest" != local ]; then echo "Federated Connector is up to date (${latest:0:7})." return 0 fi if [ $update -eq 1 ] && [ -n "$installed" ]; then say "Updating Federated Connector from ${installed:0:7} to ${latest:0:7}" changes_since "$installed" "$latest" | sed 's/^/ /' fi install_packages || echo "${YELLOW}Some of configure won't work until they're installed.$RESET" fetch_and_swap "$latest" || fail "Couldn't install Federated Connector." link_commands || fail "Couldn't put configure and update in $BIN." put_on_path refresh_units echo if [ $update -eq 1 ]; then echo "${GREEN}Federated Connector is up to date (${latest:0:7}).$RESET" return 0 fi echo "${GREEN}Federated Connector is installed in $CONNECTOR.$RESET" echo "In a new terminal, run: configure (and later, to get new versions: update)" if confirm_yes "Run configure now?"; then "$CONNECTOR/configure" < /dev/tty fi } main "$@" exit