#!/usr/bin/env bash set -euo pipefail if [[ "${EUID}" -ne 0 ]]; then echo "Please run as root: sudo ./bin/install-slave.sh" exit 1 fi SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PACKAGE_ROOT="$(dirname "$SCRIPT_DIR")" INSTALL_DIR="${1:-/opt/redis-clip-sync}" SERVICE_NAME="${SERVICE_NAME:-redis-clip-sync}" RUN_USER="${RUN_USER:-${SUDO_USER:-root}}" RUN_GROUP="${RUN_GROUP:-$RUN_USER}" TEMPLATE_PATH="$PACKAGE_ROOT/systemd/redis-clip-sync.service.tpl" TARGET_SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" if [[ ! -f "$TEMPLATE_PATH" ]]; then echo "Template not found: $TEMPLATE_PATH" exit 1 fi echo "Installing to: $INSTALL_DIR" mkdir -p "$INSTALL_DIR" if [[ -f "$INSTALL_DIR/conf/config.properties" ]]; then cp "$INSTALL_DIR/conf/config.properties" "$INSTALL_DIR/conf/config.properties.bak.$(date +%Y%m%d%H%M%S)" echo "Existing config backed up in $INSTALL_DIR/conf" fi cp -a "$PACKAGE_ROOT/bin" "$INSTALL_DIR/" cp -a "$PACKAGE_ROOT/lib" "$INSTALL_DIR/" cp -a "$PACKAGE_ROOT/conf" "$INSTALL_DIR/" cp -a "$PACKAGE_ROOT/systemd" "$INSTALL_DIR/" mkdir -p "$INSTALL_DIR/logs" "$INSTALL_DIR/run" chown -R "$RUN_USER:$RUN_GROUP" "$INSTALL_DIR" chmod +x "$INSTALL_DIR/bin/start.sh" "$INSTALL_DIR/bin/stop.sh" "$INSTALL_DIR/bin/status.sh" "$INSTALL_DIR/bin/logs.sh" SERVICE_CONTENT="$(<"$TEMPLATE_PATH")" SERVICE_CONTENT="${SERVICE_CONTENT//__INSTALL_DIR__/$INSTALL_DIR}" SERVICE_CONTENT="${SERVICE_CONTENT//__RUN_USER__/$RUN_USER}" SERVICE_CONTENT="${SERVICE_CONTENT//__RUN_GROUP__/$RUN_GROUP}" printf '%s\n' "$SERVICE_CONTENT" > "$TARGET_SERVICE_FILE" systemctl daemon-reload systemctl enable --now "$SERVICE_NAME" echo echo "Install completed" echo "Service name: $SERVICE_NAME" echo "Install dir: $INSTALL_DIR" echo echo "Useful commands:" echo " systemctl status $SERVICE_NAME" echo " journalctl -u $SERVICE_NAME -f" echo " systemctl restart $SERVICE_NAME"