#!/bin/bash # # Purpose : Install Federated Connector for macOS, or update it (`update` # runs this with --update). # # curl -fsSL https://federated.sh/mac | bash # # macOS has no git (its git asks to install the Command Line Tools), 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 and network storage jobs, # and the Federated Backup app you give Full Disk Access, point into it. An # update swaps the new files in at once. # # It also 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-macos" API_URL="https://gitea.federated.computer/api/v1/repos/Federated_Computer/connector-macos" BRANCH=main BASE="$HOME/.local/share/federated-connector" CONNECTOR="$BASE/connector" VERSION_FILE="$CONNECTOR/.version" BIN="$HOME/.local/bin" LOG_DIR="$HOME/Library/Logs/Federated Connector" CONF_DIR="$HOME/Library/Application Support/Federated Connector" BLOCK_START="# >>> Federated Connector commands >>>" BLOCK_END="# <<< Federated Connector commands <<<" 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]* ]] } # What can't be fixed from here, checked before anything changes preflight() { local major [ "$(uname -s)" = Darwin ] || fail "This is for macOS. On Linux, use CoreDesktop: https://federated.sh" major="$(sw_vers -productVersion | cut -d. -f1)" [ "$major" -ge 13 ] 2> /dev/null || fail "This needs macOS 13 (Ventura) or later; this Mac has $(sw_vers -productVersion)." [ "$(id -u)" -ne 0 ] || fail "Run this as yourself, not as root. It asks for your password when it needs it." if ! id -Gn | tr ' ' '\n' | grep -qx admin; then echo "${YELLOW}Your account isn't an administrator, so apps that need installing with your" echo "Mac password (Nextcloud, Tailscale, macFUSE) can't be installed. The rest works.$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" | plutil -extract commit.id raw -o - - 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" \ | osascript -l JavaScript -e ' function run(argv) { ObjC.import("Foundation"); var data = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile; var commits = JSON.parse($.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding).js); var out = []; for (var i = 0; i < commits.length && commits[i].sha !== argv[0]; i++) { out.push(commits[i].sha.slice(0, 7) + " " + commits[i].commit.message.split("\n")[0]); } return out.join("\n"); }' "$1" 2> /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" && COPYFILE_DISABLE=1 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 export PATH="\$HOME/.local/bin:\$PATH" $BLOCK_END EOF } # ~/.local/bin on PATH, at the top of ~/.zprofile (zsh, macOS' shell) and # ~/.bash_profile (bash), so what comes later in them, e.g. the agent # sandbox's line that configure agents adds, still comes first on PATH put_on_path() { local file tmp changed=0 for file in "$HOME/.zprofile" "$HOME/.bash_profile"; do grep -qxF "$BLOCK_START" "$file" 2> /dev/null && continue tmp="$(mktemp)" || return 1 { path_block; echo; cat "$file" 2> /dev/null || true; } > "$tmp" && cat "$tmp" > "$file" rm -f "$tmp" changed=1 done [ $changed -eq 0 ] || echo "Added ~/.local/bin to your PATH (in ~/.zprofile and ~/.bash_profile)." } # Jobs and the sandbox set up by running configure from another folder (before # there was an installer) still point there point_elsewhere() { local file found=0 for file in "$HOME/Library/LaunchAgents/computer.federated.connector."*.plist; do [ -f "$file" ] || continue if ! grep -qF "$CONNECTOR/" "$file" && ! grep -qF "Federated Backup.app" "$file"; then found=1 fi done if [ -d "$HOME/Applications/Federated Backup.app" ] \ && [ "$(plutil -extract FederatedBackupScript raw -o - "$HOME/Applications/Federated Backup.app/Contents/Info.plist" 2> /dev/null)" \ != "$CONNECTOR/system/backup" ]; then found=1 fi grep -qs 'Federated Connector >>>' "$HOME/.zshrc" && ! grep -qsF "$CONNECTOR/sandbox/bin" "$HOME/.zshrc" && found=1 if [ $found -eq 1 ]; then echo echo "${YELLOW}Some of what configure set up runs from the folder you used before. To move it" echo "here, run configure again and choose backup, storage and agents (it shows them as" echo "to do). Backups then need Full Disk Access again.$RESET" fi } 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 macOS" echo "Sets up this Mac for your Federated Core: Mail, Calendar, Contacts, files," echo "backups, 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 fetch_and_swap "$latest" || fail "Couldn't install Federated Connector." link_commands || fail "Couldn't put configure and update in $BIN." put_on_path point_elsewhere 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 window, run: configure (and later, to get new versions: update)" if confirm_yes "Run configure now?"; then "$CONNECTOR/configure" < /dev/tty fi } main "$@" exit