From e7b286fd1ef35bc2ccfbba6e4184b19dc88a8209 Mon Sep 17 00:00:00 2001
From: s3lph <s3lph@kabelsalat.ch>
Date: Sat, 18 Jan 2025 19:53:02 +0100
Subject: [PATCH] feat: add bsnotify

---
 .forgejo/workflows/package.yml |  1 +
 README.md                      |  1 +
 bsnotify/bsnotify.defaults     |  6 +++++
 bsnotify/bsnotify.service      | 14 ++++++++++
 bsnotify/build.sh              | 49 ++++++++++++++++++++++++++++++++++
 bsnotify/debian.conffiles      |  1 +
 bsnotify/debian.control        | 15 +++++++++++
 bsnotify/debian.postinst       | 11 ++++++++
 bsnotify/debian.postrm         |  9 +++++++
 bsnotify/debian.prerm          |  9 +++++++
 10 files changed, 116 insertions(+)
 create mode 100644 bsnotify/bsnotify.defaults
 create mode 100644 bsnotify/bsnotify.service
 create mode 100755 bsnotify/build.sh
 create mode 100644 bsnotify/debian.conffiles
 create mode 100644 bsnotify/debian.control
 create mode 100755 bsnotify/debian.postinst
 create mode 100755 bsnotify/debian.postrm
 create mode 100755 bsnotify/debian.prerm

diff --git a/.forgejo/workflows/package.yml b/.forgejo/workflows/package.yml
index f41ae68..a520c9d 100644
--- a/.forgejo/workflows/package.yml
+++ b/.forgejo/workflows/package.yml
@@ -34,6 +34,7 @@ jobs:
             --upload-file "${file}" \
             "${API_REPOSITORY_DEB}"
           done
+  bsnotify: *job
   daliserver: *job
   forgejo: *job
   forgejo-runner: *job
