#!/usr/bin/env bash # OpenFileSync installer # Usage: curl -fsSL https://get.openfilesync.runatyr.dev/ | bash set -euo pipefail REPO="https://github.com/runatyr1/openfilesync" INSTALL_DIR="/usr/local/share/openfilesync" BIN_LINK="/usr/local/bin/ofs" RCLONE_VERSION="1.73.1" echo "OpenFileSync Installer" echo "======================" echo "" # Check for root (needed for /usr/local install) if [[ $EUID -ne 0 ]]; then SUDO="sudo" echo "Sudo required for installation to ${INSTALL_DIR}" sudo -v || { echo "Sudo authentication failed."; exit 1; } else SUDO="" fi # --- Dependencies --- install_rclone() { if command -v rclone &>/dev/null; then local current current="$(rclone version 2>/dev/null | head -1 | grep -oP 'v\K[0-9.]+')" if [[ "$current" == "$RCLONE_VERSION" ]]; then echo "rclone: already installed (v${current})" return fi echo "rclone: upgrading v${current} -> v${RCLONE_VERSION}..." else echo "Installing rclone v${RCLONE_VERSION}..." fi local arch arch="$(uname -m)" case "$arch" in x86_64) arch="amd64" ;; aarch64) arch="arm64" ;; armv7l) arch="arm-v7" ;; esac local tmpdir tmpdir="$(mktemp -d)" local url="https://downloads.rclone.org/v${RCLONE_VERSION}/rclone-v${RCLONE_VERSION}-linux-${arch}.zip" curl -fsSL "$url" -o "${tmpdir}/rclone.zip" unzip -q "${tmpdir}/rclone.zip" -d "$tmpdir" $SUDO cp "${tmpdir}/rclone-v${RCLONE_VERSION}-linux-${arch}/rclone" /usr/local/bin/rclone $SUDO chmod +x /usr/local/bin/rclone rm -rf "$tmpdir" echo "rclone: installed v${RCLONE_VERSION}" } install_inotify_tools() { if command -v inotifywait &>/dev/null; then echo "inotify-tools: already installed" return fi echo "Installing inotify-tools..." if command -v apt-get &>/dev/null; then $SUDO apt-get update -qq && $SUDO apt-get install -y -qq inotify-tools elif command -v dnf &>/dev/null; then $SUDO dnf install -y -q inotify-tools elif command -v pacman &>/dev/null; then $SUDO pacman -S --noconfirm inotify-tools else echo "Warning: could not install inotify-tools automatically." echo "Install it manually for watch mode support." return fi echo "inotify-tools: installed" } echo "-- Checking dependencies --" install_rclone install_inotify_tools echo "" # --- Install openfilesync --- echo "-- Installing OpenFileSync --" if [[ -d "$INSTALL_DIR" ]]; then echo "Updating existing installation..." $SUDO rm -rf "$INSTALL_DIR" fi # Clone or download if command -v git &>/dev/null; then $SUDO git clone --depth 1 "$REPO" "$INSTALL_DIR" 2>/dev/null || { echo "Git clone failed, trying tarball download..." tmpdir="$(mktemp -d)" curl -fsSL "${REPO}/archive/refs/heads/main.tar.gz" -o "${tmpdir}/openfilesync.tar.gz" $SUDO mkdir -p "$INSTALL_DIR" $SUDO tar xzf "${tmpdir}/openfilesync.tar.gz" -C "$INSTALL_DIR" --strip-components=1 rm -rf "$tmpdir" } else tmpdir="$(mktemp -d)" curl -fsSL "${REPO}/archive/refs/heads/main.tar.gz" -o "${tmpdir}/openfilesync.tar.gz" $SUDO mkdir -p "$INSTALL_DIR" $SUDO tar xzf "${tmpdir}/openfilesync.tar.gz" -C "$INSTALL_DIR" --strip-components=1 rm -rf "$tmpdir" fi $SUDO chmod +x "${INSTALL_DIR}/bin/ofs" $SUDO ln -sf "${INSTALL_DIR}/bin/ofs" "$BIN_LINK" echo "Installed to ${INSTALL_DIR}" echo "Binary linked at ${BIN_LINK}" echo "" # --- Post-install: rebuild filters, resync, restart service (updates only) --- CONFIG_DIR="${HOME}/.config/openfilesync" if [[ -f "${CONFIG_DIR}/openfilesync.conf" ]]; then echo "-- Existing config detected, applying updates --" # Stop service during update echo "Stopping service..." systemctl --user stop ofs-sync.timer 2>/dev/null || true # Wait for any running sync to finish while systemctl --user is-active ofs-sync.service &>/dev/null; do echo "Waiting for running sync to finish..." sleep 2 done # Clear stale lock files (ours + rclone's) rm -f "${HOME}/.local/share/openfilesync/lock" find "${HOME}/.cache/rclone/bisync/" -name '*.lck' -delete 2>/dev/null || true echo "Rebuilding filters..." source "${INSTALL_DIR}/lib/core.sh" source "${INSTALL_DIR}/lib/filters.sh" ensure_dirs build_filters echo "Reinstalling service..." "${BIN_LINK}" install-service 2>&1 || true # Trigger a sync in the background via systemd (not foreground) echo "Triggering sync in background..." systemctl --user start ofs-sync.service --no-block 2>/dev/null || true echo "" echo "Update complete! Monitor sync with: ofs log" else echo "Installation complete!" echo "" echo "Next step: run 'ofs init' to set up your sync." fi echo ""