#!/usr/bin/env bash # Farhand host for Linux: installs (or updates) the host for this user and starts it at login. # # curl -fsSL https://farhandapp.com/linux/install.sh | bash # curl -fsSL https://farhandapp.com/linux/install.sh | bash -s -- --uninstall # # Options: --no-autostart install without starting at login # --uninstall remove the host (keeps the pairing key in ~/.farhand, so phones stay paired) # --purge with --uninstall: also leave your Farhand account and delete ~/.farhand # --yes install missing system packages without asking # # Installs to ~/.local/share/farhand. Nothing goes outside your home directory, except system # packages it may ask to install with sudo. Generated by host/scripts/release-linux.sh. set -euo pipefail VERSION="1.0.0" TARBALL_URL="${FARHAND_TARBALL_URL:-https://downloads.farhandapp.com/linux/Farhand-1.0.0-linux.tar.gz}" TARBALL_SHA256="4acde96add379c607bf1cd14fe8ce01a975b1002c3f2836498bc7996b3e50a81" PREFIX="${FARHAND_PREFIX:-${XDG_DATA_HOME:-$HOME/.local/share}/farhand}" HOST_DIR="$PREFIX/host" STATE="$HOME/.farhand" LOG="$STATE/host.log" APP_ID="com.danbryant.farhand.host" bold() { printf '\033[1m%s\033[0m\n' "$*"; } say() { printf '%s\n' "$*"; } die() { printf 'Error: %s\n' "$*" >&2; exit 1; } # curl | bash feeds this script on stdin, so questions are asked on the terminal. have_tty() { (: < /dev/tty) 2>/dev/null; } # -r isn't enough: opening fails without a terminal ask() { # ask -> 0 for yes. Defaults to yes; no terminal means no. [ "${ASSUME_YES:-}" = 1 ] && return 0 have_tty || return 1 local reply printf '%s [Y/n] ' "$1" > /dev/tty read -r reply < /dev/tty || return 1 case "$reply" in [nN]*) return 1 ;; *) return 0 ;; esac } is_wayland() { [ -n "${WAYLAND_DISPLAY:-}" ] || [ "${XDG_SESSION_TYPE:-}" = wayland ]; } # ---- system requirements ------------------------------------------------------ # The system Python: Wayland screen sharing needs the distribution's PyGObject and GStreamer. PY="${FARHAND_PYTHON:-/usr/bin/python3}" export PYTHON="$PY" # run-host.sh builds the virtualenv with it need_python() { [ -x "$PY" ] && "$PY" -c 'import sys; sys.exit(sys.version_info < (3, 10))' 2>/dev/null } need_venv() { # the real test: Debian and Ubuntu ship Python without a working venv module local tmp ok=1 tmp=$(mktemp -d) "$PY" -m venv "$tmp/v" >/dev/null 2>&1 && "$tmp/v/bin/python" -m pip --version >/dev/null 2>&1 && ok=0 rm -rf "$tmp" return $ok } need_gstreamer() { # Wayland screen sharing: PyGObject + GStreamer with its PipeWire source "$PY" - <<'PY' >/dev/null 2>&1 import gi gi.require_version("Gst", "1.0") from gi.repository import Gst Gst.init(None) assert all(Gst.ElementFactory.find(e) for e in ("pipewiresrc", "videoconvert", "appsink")) PY } need_clipboard() { if is_wayland; then command -v wl-copy >/dev/null && command -v wl-paste >/dev/null else command -v xclip >/dev/null || command -v xsel >/dev/null; fi } missing_packages() { # prints the packages to install for what's missing, for this distribution local py=0 venv=0 gst=0 clip=0 need_python || py=1 [ $py = 1 ] || need_venv || venv=1 if is_wayland; then [ $py = 1 ] || need_gstreamer || gst=1; fi need_clipboard || clip=1 local clip_pkg=xclip is_wayland && clip_pkg=wl-clipboard local p=() if command -v apt-get >/dev/null; then [ $py = 1 ] && p+=(python3) [ $venv = 1 ] && p+=(python3-venv) [ $gst = 1 ] && p+=(python3-gi gir1.2-gstreamer-1.0 gstreamer1.0-pipewire gstreamer1.0-plugins-base) elif command -v dnf >/dev/null; then [ $py = 1 ] && p+=(python3) [ $gst = 1 ] && p+=(python3-gobject gstreamer1-plugins-base pipewire-gstreamer) elif command -v pacman >/dev/null; then [ $py = 1 ] || [ $venv = 1 ] && p+=(python) [ $gst = 1 ] && p+=(python-gobject gst-plugins-base gst-plugin-pipewire) elif command -v zypper >/dev/null; then [ $py = 1 ] || [ $venv = 1 ] && p+=(python3) [ $gst = 1 ] && p+=(python3-gobject typelib-1_0-Gst-1_0 gstreamer-plugins-base gstreamer-plugin-pipewire) else [ $py = 1 ] && p+=("python3 (3.10 or later)") [ $venv = 1 ] && p+=("python3's venv module") [ $gst = 1 ] && p+=("PyGObject" "GStreamer with its PipeWire plugin") fi [ $clip = 1 ] && p+=("$clip_pkg") echo "${p[*]:-}" } install_command() { # install_command if command -v apt-get >/dev/null; then echo "sudo apt-get install -y $*" elif command -v dnf >/dev/null; then echo "sudo dnf install -y $*" elif command -v pacman >/dev/null; then echo "sudo pacman -S --needed --noconfirm $*" elif command -v zypper >/dev/null; then echo "sudo zypper install -y $*" fi } check_requirements() { local pkgs cmd pkgs=$(missing_packages) [ -z "$pkgs" ] && return 0 cmd=$(install_command $pkgs) if [ -z "$cmd" ]; then die "please install these with your package manager, then run this again: $pkgs" fi bold "Farhand needs a few system packages: $pkgs" say " $cmd" if ask "Install them now (asks for your password)?"; then if have_tty; then $cmd < /dev/tty || die "installing the packages failed" else $cmd || die "installing the packages failed"; fi pkgs=$(missing_packages) if [ -n "$pkgs" ]; then # A clipboard tool is nice to have; anything else is required. case "$pkgs" in wl-clipboard|xclip) say "Clipboard sync stays off until $pkgs is installed." ;; *) die "still missing: $pkgs" ;; esac fi else case "$pkgs" in wl-clipboard|xclip) say "Skipping: clipboard sync stays off until $pkgs is installed." ;; *) die "install them, then run this again." ;; esac fi } # ---- install -------------------------------------------------------------------- download() { # download if command -v curl >/dev/null; then curl -fsSL --retry 3 -o "$2" "$1" elif command -v wget >/dev/null; then wget -q -O "$2" "$1" else die "needs curl or wget"; fi } sha256() { if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1 else "$PY" -c 'import hashlib, sys; print(hashlib.sha256(open(sys.argv[1], "rb").read()).hexdigest())' "$1"; fi } service_active() { systemctl --user is-active --quiet farhand-host.service 2>/dev/null; } install_files() { local tmp="$WORK" say "Downloading Farhand $VERSION…" download "$TARBALL_URL" "$tmp/farhand.tar.gz" || die "couldn't download $TARBALL_URL" [ "$(sha256 "$tmp/farhand.tar.gz")" = "$TARBALL_SHA256" ] || die "the download is damaged (checksum mismatch); try again" mkdir -p "$tmp/x" tar -xzf "$tmp/farhand.tar.gz" -C "$tmp/x" local src="$tmp/x/farhand-host-$VERSION" [ -f "$src/host/run-host.sh" ] || die "unexpected archive layout" mkdir -p "$HOST_DIR" # Replace the code but keep the virtualenv (.venv), whose paths point at this directory. rm -rf "$HOST_DIR/remotedesk_host" "$PREFIX/branding" cp -R "$src/." "$PREFIX/" rm -f "$HOST_DIR/.venv/farhand-installed" # reinstall dependencies for this version } show_pairing() { # show_pairing : prints the QR code the host just logged local start=$1 i is_wayland && [ ! -s "$STATE/portal-restore-token" ] && \ bold "A dialog asks to share your screen: choose every screen, allow it to be remembered, then share." say "Waiting for the host to start…" for i in $(seq 1 360); do if [ -f "$LOG" ] && tail -c +"$((start + 1))" "$LOG" | grep -q 'farhand://pair'; then tail -c +"$((start + 1))" "$LOG" | grep -vE '^[0-9]{2}:[0-9]{2}:[0-9]{2} (INFO|DEBUG) ' return 0 fi sleep 0.5 done say "The host is taking a while to start. Its log is $LOG" } do_install() { [ "$(uname -s)" = Linux ] || die "this installer is for Linux. Get Farhand for other computers at https://farhandapp.com/download/" [ "$(id -u)" != 0 ] || die "run this as the user whose desktop you want to share, not as root" local desktop=1 if [ -z "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then desktop=0 say "Note: there's no desktop session here (SSH?). Farhand shares the desktop you log in to, and starts there at your next login." fi WORK=$(mktemp -d) trap 'rm -rf "$WORK"' EXIT bold "Installing Farhand $VERSION for Linux" check_requirements local paired=0 update=0 [ -f "$STATE/config.json" ] && paired=1 # already has a pairing key: phones may know it [ -x "$HOST_DIR/run-host.sh" ] && update=1 local was_running=0 service_active && was_running=1 install_files say "Setting up (the first time this downloads about 40 MB of Python packages)…" "$HOST_DIR/run-host.sh" --version >/dev/null || die "setting up the host failed (see the messages above)" mkdir -p "$STATE" local offset=0 [ -f "$LOG" ] && offset=$(wc -c < "$LOG") if [ "$AUTOSTART" = 1 ]; then "$HOST_DIR/install-autostart-linux.sh" >/dev/null if [ $desktop = 0 ]; then bold "Farhand is installed and starts when you log in to your desktop." say " Then pair it: open $STATE/pairing-qr.png, or run $HOST_DIR/run-host.sh --show-qr once." return 0 fi if ! systemctl --user is-enabled --quiet farhand-host.service 2>/dev/null; then # XDG autostart (no systemd session): start it now too, detached from this terminal. setsid "$STATE/autostart.sh" >/dev/null 2>&1 < /dev/null & fi if [ $update = 1 ]; then bold "Farhand is updated to $VERSION$([ $was_running = 1 ] && echo ' and restarted')." say "To pair another phone, open $STATE/pairing-qr.png or look in $LOG." else if [ $paired = 0 ]; then show_pairing "$offset"; say "" else say "Phones paired with this computer before still work. To pair another, open $STATE/pairing-qr.png."; fi bold "Farhand is installed and starts when you log in." fi say " Log: $LOG" say " Stop/start: systemctl --user stop farhand-host / systemctl --user start farhand-host" else [ $was_running = 1 ] && systemctl --user restart farhand-host.service bold "Farhand $VERSION is installed. Start it with:" say " $HOST_DIR/run-host.sh" fi say " Update: run this installer again" say " Uninstall: curl -fsSL https://farhandapp.com/linux/install.sh | bash -s -- --uninstall" } do_uninstall() { if [ "$PURGE" = 1 ] && [ -x "$HOST_DIR/.venv/bin/python" ]; then (cd "$HOST_DIR" && .venv/bin/python -m remotedesk_host --leave-account) || true fi if [ -x "$HOST_DIR/install-autostart-linux.sh" ]; then "$HOST_DIR/install-autostart-linux.sh" --remove >/dev/null fi pkill -u "$(id -u)" -f -- "$HOST_DIR/.venv/bin/python -m remotedesk_host" 2>/dev/null || true rm -rf "$PREFIX" local data="${XDG_DATA_HOME:-$HOME/.local/share}" rm -f "$data/applications/$APP_ID.desktop" "$data/icons/hicolor/256x256/apps/$APP_ID.png" if [ "$PURGE" = 1 ]; then rm -rf "$STATE" bold "Farhand is removed, along with its pairing key and settings." else bold "Farhand is removed. Its pairing key stays in $STATE, so reinstalling keeps your phones paired (--purge deletes it)." fi } usage() { cat <<'USAGE' Installs (or updates) the Farhand host for this user and starts it at login. curl -fsSL https://farhandapp.com/linux/install.sh | bash -s -- [options] --no-autostart install without starting at login --uninstall remove the host (keeps the pairing key in ~/.farhand, so phones stay paired) --purge with --uninstall: also leave your Farhand account and delete ~/.farhand --yes install missing system packages without asking USAGE } main() { AUTOSTART=1 UNINSTALL=0 PURGE=0 ASSUME_YES="${ASSUME_YES:-}" for arg in "$@"; do case "$arg" in --no-autostart) AUTOSTART=0 ;; --uninstall) UNINSTALL=1 ;; --purge) PURGE=1 ;; --yes|-y) ASSUME_YES=1 ;; -h|--help) usage; exit 0 ;; *) die "unknown option $arg (try --help)" ;; esac done case "$VERSION" in @*) die "this is the installer template; build it with host/scripts/release-linux.sh" ;; esac if [ "$UNINSTALL" = 1 ]; then do_uninstall; else do_install; fi } # Everything runs from here, so a download cut short can't run half a script. main "$@"