#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=./codex-acp-service-common.sh source "$SCRIPT_DIR/codex-acp-service-common.sh" SERVICE_NAME="${XWORKMATE_CODEX_ACP_SERVICE_NAME:-xworkmate-codex-acp.service}" XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}" SYSTEMD_USER_DIR="${XWORKMATE_CODEX_ACP_SYSTEMD_USER_DIR:-$XDG_CONFIG_HOME/systemd/user}" CONFIG_DIR="${XWORKMATE_CODEX_ACP_CONFIG_DIR:-$XDG_CONFIG_HOME/xworkmate/codex-acp}" RUNTIME_DIR="${XWORKMATE_CODEX_ACP_RUNTIME_DIR:-$XDG_STATE_HOME/xworkmate/codex-acp}" LOG_DIR="" CONFIG_FILE="$CONFIG_DIR/config.env" LAUNCHER_FILE="$RUNTIME_DIR/run-codex-acp.sh" SERVICE_FILE="$SYSTEMD_USER_DIR/$SERVICE_NAME" linux_has_systemctl() { command -v systemctl >/dev/null 2>&1 } linux_require_systemctl() { if [[ "$XWORKMATE_CODEX_ACP_DRY_RUN" == "1" ]]; then return 0 fi linux_has_systemctl || common_die "systemctl is required for Linux native service control." } linux_usage() { cat < [--port PORT] [--lines N] Commands: install Write user unit + enable/start Codex ACP service start Start service using configured or auto-selected port stop Stop the user service restart Restart service, re-picking a free port if needed status Show service state and endpoint endpoint Print configured websocket endpoint logs Show recent journal lines set-port PORT Reconfigure the service port, then restart if active uninstall Stop service and remove systemd user unit + generated files help Show this message Defaults: host $XWORKMATE_CODEX_ACP_DEFAULT_HOST default port $XWORKMATE_CODEX_ACP_DEFAULT_PORT Environment: CODEX_BIN=/absolute/path/to/codex XWORKMATE_CODEX_ACP_DRY_RUN=1 Generate files without calling systemctl EOF } linux_is_active() { if ! linux_has_systemctl; then return 1 fi systemctl --user is-active --quiet "$SERVICE_NAME" } linux_state() { if ! linux_has_systemctl; then printf 'unavailable (systemctl missing)\n' return 0 fi if systemctl --user is-enabled "$SERVICE_NAME" >/dev/null 2>&1; then if linux_is_active; then printf 'active\n' return 0 fi printf 'enabled\n' return 0 fi if linux_is_active; then printf 'active\n' return 0 fi printf 'inactive\n' } linux_write_service_file() { common_ensure_dirs mkdir -p "$SYSTEMD_USER_DIR" cat > "$SERVICE_FILE" <