From 74058b00f467d1125b2f2b6c9b44f20c33913b37 Mon Sep 17 00:00:00 2001
From: Michael Gernoth <michael@gernoth.net>
Date: Tue, 31 May 2016 22:32:56 +0200
Subject: [PATCH 1/1] build-system: update for OpenWRT/LEDE package

hmcfgusb can now directly be cloned into package/hmcfgusb and is
then available as a package in OpenWRT/LEDE.
---
 20-hmlan.OpenWRT    | 20 ---------------
 Makefile            | 62 +++++++++++++++++++++++++++++++++++++++++++++
 Makefile.OpenWRT    | 29 ---------------------
 init.hmland.OpenWRT | 14 ++++++++++
 4 files changed, 76 insertions(+), 49 deletions(-)
 delete mode 100644 20-hmlan.OpenWRT
 delete mode 100644 Makefile.OpenWRT
 create mode 100644 init.hmland.OpenWRT

diff --git a/20-hmlan.OpenWRT b/20-hmlan.OpenWRT
deleted file mode 100644
index 247ac30..0000000
--- a/20-hmlan.OpenWRT
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/sh
-
-if [ "${PRODUCT:0:9}" = "1b1f/c00f" -o "${PRODUCT:0:9}" = "1b1f/c010" ]; then
-	logger "HMLAN: ${ACTION} ${PRODUCT}"
-
-	case "$ACTION" in
-		add)
-			if [ "`pidof hmland`" = "" ]; then
-				logger "Starting hmland"
-				hmland -d
-			fi
-			;;
-		remove)
-			if [ "`pidof hmland`" != "" ]; then
-				logger "Stopping hmland"
-				killall hmland
-			fi
-			;;
-	esac
-fi
diff --git a/Makefile b/Makefile
index bd8fbf7..0cf13bd 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,6 @@
+ifeq ($(OPENWRT_BUILD),)
+
+#Normal system
 CFLAGS=-MMD -O2 -Wall -I/opt/local/include -g
 LDFLAGS=-L/opt/local/lib
 LDLIBS=-lusb-1.0 -lrt
@@ -27,3 +30,62 @@ clean:
 	rm -f $(HMLAN_OBJS) $(HMSNIFF_OBJS) $(FLASH_HMCFGUSB_OBJS) $(FLASH_OTA_OBJS) $(DEPEND) hmland hmsniff flash-hmcfgusb flash-ota
 
 .PHONY: all clean
+
+else
+
+#OpenWRT/LEDE
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=hmcfgusb
+PKG_VERSION:=$(shell grep 'VERSION' version.h | cut -d'"' -f 2)
+PKG_RELEASE:=1
+PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/hmcfgusb
+  SECTION:=utils
+  CATEGORY:=Utilities
+  DEPENDS:=+libusb-1.0
+  TITLE:=HM-CFG-USB utilities
+endef
+
+define Build/Prepare
+	mkdir -p $(PKG_BUILD_DIR)
+	$(CP) ./* $(PKG_BUILD_DIR)/
+	$(SED) 's/OPENWRT_BUILD/DISABLED_CHECK_FOR_OPENWRT_TO_USE_CORRECT_BLOCK_NOW/' $(PKG_BUILD_DIR)/Makefile
+endef
+
+define Package/hmcfgusb/install
+	$(INSTALL_DIR) $(1)/usr/sbin/
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hmland $(1)/usr/sbin/
+	$(INSTALL_DIR) $(1)/usr/bin/
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hmsniff $(1)/usr/bin/
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/flash-hmcfgusb $(1)/usr/bin/
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/flash-ota $(1)/usr/bin/
+	$(INSTALL_DIR) $(1)/etc/init.d/
+	$(INSTALL_BIN) $(PKG_BUILD_DIR)/init.hmland.OpenWRT $(1)/etc/init.d/hmland
+endef
+
+define Package/hmcfgusb/postinst
+#!/bin/sh
+# check if we are on real system
+if [ -z "$${IPKG_INSTROOT}" ]; then
+	echo "Enabling rc.d symlink for hmland"
+	/etc/init.d/hmland enable
+fi
+exit 0
+endef
+
+define Package/hmcfgusb/prerm
+#!/bin/sh
+# check if we are on real system
+if [ -z "$${IPKG_INSTROOT}" ]; then
+	echo "Removing rc.d symlink for hmland"
+	/etc/init.d/hmland disable
+fi
+exit 0
+endef
+
+$(eval $(call BuildPackage,hmcfgusb))
+endif
diff --git a/Makefile.OpenWRT b/Makefile.OpenWRT
deleted file mode 100644
index 40b40c3..0000000
--- a/Makefile.OpenWRT
+++ /dev/null
@@ -1,29 +0,0 @@
-include $(TOPDIR)/rules.mk
-
-PKG_NAME:=hmcfgusb
-PKG_RELEASE:=1
-PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
-
-include $(INCLUDE_DIR)/package.mk
-
-define Package/hmcfgusb
-  SECTION:=utils
-  CATEGORY:=Utilities
-  DEPENDS:=+libusb-1.0
-  TITLE:=HM-CFG-USB utilities
-endef
-
-define Build/Prepare
-	mkdir -p $(PKG_BUILD_DIR)
-	$(CP) ./src/* $(PKG_BUILD_DIR)/
-endef
-
-define Package/hmcfgusb/install
-	$(INSTALL_DIR) $(1)/usr/bin/
-	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hmland $(1)/usr/bin/
-	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hmsniff $(1)/usr/bin/
-	$(INSTALL_DIR) $(1)/etc/hotplug.d/usb/
-	$(INSTALL_BIN) $(PKG_BUILD_DIR)/20-hmlan.OpenWRT $(1)/etc/hotplug.d/usb/20-hmlan
-endef
-
-$(eval $(call BuildPackage,hmcfgusb))
diff --git a/init.hmland.OpenWRT b/init.hmland.OpenWRT
new file mode 100644
index 0000000..c27a27b
--- /dev/null
+++ b/init.hmland.OpenWRT
@@ -0,0 +1,14 @@
+#!/bin/sh /etc/rc.common
+
+START=50
+USE_PROCD=1
+
+start_service() {
+	procd_open_instance
+	procd_set_param command /usr/sbin/hmland -p 1000
+	procd_set_param respawn 10 5 0
+	procd_set_param stdout 1
+	procd_set_param stderr 1
+	procd_set_param user root
+	procd_close_instance
+}
-- 
2.39.5