Files
hkkb-layout-web/run.sh
2026-05-27 18:30:14 +09:00

47 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
# run.sh — build, ensure the keyboard is accessible, and launch the web editor.
#
# On the first run it asks for your doas password once to install a udev rule
# (and fix the current device node). After that, access is persistent and no
# password is needed. Pass extra flags through to hhkb-web, e.g. ./run.sh -open=false
set -u
cd "$(dirname "$0")"
echo "[1/3] building hhkb-web…"
go build -o hhkb-web ./cmd/hhkb-web || { echo "build failed"; exit 1; }
# Locate the HHKB (USB vendor 04fe) hidraw nodes.
node=""
for n in /dev/hidraw*; do
[ -e "$n" ] || continue
if grep -q 000004FE "/sys/class/hidraw/${n##*/}/device/uevent" 2>/dev/null; then
node="$n"
break
fi
done
if [ -z "$node" ]; then
echo "[2/3] no HHKB detected (is it plugged in?) — starting anyway."
elif [ -r "$node" ] && [ -w "$node" ]; then
echo "[2/3] keyboard already accessible — no password needed."
else
echo "[2/3] granting access (doas password needed once)…"
doas sh -c '
mkdir -p /etc/udev/rules.d
printf "%s\n" "KERNEL==\"hidraw*\", ATTRS{idVendor}==\"04fe\", MODE=\"0660\", GROUP=\"input\"" \
> /etc/udev/rules.d/70-hhkb.rules
udevadm control --reload-rules
udevadm trigger
for n in /dev/hidraw*; do
grep -q 000004FE "/sys/class/hidraw/${n##*/}/device/uevent" 2>/dev/null \
&& chgrp input "$n" && chmod g+rw "$n"
done
true
' && echo " done — rule installed, future runs need no password." \
|| echo " setup failed; the editor will show the access banner."
fi
pkill -x hhkb-web 2>/dev/null || true # stop any previous instance so the new build is served
echo "[3/3] http://127.0.0.1:8080"
exec ./hhkb-web "$@"