diet-guard/install.sh
Krzysztof kuhy Rudnicki 957769ff0e
Some checks are pending
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
Add budget-adherence calendar, streaks, and freely-editable synced budget
Both platforms gain a calendar tab/screen (green/yellow/red/not-logged
day coloring, logging/adherence streaks, year-to-date tally), and
tapping a day drills into that day's logged entries. This requires the
daily budget to become a normal, freely-editable value synced between
devices like the food log — replaces the previous chattr +i seal
(deliberately, per user decision) with plain last-edit-wins sync via a
new budget.json alongside food_log.json. The budget field defaults to
2200 kcal, starts read-only, and unlocks via an Edit/Save toggle on
both the PC gate's History tab and the phone app's Calendar screen.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016dHac1iYQKTMZEtL7xDJvA
2026-07-13 21:31:45 +02:00

95 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# Diet Guard installer: log-to-unlock gate with a freely-editable budget.
#
# Usage: bash install.sh
#
# What it does:
# 1. Ensures system deps (setxkbmap for VT-disable, requests for OFF lookups)
# 2. pip-installs this package + gatelock into system Python's user
# site-packages (the systemd service runs /usr/bin/python directly, not
# a venv, so the package must live where that interpreter can find it —
# see CLAUDE.md's "Production dependency installation" section)
# 3. Installs + enables the systemd user timer that fires the gate every ~30m
# 4. Installs + enables the systemd user timer that syncs the log every ~15m
# (the sync itself stays unconfigured -- and a no-op -- until you create
# a sync token; see the reminder this step prints)
# 5. Sets your initial daily budget from biometrics (only if not already set;
# it stays freely editable afterward from the gate or the phone app)
# ============================================================================
set -euo pipefail
# Split declare/assign so the command-substitution exit code is not masked (SC2155).
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
readonly SCRIPT_DIR
readonly REPO_DIR="$SCRIPT_DIR"
readonly SERVICE_SRC="$SCRIPT_DIR/diet-guard-gate.service"
readonly TIMER_SRC="$SCRIPT_DIR/diet-guard-gate.timer"
readonly SYNC_SERVICE_SRC="$SCRIPT_DIR/diet-guard-sync.service"
readonly SYNC_TIMER_SRC="$SCRIPT_DIR/diet-guard-sync.timer"
readonly SYSTEMD_USER_DIR="$HOME/.config/systemd/user"
readonly DATA_DIR="$HOME/.local/share/diet_guard"
readonly BUDGET_FILE="$DATA_DIR/.budget"
readonly SYNC_TOKEN_FILE="$HOME/.config/diet_guard/sync_token"
echo "=== Diet Guard Installer ==="
# 1. System dependencies ------------------------------------------------------
echo "[1/5] Checking system dependencies..."
if ! command -v setxkbmap &>/dev/null; then
echo " Installing xorg-setxkbmap (gate disables VT switching while locked)..."
sudo pacman -S --noconfirm xorg-setxkbmap
else
echo " setxkbmap present"
fi
# 2. Install this package + its dependencies into system Python -------------
echo "[2/5] Installing diet_guard + dependencies for /usr/bin/python..."
/usr/bin/python3 -m pip install --user --break-system-packages -e "$REPO_DIR"
echo " Installed. Verifying import..."
/usr/bin/python3 -c "import diet_guard; import gatelock" \
&& echo " diet_guard and gatelock import cleanly from the system interpreter."
# 3. systemd user timer + service (gate) -------------------------------------
echo "[3/5] Installing the gate's systemd user timer + service..."
mkdir -p "$SYSTEMD_USER_DIR"
cp "$SERVICE_SRC" "$SYSTEMD_USER_DIR/diet-guard-gate.service"
cp "$TIMER_SRC" "$SYSTEMD_USER_DIR/diet-guard-gate.timer"
systemctl --user daemon-reload
systemctl --user enable --now diet-guard-gate.timer
echo " Timer enabled and started (fires the gate every ~30 min)."
# 4. systemd user timer + service (sync) -------------------------------------
echo "[4/5] Installing the sync's systemd user timer + service..."
cp "$SYNC_SERVICE_SRC" "$SYSTEMD_USER_DIR/diet-guard-sync.service"
cp "$SYNC_TIMER_SRC" "$SYSTEMD_USER_DIR/diet-guard-sync.timer"
systemctl --user daemon-reload
systemctl --user enable --now diet-guard-sync.timer
echo " Timer enabled and started (syncs the log every ~15 min)."
if [[ -e "$SYNC_TOKEN_FILE" ]]; then
echo " Sync token already present at $SYNC_TOKEN_FILE."
else
echo " No sync token yet at $SYNC_TOKEN_FILE -- sync will no-op (and log a"
echo " failure) on every tick until you create a fine-grained GitHub PAT"
echo " scoped to the diet-guard-sync repo's contents and save it there,"
echo " mode 600: chmod 600 \"$SYNC_TOKEN_FILE\""
fi
# 5. Set the initial daily budget ---------------------------------------------
echo "[5/5] Setting your initial daily budget..."
if [[ -e "$BUDGET_FILE" ]]; then
echo " Budget already set at $BUDGET_FILE - skipping init."
echo " Edit it any time from the gate's calendar tab or the phone app."
else
echo " Enter your biometrics (used once then discarded to compute a"
echo " starting value; you can change the number itself any time from"
echo " the gate's calendar tab or the phone app):"
python -m diet_guard init
fi
echo "=== Installation complete ==="
echo "The gate checks every ~30 min (08:00-22:00) and locks until you log a meal"
echo "once you have gone 5h without logging."
echo "Test the lock now (safe, closeable): python -m diet_guard gate --demo"