#!/usr/bin/env bash # install-service.sh # Generates and installs a systemd user-level service for the Battery Tracker app. # No root required — uses ~/.config/systemd/user/. # # Usage: bash sbin/install-service.sh set -euo pipefail # Resolve the app root directory from the script's own location APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" VENV_WAITRESS="$APP_DIR/.venv/bin/waitress-serve" SERVICE_DIR="$HOME/.config/systemd/user" SERVICE_FILE="$SERVICE_DIR/battery-tracker.service" echo "=== Battery Tracker — systemd user service installer ===" echo "App directory: $APP_DIR" echo # Sanity checks if [[ ! -f "$VENV_WAITRESS" ]]; then echo "ERROR: waitress-serve not found at $VENV_WAITRESS" echo "Run: python3 -m venv .venv && .venv/bin/pip install -r requirements.txt" exit 1 fi if [[ ! -f "$APP_DIR/app.py" ]]; then echo "ERROR: app.py not found in $APP_DIR" exit 1 fi # Prompt for port and host read -rp "Listen host [default: 127.0.0.1]: " HOST HOST="${HOST:-127.0.0.1}" read -rp "Listen port [default: 5000]: " PORT PORT="${PORT:-5000}" echo echo "Home Assistant integration (optional — press Enter to skip):" read -rp " HOMEASSISTANT_URL (e.g. http://homeassistant.local:8123): " HA_URL read -rp " HOMEASSISTANT_API_KEY (long-lived access token): " HA_KEY read -rp " HOMEASSISTANT_POLL_INTERVAL seconds [default: 300]: " HA_INTERVAL HA_INTERVAL="${HA_INTERVAL:-300}" echo echo "Generating service file → $SERVICE_FILE" mkdir -p "$SERVICE_DIR" cat > "$SERVICE_FILE" <