From 64e794f1b51c127243f23bbebf48d42e224a6e37 Mon Sep 17 00:00:00 2001
From: Arksine <9563098+Arksine@users.noreply.github.com>
Date: Mon, 19 Apr 2021 14:46:33 -0400
Subject: [PATCH] scripts: add fetch-apikey helper script

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
---
 scripts/fetch-apikey.sh | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100755 scripts/fetch-apikey.sh

diff --git a/scripts/fetch-apikey.sh b/scripts/fetch-apikey.sh
new file mode 100755
index 0000000..d022366
--- /dev/null
+++ b/scripts/fetch-apikey.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+# Helper Script for fetching the API Key from a moonraker database
+DATABASE_PATH="${HOME}/.moonraker_database"
+MOONRAKER_ENV="${HOME}/moonraker-env"
+DB_ARGS="--read=READ --db=authorized_users get _API_KEY_USER_"
+API_REGEX='(?<="api_key": ")([^"]+)'
+
+print_help()
+{
+    echo "Moonraker API Key Extraction Utility"
+    echo
+    echo "usage: fetch-apikey.sh [-h] [-e <python env path>] [-d <database path>]"
+    echo
+    echo "optional arguments:"
+    echo "  -h                  show this message"
+    echo "  -e <env path>       path to Moonraker env folder"
+    echo "  -d <database path>  path to Moonraker LMDB database folder"
+    exit 0
+}
+
+# Parse command line arguments
+while getopts "he:d:" arg; do
+    case $arg in
+        h) print_help;;
+        e) MOONRAKER_ENV=$OPTARG;;
+        d) DATABASE_PATH=$OPTARG;;
+    esac
+done
+
+PYTHON_BIN="${MOONRAKER_ENV}/bin/python"
+
+if [ ! -f $PYTHON_BIN ]; then
+    echo "No Python binary found at '${PYTHON_BIN}'"
+    exit -1
+fi
+
+if [ ! -d $DATABASE_PATH ]; then
+    echo "No Moonraker database found at '${DATABASE_PATH}'"
+    exit -1
+fi
+
+${PYTHON_BIN} -mlmdb --env=${DATABASE_PATH} ${DB_ARGS} | grep -Po "${API_REGEX}"