BASH_PATH := $(shell command -v bash 2>/dev/null)
ifneq ($(BASH_PATH),)
SHELL := $(BASH_PATH)
.SHELLFLAGS := -euo pipefail -c
else
SHELL := /bin/sh
.SHELLFLAGS := -euc
endif
INTERVAL ?= 600
ifeq ($(HOME),)
$(error "CRITICAL ERROR: $$HOME environment variable is not set. Aborting to prevent unsafe filesystem operations.")
endif
CARGO := $(shell command -v cargo 2>/dev/null)
PYTHON3 := $(shell command -v python3 2>/dev/null)
SYSTEMCTL := $(shell command -v systemctl 2>/dev/null)
.PHONY: all build install setup-systemd uninstall check-requirements
all: build
check-requirements:
ifeq ($(CARGO),)
$(error "CRITICAL: 'cargo' binary not found in PATH. Please install Rust (rustup).")
endif
ifeq ($(PYTHON3),)
$(error "CRITICAL: 'python3' binary not found in PATH. Please install Python 3.")
endif
@if [ ! -f "setup_systemd.py" ]; then \
echo "CRITICAL: 'setup_systemd.py' is missing in the current directory." >&2; \
exit 1; \
fi
build: check-requirements
"$(CARGO)" build --release
install: check-requirements
"$(CARGO)" install --path=.
"$(PYTHON3)" setup_systemd.py --seconds "$(INTERVAL)"
setup-systemd: check-requirements
"$(PYTHON3)" setup_systemd.py --seconds "$(INTERVAL)"
uninstall:
@echo "=> Starting uninstallation process..."
@echo "=> Checking systemd configuration..."
@if [ -n "$(SYSTEMCTL)" ]; then \
"$(SYSTEMCTL)" --user disable --now wallswitch.timer 2>/dev/null || true; \
else \
echo " (systemctl not found; skipping systemd timer deactivation)"; \
fi
@echo "=> Removing generated unit files safely..."
-rm -f "$(HOME)/.config/systemd/user/wallswitch.service"
-rm -f "$(HOME)/.config/systemd/user/wallswitch.timer"
@if [ -n "$(SYSTEMCTL)" ]; then \
"$(SYSTEMCTL)" --user daemon-reload 2>/dev/null || true; \
fi
@echo "=> Uninstalling cargo binary..."
@if [ -n "$(CARGO)" ]; then \
"$(CARGO)" uninstall wallswitch 2>/dev/null || true; \
else \
echo " (cargo not found; cannot cleanly uninstall binary from ~/.cargo/bin)"; \
fi
@echo "=> Uninstallation finished successfully."