From 611bbdd444aac08e952d55c9f981304af5b51ed3 Mon Sep 17 00:00:00 2001
From: Peter Steenbergen <info@j1nx.nl>
Date: Mon, 30 Sep 2019 16:13:38 +0200
Subject: [PATCH] MycroftOS: Further work on the MycroftOS-WiFiSetup system

---
 buildroot-external/Config.in                  |  2 +-
 .../configs/mycroftos_rpi3_defconfig          | 11 +++---
 .../firstboot-service/firstboot.service       |  4 +--
 .../package/nodogsplash/Config.in             | 13 -------
 .../package/nodogsplash/nodogsplash.hash      |  2 --
 .../package/nodogsplash/nodogsplash.mk        | 34 -------------------
 .../package/wifisetup-service/Config.in       |  4 +++
 .../mycroftos-wifisetup.service               | 12 +++++++
 .../wifisetup-service/wifisetup-service.mk    | 21 ++++++++++++
 9 files changed, 47 insertions(+), 56 deletions(-)
 delete mode 100644 buildroot-external/package/nodogsplash/Config.in
 delete mode 100644 buildroot-external/package/nodogsplash/nodogsplash.hash
 delete mode 100644 buildroot-external/package/nodogsplash/nodogsplash.mk
 create mode 100644 buildroot-external/package/wifisetup-service/Config.in
 create mode 100644 buildroot-external/package/wifisetup-service/mycroftos-wifisetup.service
 create mode 100644 buildroot-external/package/wifisetup-service/wifisetup-service.mk

diff --git a/buildroot-external/Config.in b/buildroot-external/Config.in
index ff331abf..007fa0d4 100644
--- a/buildroot-external/Config.in
+++ b/buildroot-external/Config.in
@@ -3,9 +3,9 @@ menu "Mycroft A.I. Personal Assistant"
 	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/python-mycroft/Config.in"
 	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/mycroft-service/Config.in"
 	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/mycroft-splash/Config.in"
+	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/wifisetup-service/Config.in"
 menu "Additional drivers, libraries and/or applications"
 	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/fann/Config.in"
-	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/nodogsplash/Config.in"
 	source "$BR2_EXTERNAL_MYCROFTOS_PATH/package/respeaker/Config.in"
 endmenu
 menu "Additional external python modules"
diff --git a/buildroot-external/configs/mycroftos_rpi3_defconfig b/buildroot-external/configs/mycroftos_rpi3_defconfig
index 43e68191..9f471c52 100644
--- a/buildroot-external/configs/mycroftos_rpi3_defconfig
+++ b/buildroot-external/configs/mycroftos_rpi3_defconfig
@@ -87,8 +87,10 @@ BR2_PACKAGE_RPI_WIFI_FIRMWARE=y
 BR2_PACKAGE_DBUS_PYTHON=y
 BR2_PACKAGE_KBD=y
 BR2_PACKAGE_PARTED=y
-BR2_PACKAGE_RPI_USERLAND=y
 BR2_PACKAGE_SPI_TOOLS=y
+BR2_PACKAGE_NODEJS=y
+BR2_PACKAGE_NODEJS_NPM=y
+BR2_PACKAGE_NODEJS_MODULES_ADDITIONAL="body-parser express handlebars git://github.com/j1nx/MycroftOS-WiFiSetup.git"
 BR2_PACKAGE_PYTHON3=y
 BR2_PACKAGE_PYTHON3_PY_PYC=y
 BR2_PACKAGE_PYTHON3_BZIP2=y
@@ -136,14 +138,13 @@ BR2_PACKAGE_LIBOPENSSL_ENGINES=y
 BR2_PACKAGE_LIBCONFIG=y
 BR2_PACKAGE_LIBNFS=y
 BR2_PACKAGE_BCM2835=y
-BR2_PACKAGE_DTC=y
-BR2_PACKAGE_DTC_PROGRAMS=y
 BR2_PACKAGE_WIRINGPI=y
 BR2_PACKAGE_LIBMPEG2=y
 BR2_PACKAGE_LIBMPEG2_BINS=y
 BR2_PACKAGE_LIBTHEORA=y
 BR2_PACKAGE_LIBCURL=y
 BR2_PACKAGE_CURL=y
