#!/bin/bash # # Install rpi-temp-humid-monitor as a system app with a Python venv. # set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PYTHON_CODE="${REPO_ROOT}/python_code" SERVICE_TEMPLATE="${REPO_ROOT}/systemd/thmonitor.service.in" INSTALL_ROOT="${INSTALL_ROOT:-/usr/local/lib/thmonitor}" CONFIG_FILE="${CONFIG_FILE:-/etc/thMonitor.conf}" LOG_FILE="${LOG_FILE:-/var/log/th-python.log}" BIN_SINGLE="${BIN_SINGLE:-/usr/local/bin/thmonitor-single}" BIN_LOOP="${BIN_LOOP:-/usr/local/bin/thmonitor-loop}" SYSTEMD_UNIT="${SYSTEMD_UNIT:-/etc/systemd/system/thmonitor.service}" CRON_FILE="${CRON_FILE:-/etc/cron.d/thmonitor}" LEGACY_DIR="/usr/local/bin/thMonitor" RUN_MODE="systemd" DO_START=1 DO_INSTALL=0 DO_UNINSTALL=0 APT_PACKAGES=( python3 python3-venv python3-dev python3-pip default-libmysqlclient-dev build-essential ) PYTHON_MODULES=( dhtreader.py readMysql.py temp-humid-read-loop.py temp-humid-read-single.py thmonitor_common.py updateMysql.py ) usage() { local cmd cmd="$(basename "$0")" cat < %s\n' "$*" } die() { printf 'ERROR: %s\n' "$*" >&2 exit 1 } need_root() { if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then die "Run this script as root (e.g. sudo $0)" fi } parse_args() { if [[ $# -eq 0 ]]; then usage exit 0 fi while [[ $# -gt 0 ]]; do case "$1" in --install) DO_INSTALL=1 shift ;; --cron) RUN_MODE="cron" shift ;; --no-start) DO_START=0 shift ;; --uninstall) DO_UNINSTALL=1 shift ;; -h|--help) usage exit 0 ;; *) die "Unknown option: $1 (try --help)" ;; esac done if [[ "$DO_INSTALL" -eq 1 && "$DO_UNINSTALL" -eq 1 ]]; then die "Choose either --install or --uninstall" fi if [[ "$DO_UNINSTALL" -eq 1 ]]; then if [[ "$RUN_MODE" == "cron" || "$DO_START" -eq 0 ]]; then die "--cron and --no-start apply only with --install" fi return fi if [[ "$DO_INSTALL" -ne 1 ]]; then die "Specify --install or --uninstall (try --help)" fi } install_apt_deps() { local missing=() local pkg for pkg in "${APT_PACKAGES[@]}"; do if ! dpkg -s "$pkg" >/dev/null 2>&1; then missing+=("$pkg") fi done if ((${#missing[@]} == 0)); then log "System packages already installed" return fi log "Installing system packages: ${missing[*]}" apt-get update DEBIAN_FRONTEND=noninteractive apt-get install -y "${missing[@]}" } remove_legacy_install() { if [[ -d "$LEGACY_DIR" ]]; then log "Removing legacy install directory ${LEGACY_DIR}" rm -rf "$LEGACY_DIR" fi if [[ -f /etc/init.d/thMonitor ]]; then if command -v invoke-rc.d >/dev/null 2>&1; then invoke-rc.d thMonitor stop >/dev/null 2>&1 || true fi if command -v update-rc.d >/dev/null 2>&1; then update-rc.d thMonitor remove >/dev/null 2>&1 || true fi rm -f /etc/init.d/thMonitor fi find "$INSTALL_ROOT" "$LEGACY_DIR" -name 'dhtreader.so' -delete 2>/dev/null || true } install_app_files() { local module log "Installing application to ${INSTALL_ROOT}" install -d -m 755 "$INSTALL_ROOT" for module in "${PYTHON_MODULES[@]}"; do install -m 644 "${PYTHON_CODE}/${module}" "${INSTALL_ROOT}/${module}" done install -m 644 "${PYTHON_CODE}/requirements.txt" "${INSTALL_ROOT}/requirements.txt" rm -f "${INSTALL_ROOT}/dhtreader.so" } create_venv() { local venv_python="${INSTALL_ROOT}/venv/bin/python" local venv_pip="${INSTALL_ROOT}/venv/bin/pip" log "Creating Python virtual environment" if [[ ! -x "$venv_python" ]]; then python3 -m venv "${INSTALL_ROOT}/venv" fi log "Installing Python dependencies into venv" "$venv_pip" install --upgrade pip "$venv_pip" install -r "${PYTHON_CODE}/requirements.txt" } install_config() { install -d -m 755 "$(dirname "$LOG_FILE")" touch "$LOG_FILE" chmod 644 "$LOG_FILE" if [[ -f "$CONFIG_FILE" ]]; then log "Keeping existing config ${CONFIG_FILE}" return fi log "Installing sample config ${CONFIG_FILE}" install -m 640 "${PYTHON_CODE}/temphumid.conf" "$CONFIG_FILE" } install_bin_wrappers() { local venv_python="${INSTALL_ROOT}/venv/bin/python" log "Installing command wrappers" cat >"$BIN_SINGLE" <"$BIN_LOOP" <"$SYSTEMD_UNIT" chmod 644 "$SYSTEMD_UNIT" if command -v systemctl >/dev/null 2>&1; then systemctl daemon-reload else log "systemctl not found; systemd unit installed but not loaded" fi } install_cron_job() { log "Installing cron job ${CRON_FILE}" cat >"$CRON_FILE" </dev/null 2>&1 EOF chmod 644 "$CRON_FILE" } disable_systemd_service() { if command -v systemctl >/dev/null 2>&1 && [[ -f "$SYSTEMD_UNIT" ]]; then systemctl disable --now thmonitor.service >/dev/null 2>&1 || true fi } enable_systemd_service() { if [[ "$DO_START" -ne 1 ]]; then log "Skipping service start (--no-start)" return fi if ! command -v systemctl >/dev/null 2>&1; then log "systemctl not available; start the loop manually with ${BIN_LOOP}" return fi log "Enabling and starting thmonitor.service" systemctl enable thmonitor.service systemctl restart thmonitor.service systemctl --no-pager --full status thmonitor.service || true } remove_cron_job() { rm -f "$CRON_FILE" } do_install() { need_root [[ -d "$PYTHON_CODE" ]] || die "Missing python_code directory: ${PYTHON_CODE}" install_apt_deps remove_legacy_install install_app_files create_venv install_config install_bin_wrappers case "$RUN_MODE" in systemd) remove_cron_job install_systemd_service enable_systemd_service ;; cron) disable_systemd_service install_cron_job ;; *) die "Unknown run mode: ${RUN_MODE}" ;; esac cat </dev/null 2>&1; then systemctl daemon-reload fi cat <