2026-09-27 20:24:15 +08:00
#!/bin/bash
set -euo pipefail
cd " $( dirname " $0 " ) "
NO_START = 0
IMPORT = ''
2026-09-27 22:28:05 +08:00
INTEGRATION = ''
2026-09-27 20:24:15 +08:00
while [ " $# " -gt 0 ] ; do
case " $1 " in
2026-09-27 22:28:05 +08:00
--enable-integration) INTEGRATION = true; shift ;;
--disable-integration) INTEGRATION = false; shift ;;
2026-09-27 20:24:15 +08:00
--no-start) NO_START = 1; shift ;;
--import-monitor-config) [ " $# " -ge 2 ] || exit 2; IMPORT = " $2 " ; shift 2 ;;
2026-09-27 22:28:05 +08:00
-h| --help) echo 'Usage: sudo ./install.sh [--no-start] [--enable-integration|--disable-integration] [--import-monitor-config /etc/pigway-pi-control.conf]' ; exit 0 ;;
2026-09-27 20:24:15 +08:00
*) echo "ERROR: unknown option: $1 " >& 2; exit 2 ;;
esac
done
2026-09-27 20:54:05 +08:00
for f in app/local_state.py app/migrate.py app/service.py app/runtime.py app/settings.py app/drivers/yahboom.py app/oled_font_5x7.bin config/pigway-cooling-hat.conf systemd/pigway-cooling-hat.service; do
2026-09-27 20:24:15 +08:00
[ -f " $f " ] || { echo "ERROR: required file missing: $f " >& 2; exit 1; }
done
[ " $( id -u) " -eq 0 ] || { echo 'ERROR: run as root' >& 2; exit 1; }
[ -e /dev/i2c-1 ] || { echo 'ERROR: /dev/i2c-1 missing; configure I2C before installing this driver' >& 2; exit 1; }
# Never stop unrelated I2C users. A previous plugin instance is allowed on upgrade.
owner = " $( systemctl show -p MainPID --value pigway-cooling-hat.service 2>/dev/null || true ) "
for pid in $( fuser /dev/i2c-1 2>/dev/null || true ) ; do
[ " $pid " = " $owner " ] || { echo "ERROR: I2C already owned by PID $pid " >& 2; exit 1; }
done
python3 -c 'import smbus2, PIL' 2>/dev/null || { apt-get update; apt-get install -y python3 python3-smbus2 python3-pil; }
# Validate a possible migration before changing installed files.
if [ -n " $IMPORT " ] ; then PYTHONDONTWRITEBYTECODE = 1 python3 app/migrate.py " $IMPORT " --check; fi
2026-09-27 22:28:05 +08:00
if [ -z " $INTEGRATION " ] ; then
INTEGRATION = false
if [ -t 0 ] ; then
read -r -p 'Allow local monitor integration? [y/N] ' answer
case " $answer " in y| Y| yes| YES) INTEGRATION = true ;; esac
fi
fi
2026-09-27 20:24:15 +08:00
getent group pigway-hardware >/dev/null || groupadd --system pigway-hardware
mkdir -p /usr/local/lib/pigway-cooling-hat/drivers
install -m 0644 app/*.py app/oled_font_5x7.bin /usr/local/lib/pigway-cooling-hat/
install -m 0644 app/drivers/*.py /usr/local/lib/pigway-cooling-hat/drivers/
if [ ! -f /etc/pigway-cooling-hat.conf ] ; then
install -m 0640 -g pigway-hardware config/pigway-cooling-hat.conf /etc/pigway-cooling-hat.conf
fi
if [ -n " $IMPORT " ] ; then
cp /etc/pigway-cooling-hat.conf "/etc/pigway-cooling-hat.conf.bak. $( date +%Y%m%d-%H%M%S) "
PYTHONDONTWRITEBYTECODE = 1 python3 app/migrate.py " $IMPORT " --output /etc/pigway-cooling-hat.conf
fi
2026-09-27 22:28:05 +08:00
INTEGRATION = " $INTEGRATION " PYTHONDONTWRITEBYTECODE = 1 python3 - <<'PYCFG'
import os,sys
from pathlib import Path
sys.path.insert(0,'app')
from settings import load,save
p=Path('/etc/pigway-cooling-hat.conf');c=load(p);c['integration']['enabled']=os.environ['INTEGRATION'];save(p,c)
PYCFG
2026-09-27 20:24:15 +08:00
install -m 0644 systemd/pigway-cooling-hat.service /etc/systemd/system/
systemctl daemon-reload
if [ " $NO_START " -eq 0 ] ; then
systemctl enable pigway-cooling-hat.service >/dev/null
systemctl restart pigway-cooling-hat.service
systemctl is-active --quiet pigway-cooling-hat.service
fi
echo 'Hardware plugin installed independently. No monitor service was changed.'
echo 'Start if needed: sudo systemctl start pigway-cooling-hat.service'
echo 'Discover and connect from the monitor Web hardware plugins page.'