+BR2_PACKAGE_LIBMICROHTTPD=y
 BR2_PACKAGE_LIBTIRPC=y
 BR2_PACKAGE_LIBFRIBIDI=y
 BR2_PACKAGE_LIBUNISTRING=y
@@ -156,6 +157,8 @@ BR2_PACKAGE_PCRE2_32=y
 BR2_PACKAGE_CONNMAN=y
 BR2_PACKAGE_CONNMAN_WIFI=y
 BR2_PACKAGE_CONNMAN_BLUETOOTH=y
+BR2_PACKAGE_DNSMASQ=y
+BR2_PACKAGE_HOSTAPD=y
 BR2_PACKAGE_IW=y
 BR2_PACKAGE_NTP=y
 BR2_PACKAGE_OPENSSH=y
@@ -194,8 +197,8 @@ BR2_PACKAGE_FIRSTBOOT_SERVICE=y
 BR2_PACKAGE_PYTHON_MYCROFT=y
 BR2_PACKAGE_MYCROFT_SERVICE=y
 BR2_PACKAGE_MYCROFT_SPLASH=y
+BR2_PACKAGE_WIFISETUP_SERVICE=y
 BR2_PACKAGE_FANN=y
-BR2_PACKAGE_NODOGSPLASH=y
 BR2_PACKAGE_RESPEAKER=y
 BR2_PACKAGE_PYTHON_ADAPT_PARSER=y
 BR2_PACKAGE_PYTHON_COLORZERO=y
diff --git a/buildroot-external/package/firstboot-service/firstboot.service b/buildroot-external/package/firstboot-service/firstboot.service
index 4f140799..6a18a865 100644
--- a/buildroot-external/package/firstboot-service/firstboot.service
+++ b/buildroot-external/package/firstboot-service/firstboot.service
@@ -5,12 +5,12 @@ Conflicts=shutdown.target
 After=systemd-remount-fs.service
 Before=sysinit.target shutdown.target
 ConditionPathIsReadWrite=/etc
-ConditionFirstBoot=yes
+ConditionPathExists=/etc/firstboot
 
 [Service]
 Type=oneshot
 RemainAfterExit=yes
-ExecStart=/usr/sbin/resizeSD
+ExecStart=/usr/sbin/firstboot
 
 [Install]
 WantedBy=sysinit.target