diff --git a/README.md b/README.md
index 221cd8c..e37c096 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@ information, please see [https://repo.s3lph.me/](https://repo.s3lph.me/).
 ## Packages
 
 - [ansible-semaphore](https://github.com/ansible-semaphore/semaphore)
+- [bsnotify](https://github.com/jonans/bsnotify)
 - [daliserver](https://github.com/onitake/daliserver)
 - [forgejo](https://codeberg.org/forgejo/forgejo)
 - [forgejo-runner](https://code.forgejo.org/forgejo/runner)
diff --git a/bsnotify/bsnotify.defaults b/bsnotify/bsnotify.defaults
new file mode 100644
index 0000000..cb3edf9
--- /dev/null
+++ b/bsnotify/bsnotify.defaults
@@ -0,0 +1,6 @@
+#
+# This MUST be populated for bsnotify to run:
+# BSNOTIFY_ARGS="<printer ip> <printer serial number> [<local bind ip 1>[,...]]"
+#
+
+BSNOTIFY_ARGS=
diff --git a/bsnotify/bsnotify.service b/bsnotify/bsnotify.service
new file mode 100644
index 0000000..d654343
--- /dev/null
+++ b/bsnotify/bsnotify.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Use Bambu Studio or Orca Slicer with printers outside your LAN
+After=network-online.target
+Wants=network-online.target
+
+[Service]
+EnvironmentFile=-/etc/default/bsnotify
+ExecStart=/usr/bin/python3 /usr/bin/bsnotify $BSNOTIFY_ARGS
+DynamicUser=true
+CapabilityBoundingSet=
+Restart=on-failure
+
+[Install]
+WantedBy=multi-user.target
\ No newline at end of file
diff --git a/bsnotify/build.sh b/bsnotify/build.sh
new file mode 100755
index 0000000..49fca64
--- /dev/null
+++ b/bsnotify/build.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+set -exo pipefail
+
+
+ROOT=$(pwd)
+function fetch() {
+    cd "${SRCDIR}"
+    git clone https://github.com/jonans/bsnotify
+    cd bsnotify
+    export VERSION=0.$(git rev-list --count HEAD)
+}
+
+function prepare() {
+    chmod +x "${SRCDIR}/bsnotify/bsnotify"
+    mkdir -p \
+          "${PKGDIR}/DEBIAN" \
+          "${PKGDIR}/usr/bin" \
+          "${PKGDIR}/etc/default" \
+          "${PKGDIR}/lib/systemd/system"
+    cp "${SRCDIR}/bsnotify" "${PKGDIR}/usr/bin/bsnotify"
+    cp "${ROOT}/bsnotify.defaults" "${PKGDIR}/etc/default/bsnotify"
+    cp "${ROOT}/bsnotify.service" "${PKGDIR}/lib/systemd/system/bsnotify.service"
+    cp "${ROOT}/debian.control" "${PKGDIR}/DEBIAN/control"
+    cp "${ROOT}/debian.conffiles" "${PKGDIR}/DEBIAN/conffiles"
+    cp "${ROOT}/debian.postinst" "${PKGDIR}/DEBIAN/postinst"
+    cp "${ROOT}/debian.prerm" "${PKGDIR}/DEBIAN/prerm"
+    cp "${ROOT}/debian.postrm" "${PKGDIR}/DEBIAN/postrm"
+    sed -re "s/__VERSION__/${VERSION}/g" -i "${PKGDIR}/DEBIAN/control"
+    sed -re "s/__MAINTAINER__/${MAINTAINER}/g" -i "${PKGDIR}/DEBIAN/control"
+    find "${PKGDIR}" -exec touch -m -d "${ISODATE}" {} \;
+}
+
+function package() {
+    cd "${BUILDDIR}"
+    dpkg-deb --build "${PKGDIR}" "${BUILDDIR}"
+}
+
+function build() {
+    export BUILDDIR=${ROOT}/build
+    export SRCDIR=${ROOT}/build/srcdir
+    export PKGDIR=${ROOT}/build/pkgdir
+    mkdir -p ${SRCDIR} ${PKGDIR}
+    fetch
+    prepare
+    package
+}
+
+build
diff --git a/bsnotify/debian.conffiles b/bsnotify/debian.conffiles
new file mode 100644
index 0000000..f95a2ea
--- /dev/null
+++ b/bsnotify/debian.conffiles
@@ -0,0 +1 @@
+/etc/default/bsnotify
diff --git a/bsnotify/debian.control b/bsnotify/debian.control
new file mode 100644
index 0000000..e154802
--- /dev/null
+++ b/bsnotify/debian.control
@@ -0,0 +1,15 @@
+Package: bsnotify
+Version: __VERSION__
+Maintainer: __MAINTAINER__
+Section: web
+Priority: optional
+Architecture: amd64
+Depends: python3
+Description: Use Bambu Studio or Orca Slicer with printers outside your LAN
+ Bambu Lab printers have a LAN only mode to avoid relying on cloud
+ services for general tasks. One limitation is that Bambu Studio can
+ only find your printer when both are on the same LAN. Discovery
+ happens using SSDP but due non-standard/broken implementation it does
+ not work when they are on different LANs. Since BS lacks the simple
+ ability to add printer via IP, SSDP is the only way.  Thats where
+ bsnotify comes in, it will notify BS of printers outside your LAN.
\ No newline at end of file
diff --git a/bsnotify/debian.postinst b/bsnotify/debian.postinst
new file mode 100755
index 0000000..84b632f
--- /dev/null
+++ b/bsnotify/debian.postinst
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+set -e
+
+if [[ "$1" == "configure" ]]; then
+
+    deb-systemd-helper enable bsnotify.service
+    deb-systemd-invoke restart bsnotify.service
+
+fi
+
diff --git a/bsnotify/debian.postrm b/bsnotify/debian.postrm
new file mode 100755
index 0000000..305068d
--- /dev/null
+++ b/bsnotify/debian.postrm
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -e
+
+if [[ "$1" == "remove" ]]; then
+
+    systemctl daemon-reload || true
+
+fi
diff --git a/bsnotify/debian.prerm b/bsnotify/debian.prerm
new file mode 100755
index 0000000..3fbd44d
--- /dev/null
+++ b/bsnotify/debian.prerm
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -e
+
+if [[ "$1" == "remove" ]]; then
+
+    deb-systemd-invoke stop bsnotify.service
+
+fi