From 4a4aaee884fe000a7b0a231d95f1047cd7b74500 Mon Sep 17 00:00:00 2001 From: Oleksandr Berezovskyi Date: Sun, 24 May 2026 18:53:11 +0300 Subject: [PATCH] chore: add Makefile and Debian packaging --- Makefile | 77 +++++++++++++++++++++ packaging/DEBIAN/conffiles | 1 + packaging/DEBIAN/control | 14 ++++ packaging/DEBIAN/postinst | 33 +++++++++ packaging/DEBIAN/postrm | 19 +++++ packaging/DEBIAN/prerm | 13 ++++ packaging/systemd/ezcoo-usb-control.service | 26 +++++++ 7 files changed, 183 insertions(+) create mode 100644 Makefile create mode 100644 packaging/DEBIAN/conffiles create mode 100644 packaging/DEBIAN/control create mode 100755 packaging/DEBIAN/postinst create mode 100755 packaging/DEBIAN/postrm create mode 100755 packaging/DEBIAN/prerm create mode 100644 packaging/systemd/ezcoo-usb-control.service diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40db3b9 --- /dev/null +++ b/Makefile @@ -0,0 +1,77 @@ +BINARY := ezcoo-usb-control +VERSION ?= 0.1.0 +CMD := ./cmd/$(BINARY) + +BUILD_DIR := build +DIST_DIR := dist + +DEB_TARGETS := arm64 armhf amd64 + +.PHONY: build deb-all $(addprefix deb-,$(DEB_TARGETS)) clean deps prepare lint + +deps: + go mod tidy + +prepare: deps + go mod download + go generate ./... + +lint: prepare + gofmt -w -s . + goimports -w . + go vet ./... + golangci-lint run --color always + +build: prepare + mkdir -p $(BUILD_DIR) + go build -o $(BUILD_DIR)/$(BINARY) $(CMD) + +# ── Per-architecture .deb builder ───────────────────────────────────────────── +# $(1) = Debian arch (arm64 / armhf / amd64) +# $(2) = GOARCH (arm64 / arm / amd64) +# $(3) = GOARM (7 for armhf, empty otherwise) +define make_deb + $(eval BIN := $(BUILD_DIR)/$(BINARY)-$(1)) + $(eval PKG_DIR := $(BUILD_DIR)/pkg-$(1)) + $(eval DEB_OUT := $(DIST_DIR)/$(BINARY)_$(VERSION)_$(1).deb) + $(eval GO_ENV := GOOS=linux GOARCH=$(2)$(if $(3), GOARM=$(3),)) + + mkdir -p $(BUILD_DIR) $(DIST_DIR) + $(GO_ENV) go build -o $(BIN) $(CMD) + + rm -rf $(PKG_DIR) + + install -D -m 0755 $(BIN) $(PKG_DIR)/usr/bin/$(BINARY) + install -D -m 0640 cmd/ezcoo-usb-control/config.example.yaml $(PKG_DIR)/etc/$(BINARY)/config.yaml + install -D -m 0644 packaging/systemd/$(BINARY).service $(PKG_DIR)/lib/systemd/system/$(BINARY).service + + install -d $(PKG_DIR)/DEBIAN + sed -e 's/VERSION_PLACEHOLDER/$(VERSION)/' \ + -e 's/ARCH_PLACEHOLDER/$(1)/' \ + packaging/DEBIAN/control > $(PKG_DIR)/DEBIAN/control + install -m 0644 packaging/DEBIAN/conffiles $(PKG_DIR)/DEBIAN/conffiles + install -m 0755 packaging/DEBIAN/postinst $(PKG_DIR)/DEBIAN/postinst + install -m 0755 packaging/DEBIAN/prerm $(PKG_DIR)/DEBIAN/prerm + install -m 0755 packaging/DEBIAN/postrm $(PKG_DIR)/DEBIAN/postrm + + dpkg-deb --build --root-owner-group $(PKG_DIR) $(DEB_OUT) + rm -rf $(PKG_DIR) + @echo "→ $(DEB_OUT)" +endef + +deb-arm64: + $(call make_deb,arm64,arm64,) + +deb-armhf: + $(call make_deb,armhf,arm,7) + +deb-amd64: + $(call make_deb,amd64,amd64,) + +deb-all: $(addprefix deb-,$(DEB_TARGETS)) + @echo "" + @echo "Built packages:" + @ls -lh $(DIST_DIR)/$(BINARY)_$(VERSION)_*.deb + +clean: + rm -rf $(BUILD_DIR) $(DIST_DIR) diff --git a/packaging/DEBIAN/conffiles b/packaging/DEBIAN/conffiles new file mode 100644 index 0000000..bb4f390 --- /dev/null +++ b/packaging/DEBIAN/conffiles @@ -0,0 +1 @@ +/etc/ezcoo-usb-control/config.yaml diff --git a/packaging/DEBIAN/control b/packaging/DEBIAN/control new file mode 100644 index 0000000..f4c057d --- /dev/null +++ b/packaging/DEBIAN/control @@ -0,0 +1,14 @@ +Package: ezcoo-usb-control +Version: VERSION_PLACEHOLDER +Architecture: ARCH_PLACEHOLDER +Maintainer: Oleksandr Berezovskyi +Depends: adduser +Recommends: mosquitto-clients +Description: MQTT bridge for EZCOO HDMI matrix switches + Controls an EZCOO EZ-MX42HAS-ARC (4-in 2-out) HDMI matrix via USB-UART + and exposes it to Home Assistant as two select entities through MQTT + auto-discovery. + . + Communicates with the matrix using the EZCOO ASCII serial protocol and + periodically polls routing state to detect external changes (front panel, + IR remote). diff --git a/packaging/DEBIAN/postinst b/packaging/DEBIAN/postinst new file mode 100755 index 0000000..5ed5268 --- /dev/null +++ b/packaging/DEBIAN/postinst @@ -0,0 +1,33 @@ +#!/bin/sh +set -e + +case "$1" in + configure) + # Create system user if it doesn't exist. + if ! id -u ezcoo > /dev/null 2>&1; then + adduser --system --no-create-home --shell /usr/sbin/nologin \ + --group --gecos "EZCOO HDMI matrix bridge" ezcoo + fi + + # Ensure the user is in the dialout group for serial access. + if getent group dialout > /dev/null 2>&1; then + adduser ezcoo dialout + fi + + # Lock down the config directory so only ezcoo can read credentials. + chown -R root:ezcoo /etc/ezcoo-usb-control + chmod 750 /etc/ezcoo-usb-control + chmod 640 /etc/ezcoo-usb-control/config.yaml + ;; +esac + +if [ -d /run/systemd/system ]; then + systemctl daemon-reload + systemctl enable ezcoo-usb-control.service + # Only start automatically on fresh installs, not upgrades. + if [ "$1" = "configure" ] && [ -z "$2" ]; then + systemctl start ezcoo-usb-control.service || true + fi +fi + +#DEBHELPER# diff --git a/packaging/DEBIAN/postrm b/packaging/DEBIAN/postrm new file mode 100755 index 0000000..fcb01bb --- /dev/null +++ b/packaging/DEBIAN/postrm @@ -0,0 +1,19 @@ +#!/bin/sh +set -e + +case "$1" in + purge) + rm -rf /etc/ezcoo-usb-control + if id -u ezcoo > /dev/null 2>&1; then + deluser --quiet ezcoo || true + fi + if getent group ezcoo > /dev/null 2>&1; then + delgroup --quiet ezcoo || true + fi + if [ -d /run/systemd/system ]; then + systemctl daemon-reload + fi + ;; +esac + +#DEBHELPER# diff --git a/packaging/DEBIAN/prerm b/packaging/DEBIAN/prerm new file mode 100755 index 0000000..73f75d8 --- /dev/null +++ b/packaging/DEBIAN/prerm @@ -0,0 +1,13 @@ +#!/bin/sh +set -e + +case "$1" in + remove|upgrade|deconfigure) + if [ -d /run/systemd/system ]; then + systemctl stop ezcoo-usb-control.service || true + systemctl disable ezcoo-usb-control.service || true + fi + ;; +esac + +#DEBHELPER# diff --git a/packaging/systemd/ezcoo-usb-control.service b/packaging/systemd/ezcoo-usb-control.service new file mode 100644 index 0000000..b2fe6c3 --- /dev/null +++ b/packaging/systemd/ezcoo-usb-control.service @@ -0,0 +1,26 @@ +[Unit] +Description=EZCOO HDMI Matrix MQTT bridge +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +ExecStart=/usr/bin/ezcoo-usb-control --config /etc/ezcoo-usb-control/config.yaml +Restart=on-failure +RestartSec=5 +User=ezcoo +SupplementaryGroups=dialout + +# Allow reading an optional environment file for secrets. +# EnvironmentFile=-/etc/ezcoo-usb-control/env + +# Hardening +NoNewPrivileges=true +ProtectSystem=strict +ProtectHome=true +PrivateTmp=true +DeviceAllow=char-ttyUSB rw +DeviceAllow=char-ttyACM rw + +[Install] +WantedBy=multi-user.target