diff --git a/buildroot-external/package/nodogsplash/Config.in b/buildroot-external/package/nodogsplash/Config.in
deleted file mode 100644
index 48851d71..00000000
--- a/buildroot-external/package/nodogsplash/Config.in
+++ /dev/null
@@ -1,13 +0,0 @@
-config BR2_PACKAGE_NODOGSPLASH
-	bool "nodogsplash"
-	select BR2_PACKAGE_LIBMICROHTTPD
-	help
-	  Nodogspash (NDS) is a high performance, 
-	  small footprint Captive Portal, offering 
-	  by default a simple splash page restricted 
-	  Internet connection, yet incorporates an 
-	  API that allows the creation of 
-	  sophisticated authentication applications.
-
-	  https://github.com/nodogsplash/nodogsplash
-
diff --git a/buildroot-external/package/nodogsplash/nodogsplash.hash b/buildroot-external/package/nodogsplash/nodogsplash.hash
deleted file mode 100644
index fc2a8bfc..00000000
--- a/buildroot-external/package/nodogsplash/nodogsplash.hash
+++ /dev/null
@@ -1,2 +0,0 @@
-# Locally computed
-sha256	fdc936b1b76bcae9a0a42cd8887d4cb5037fb328449a57c2c33f03ad5013638c  nodogsplash-3.2.1.tar.gz
diff --git a/buildroot-external/package/nodogsplash/nodogsplash.mk b/buildroot-external/package/nodogsplash/nodogsplash.mk
deleted file mode 100644
index fd0b3645..00000000
--- a/buildroot-external/package/nodogsplash/nodogsplash.mk
+++ /dev/null
@@ -1,34 +0,0 @@
-################################################################################
-#
-# nodogsplash
-#
-################################################################################
-
-NODOGSPLASH_VERSION = 3.2.1
-NODOGSPLASH_SITE = $(call github,nodogsplash,nodogsplash,$(NODOGSPLASH_VERSION))
-NODOGSPLASH_LICENSE = GNU General Public License v2.0
-#NODOGSPLASH_AUTORECONF = YES
-#NODOGSPLASH_INSTALL_STAGING = yes
-NODOGSPLASH_DEPENDENCIES = libmicrohttpd
-
-define NODOGSPLASH_BUILD_CMDS
-	$(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(@D) all
-endef
-
-define NODOGSPLASH_INSTALL_TARGET_CMDS
-	$(INSTALL) -D -m 0755 $(@D)/ndsctl $(TARGET_DIR)/usr/bin/
-	$(INSTALL) -D -m 0755 $(@D)/nodogsplash $(TARGET_DIR)/usr/bin/
-	mkdir -p $(TARGET_DIR)/etc/nodogsplash/htdocs/images
-	$(INSTALL) -D -m 0644 $(@D)/resources/nodogsplash.conf \
-		$(TARGET_DIR)/etc/nodogsplash/
-	$(INSTALL) -D -m 0644 $(@D)/resources/splash.html \
-                $(TARGET_DIR)/etc/nodogsplash/htdocs/
-        $(INSTALL) -D -m 0644 $(@D)/resources/splash.css \
-                $(TARGET_DIR)/etc/nodogsplash/htdocs/
-        $(INSTALL) -D -m 0644 $(@D)/resources/status.html \
-                $(TARGET_DIR)/etc/nodogsplash/htdocs/
-        $(INSTALL) -D -m 0644 $(@D)/resources/splash.jpg \
-                $(TARGET_DIR)/etc/nodogsplash/htdocs/images/
-endef
-
-$(eval $(generic-package))
diff --git a/buildroot-external/package/wifisetup-service/Config.in b/buildroot-external/package/wifisetup-service/Config.in
new file mode 100644
index 00000000..33ad9c6a
--- /dev/null
+++ b/buildroot-external/package/wifisetup-service/Config.in
@@ -0,0 +1,4 @@
+config BR2_PACKAGE_WIFISETUP_SERVICE
+	bool "wifisetup-service"
+	help
+	  A systemd service to start the MycroftOS-WiFiSetup.
diff --git a/buildroot-external/package/wifisetup-service/mycroftos-wifisetup.service b/buildroot-external/package/wifisetup-service/mycroftos-wifisetup.service
new file mode 100644
index 00000000..590bca1c
--- /dev/null
+++ b/buildroot-external/package/wifisetup-service/mycroftos-wifisetup.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=MycroftOS Wifi Setup
+After=network.target
+
+[Service]
+Type=simple
+User=root
+WorkingDirectory=/usr/lib/node_modules/MycroftOS-WiFiSetup/
+ExecStart=/usr/lib/node_modules/MycroftOS-WiFiSetup/run.sh 
+
+[Install]
+WantedBy=multi-user.target
diff --git a/buildroot-external/package/wifisetup-service/wifisetup-service.mk b/buildroot-external/package/wifisetup-service/wifisetup-service.mk
new file mode 100644
index 00000000..af2f4a79
--- /dev/null
+++ b/buildroot-external/package/wifisetup-service/wifisetup-service.mk
@@ -0,0 +1,21 @@
+################################################################################
+#
+# wifisetup-service
+#
+################################################################################
+
+WIFISETUP_SERVICE_VERSION = 0.1.0
+WIFISETUP_SERVICE_SITE = $(BR2_EXTERNAL_MYCROFTOS_PATH)/package/wifisetup-service
+WIFISETUP_SERVICE_SITE_METHOD = local
+WIFISETUP_SERVICE_LICENSE = Apache License 2.0
+WIFISETUP_SERVICE_LICENSE_FILES = LICENSE
+
+define WIFISETUP_SERVICE_INSTALL_TARGET_CMDS
+	$(INSTALL) -D -m 644 $(@D)/mycroftos-wifisetup.service \
+		$(TARGET_DIR)/usr/lib/systemd/system/mycroftos-wifisetup.service
+	mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
+	ln -fs ../../../../usr/lib/systemd/system/mycroftos-wifisetup.service \
+		$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/mycroftos-wifisetup.service
+endef
+
+$(eval $(generic-package))