#!/usr/bin/env bash # Install or update we_monitor as a systemd user service. # # Fresh install: # chmod +x install.sh # ./install.sh # # then edit ~/.config/we_monitor/credentials.json # systemctl --user start we_monitor # # Update (code already pulled via git): # ./install.sh # (restarts the service automatically if it was running) set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SERVICE_NAME="we_monitor" CREDS_DIR="$HOME/.config/we_monitor" CREDS_FILE="$CREDS_DIR/credentials.json" SYSTEMD_DIR="$HOME/.config/systemd/user" SERVICE_FILE="$SYSTEMD_DIR/$SERVICE_NAME.service" LOG_DIR="$SCRIPT_DIR/logs" VENV="$SCRIPT_DIR/.venv" PYTHON="$VENV/bin/python" # detect fresh install vs update IS_UPDATE=false WAS_ACTIVE=false if systemctl --user is-enabled "$SERVICE_NAME" &>/dev/null; then IS_UPDATE=true if systemctl --user is-active "$SERVICE_NAME" &>/dev/null; then WAS_ACTIVE=true fi fi if $IS_UPDATE; then echo "==> we_monitor updater" else echo "==> we_monitor installer" fi echo " Project : $SCRIPT_DIR" echo " User : $USER" echo "" # ── 1. virtual environment ──────────────────────────────────────────────────── if [[ ! -x "$PYTHON" ]]; then echo "==> Creating virtual environment : $VENV" python3 -m venv "$VENV" else echo "==> Virtual environment exists : $VENV" fi echo "==> Installing / updating dependencies" "$VENV/bin/pip" install --quiet --upgrade pip "$VENV/bin/pip" install --quiet --upgrade -r "$SCRIPT_DIR/requirements.txt" echo " Python : $PYTHON" echo "" # ── 2. credentials template (only on fresh install) ─────────────────────────── mkdir -p "$CREDS_DIR" chmod 700 "$CREDS_DIR" if [[ ! -f "$CREDS_FILE" ]]; then cat > "$CREDS_FILE" <<'EOF' { "credentials": { "username": "me@example.com", "password": "secret" }, "vin": "", "domains": ["charging", "electric_drive", "connectivity"], "interval_s": 300, "host": "0.0.0.0", "port": 9999, "log_dir": "./logs", "diff": false } EOF chmod 600 "$CREDS_FILE" echo "==> Created credentials template : $CREDS_FILE" echo " *** Edit this file before starting the service! ***" else echo "==> Credentials file exists : $CREDS_FILE" fi # ── 3. log directory ────────────────────────────────────────────────────────── mkdir -p "$LOG_DIR" echo "==> Log directory : $LOG_DIR" # ── 4. systemd user service ─────────────────────────────────────────────────── mkdir -p "$SYSTEMD_DIR" cat > "$SERVICE_FILE" < Installed service unit : $SERVICE_FILE" # ── 5. enable linger (service survives logout / starts at boot) ─────────────── if loginctl enable-linger "$USER" 2>/dev/null; then echo "==> Linger enabled for $USER (service starts at boot)" else echo " Note: could not enable linger — service runs only while logged in" echo " Run as root to fix: loginctl enable-linger $USER" fi # ── 6. reload systemd and enable / restart ──────────────────────────────────── systemctl --user daemon-reload systemctl --user enable "$SERVICE_NAME" if $WAS_ACTIVE; then systemctl --user restart "$SERVICE_NAME" echo "==> Service restarted" fi echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" if $IS_UPDATE; then echo " Update complete." echo "" if $WAS_ACTIVE; then echo " Service was restarted automatically." else echo " Service was not running — start it manually if needed:" echo " systemctl --user start $SERVICE_NAME" fi else echo " Installation complete." echo "" echo " Next steps:" echo " 1. Edit credentials (username, password, vin, interval_s, …):" echo " \$EDITOR $CREDS_FILE" echo "" echo " 2. Start the service:" echo " systemctl --user start $SERVICE_NAME" fi echo "" echo " Check status : systemctl --user status $SERVICE_NAME" echo " Follow logs : journalctl --user -u $SERVICE_NAME -f" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"