From 967036acf323a94dd9e75128af3584df79c6457f Mon Sep 17 00:00:00 2001
From: ewin <git@ewin.moe>
Date: Thu, 13 Mar 2025 11:08:53 -0400
Subject: [PATCH] initial commit

---
 index.mjs    | 35 +++++++++++++++++++++++++++++++++++
 package.json |  9 +++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 index.mjs
 create mode 100644 package.json

diff --git a/index.mjs b/index.mjs
new file mode 100644
index 0000000..1b6ccd1
--- /dev/null
+++ b/index.mjs
@@ -0,0 +1,35 @@
+import {glob, readFile, stat, watch} from 'node:fs/promises';
+import path from 'node:path';
+
+const steamUserID = process.env.STEAM_USER_ID;
+const homeDir = process.env.HOME;
+
+const solutionsDir = `${homeDir}/.local/share/Opus Magnum/${steamUserID}`;
+
+async function readSolutionName (solutionPath) {
+	const buf = await readFile(solutionPath);
+	return buf.subarray(0x0A, 0x0A + buf[0x09]).toString('utf-8');
+}
+
+const solutionNames = Object.create(null);
+
+for await (const solutionPath of glob(path.join(solutionsDir, '*.solution'))) {
+	const solutionName = await readSolutionName(solutionPath);
+	solutionNames[solutionPath] = solutionName;
+}
+
+for await (const {filename} of watch(solutionsDir)) {
+	if (path.extname(filename) !== '.solution') continue; // ignore options etc
+
+	const solutionPath = path.join(solutionsDir, filename);
+	if (!(await stat(solutionPath)).isFile) continue; // ignore deleted
+
+	const solutionName = await readSolutionName(solutionPath);
+	if (solutionName !== solutionNames[solutionPath]) {
+		console.log(
+			'changed:', filename,
+			solutionNames[solutionPath], '->', solutionName
+		);
+		solutionNames[solutionPath] = solutionName;
+	}
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6399799
--- /dev/null
+++ b/package.json
@@ -0,0 +1,9 @@
+{
+  "name": "opus-magnum-solution-watcher",
+  "private": true,
+  "type": "module",
+  "main": "index.mjs",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  }
+}