diff --git a/debian/arduino-app-cli/DEBIAN/postinst b/debian/arduino-app-cli/DEBIAN/postinst index 8dbe7413..da060a89 100755 --- a/debian/arduino-app-cli/DEBIAN/postinst +++ b/debian/arduino-app-cli/DEBIAN/postinst @@ -4,3 +4,4 @@ chown -R arduino:arduino /home/arduino/.local/share/arduino-app-cli systemctl enable arduino-app-cli systemctl enable arduino-burn-bootloader +systemctl enable arduino-avahi-serial.service diff --git a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service new file mode 100644 index 00000000..17a4f327 --- /dev/null +++ b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service @@ -0,0 +1,17 @@ +[Unit] +Description=Configure Avahi with board serial number +Before=avahi-daemon.service +ConditionPathExists=!/var/lib/arduino/avahi_serial_configured.flag + +[Service] +Type=oneshot +RemainAfterExit=true +ExecStart=/usr/sbin/arduino-avahi-serial.sh +ExecStartPost=/bin/mkdir -p /var/lib/arduino +ExecStartPost=/bin/touch /var/lib/arduino/avahi_serial_configured.flag + +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh new file mode 100755 index 00000000..89f89fb1 --- /dev/null +++ b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# +# Configure Avahi with the serial number. +# This operation is non-blocking: if it fails, +# the script will exit with success in order to +# not to interrupt the post-install process. +# + +TARGET_FILE="/etc/avahi/services/arduino.service" +MARKER_LINE="" +SERIAL_NUMBER_PATH="/sys/devices/soc0/serial_number" + +echo "Configuring Avahi with serial number for network discovery..." + +if [ ! -f "$SERIAL_NUMBER_PATH" ]; then + echo "Warning: Serial number path not found at $SERIAL_NUMBER_PATH. Skipping." >&2 + exit 0 +fi + + +if [ ! -w "$TARGET_FILE" ]; then + echo "Warning: Target file $TARGET_FILE not found or not writable. Skipping." >&2 + exit 0 +fi + +SERIAL_NUMBER=$(cat "$SERIAL_NUMBER_PATH") + +if [ -z "$SERIAL_NUMBER" ]; then + echo "Warning: Serial number file is empty. Skipping." >&2 + exit 0 +fi + +if grep -q "serial_number=${SERIAL_NUMBER}" "$TARGET_FILE"; then + echo "Serial number ($SERIAL_NUMBER) already configured. Skipping." + exit 0 +fi + +SERIAL_NUMBER_ESCAPED=$(echo "$SERIAL_NUMBER" | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/\&/\\\&/g') +NEW_LINE=" serial_number=${SERIAL_NUMBER_ESCAPED}" + +echo "Adding serial number to $TARGET_FILE..." + +sed -i "\#${MARKER_LINE}#i ${NEW_LINE}" "$TARGET_FILE" + +echo "Avahi configuration attempt finished." +exit 0 \ No newline at end of file