#!/bin/sh set -eu operator=${SUDO_USER:-} mode=simulation while [ "$#" -gt 0 ]; do case "$1" in --user) [ "$#" -ge 2 ] || exit 2; operator=$2; shift 2;; --enable-shutdown) mode=shutdown; shift;; *) echo 'Usage: sudo sh install.sh [--user ACCOUNT] [--enable-shutdown]' >&2; exit 2;; esac done [ "$(id -u)" = 0 ] || { echo 'Run the installer with sudo' >&2; exit 1; } case "$operator" in ''|*[!A-Za-z0-9_.-]*) echo 'Supply the desktop account using --user' >&2; exit 1;; esac uid=$(id -u "$operator") [ "$uid" != 0 ] || { echo 'Use a non-root desktop account' >&2; exit 1; } gid=$(id -g "$operator") base=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) platform=$(uname -s) version=$(cat "$base/architecture") case "$(uname -m):$version" in x86_64:x64|arm64:arm64|aarch64:arm64) ;; *) echo 'This package targets a different CPU' >&2; exit 1;; esac case "$platform" in Linux) command -v systemctl >/dev/null home=$(getent passwd "$operator" | cut -d: -f6) root=/opt/lanups; data=/etc/LanUps; group=root test -f "$base/lanups-agent.service" ;; Darwin) home=$(dscl . -read "/Users/$operator" NFSHomeDirectory | sed 's/^NFSHomeDirectory: //') root=/usr/local/libexec/lanups; data='/Library/Application Support/LanUps'; group=wheel test -f "$base/local.lanups.agent.plist" if [ -e /Applications/LanUps.app ] && [ ! -L /Applications/LanUps.app ]; then echo '/Applications/LanUps.app already exists and is not managed by this installer' >&2; exit 1 fi ;; *) echo 'This installer supports Linux with systemd and macOS' >&2; exit 1;; esac case "$home" in /*) ;; *) echo 'Cannot determine the desktop account home' >&2; exit 1;; esac install -d -m 0755 -o root -g "$group" "$root" "$data" stage=$(mktemp -d "$root/release.XXXXXX") chmod 0755 "$stage" cp -R "$base/agent" "$stage/agent" chmod 0755 "$stage/agent/LanUps.Agent" if [ "$platform" = Linux ]; then cp -R "$base/desktop" "$stage/desktop" chmod 0755 "$stage/desktop/LanUps.Desktop" install -m 0644 "$base/ups.png" "$stage/ups.png" else cp -R "$base/LanUps.app" "$stage/LanUps.app" chmod 0755 "$stage/LanUps.app/Contents/MacOS/LanUps.Desktop" fi chown -R root:"$group" "$stage" chmod -R go-w "$stage" token="$data/agent.token" if [ ! -f "$token" ]; then "$stage/agent/LanUps.Agent" --provision; fi chmod 0600 "$token" if [ "$platform" = Linux ]; then getent group lanups-operators >/dev/null || groupadd --system lanups-operators usermod -a -G lanups-operators "$operator" chown root:lanups-operators "$token" chmod 0640 "$token" args='' [ "$mode" = simulation ] || args=' --enable-shutdown' sed "s|^ExecStart=.*|ExecStart=$stage/agent/LanUps.Agent$args|" "$base/lanups-agent.service" >"$stage/lanups-agent.service" install -m 0644 "$stage/lanups-agent.service" /etc/systemd/system/lanups-agent.service install -d -m 0755 /usr/local/share/applications sed "s|^Exec=.*|Exec=$stage/desktop/LanUps.Desktop|; /^Icon=/d; \$a Icon=$stage/ups.png" "$base/lanups-desktop.desktop" >"$stage/menu.desktop" install -m 0644 "$stage/menu.desktop" /usr/local/share/applications/lanups-desktop.desktop sed "s|^Exec=.*|Exec=$stage/desktop/LanUps.Desktop --tray|" "$base/lanups-desktop.desktop" >"$stage/autostart.desktop" sudo -u "$operator" mkdir -p "$home/.config/autostart" sudo -u "$operator" install -m 0644 "$stage/autostart.desktop" "$home/.config/autostart/lanups-desktop.desktop" systemctl daemon-reload systemctl enable lanups-agent.service systemctl restart lanups-agent.service else chmod -N "$token" chmod +a "user:$operator allow read" "$token" if launchctl print system/local.lanups.agent >/dev/null 2>&1; then launchctl bootout system/local.lanups.agent; fi suffix='' [ "$mode" = simulation ] || suffix='--enable-shutdown' sed "s|/usr/local/libexec/lanups/agent/LanUps.Agent|$stage/agent/LanUps.Agent$suffix|" \ "$base/local.lanups.agent.plist" >"$stage/agent.plist" install -m 0644 -o root -g wheel "$stage/agent.plist" /Library/LaunchDaemons/local.lanups.agent.plist if launchctl print "gui/$uid/local.lanups.desktop" >/dev/null 2>&1; then launchctl bootout "gui/$uid/local.lanups.desktop"; fi app=/Applications/LanUps.app ln -sfn "$stage/LanUps.app" "$app" sudo -u "$operator" mkdir -p "$home/Library/LaunchAgents" sudo -u "$operator" install -m 0644 "$base/local.lanups.desktop.plist" "$home/Library/LaunchAgents/local.lanups.desktop.plist" launchctl bootstrap system /Library/LaunchDaemons/local.lanups.agent.plist if launchctl print "gui/$uid" >/dev/null 2>&1; then launchctl bootstrap "gui/$uid" "$home/Library/LaunchAgents/local.lanups.desktop.plist" fi fi echo "LAN UPS installed ($mode). The desktop client automatically starts at login."