droboticflow/tools/drosync/packaging/linux/install-systemd-portable.sh
2026-08-20 19:47:43 +08:00

72 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "run this installer as root (for example: sudo ./install-systemd-portable.sh)" >&2
exit 1
fi
SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_ROOT="/opt/drosync"
ENABLE=0
usage() {
echo "usage: $0 [--prefix /opt/drosync] [--enable]" >&2
}
while [[ $# -gt 0 ]]; do
case "$1" in
--prefix)
[[ $# -ge 2 ]] || { usage; exit 2; }
INSTALL_ROOT="$2"
shift 2
;;
--enable)
ENABLE=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
usage
exit 2
;;
esac
done
case "$INSTALL_ROOT" in
/*) ;;
*) echo "--prefix must be an absolute path" >&2; exit 2 ;;
esac
for binary in drosync drosync-daemon; do
[[ -x "$SOURCE_DIR/$binary" ]] || {
echo "portable bundle is missing executable $binary" >&2
exit 1
}
done
if ! getent group drosync >/dev/null 2>&1; then
groupadd --system drosync
fi
if ! getent passwd drosync >/dev/null 2>&1; then
useradd --system --gid drosync --home-dir /var/lib/drosync --no-create-home --shell /usr/sbin/nologin drosync
fi
install -d -o drosync -g drosync -m 0700 /var/lib/drosync
install -d -m 0755 /etc/drosync "$INSTALL_ROOT"
install -m 0755 "$SOURCE_DIR/drosync" "$INSTALL_ROOT/drosync"
install -m 0755 "$SOURCE_DIR/drosync-daemon" "$INSTALL_ROOT/drosync-daemon"
if [[ -f "$SOURCE_DIR/drosync-gui" ]]; then
install -m 0755 "$SOURCE_DIR/drosync-gui" "$INSTALL_ROOT/drosync-gui"
fi
sed "s|@DROSYNC_INSTALL_ROOT@|$INSTALL_ROOT|g" "$SOURCE_DIR/drosync-portable.service" \
>/etc/systemd/system/drosync.service
chmod 0644 /etc/systemd/system/drosync.service
systemctl daemon-reload
if [[ "$ENABLE" == 1 ]]; then
systemctl enable --now drosync.service
fi
echo "installed drosync portable systemd bundle to $INSTALL_ROOT"