From 9ae5259c592406577656b8432c4aa2e86ea2a7f6 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Thu, 20 Nov 2025 15:18:15 +0800
Subject: [PATCH] Add generate deb package support

---
 project/lightd/makefile      |   14 +++-
 .gitignore                   |    6 +
 project/deb/build.sh         |  127 ++++++++++++++++++++++++++++++++++++++++++
 project/deb/makefile         |   14 ++++
 project/thingsboard/makefile |   14 +++-
 5 files changed, 168 insertions(+), 7 deletions(-)

diff --git a/.gitignore b/.gitignore
index e66af8e..75e1e26 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,7 +8,10 @@
 *.o
 lib*.so*
 lib*.a
-mqttd
+
+# deb files
+project/deb/*d
+*.deb
 
 # ctags files
 cscope.*
@@ -28,6 +31,7 @@
 
 # symbolic link
 project/*/booster
+project/*/deb
 project/*/modules
 project/*/openlibs
 
diff --git a/project/deb/build.sh b/project/deb/build.sh
new file mode 100755
index 0000000..5c40df9
--- /dev/null
+++ b/project/deb/build.sh
@@ -0,0 +1,127 @@
+#!/bin/bash
+
+set -e
+
+PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
+APP_NAME=$(basename "${PRJ_PATH}")
+PRJ_APP=${PRJ_PATH}
+VERSION=1.0
+ARCH=armhf
+
+do_export() {
+    OUTPUT_DEB="${APP_NAME}.deb"
+    BUILD_DIR="${APP_NAME}"
+
+    BINARY_PATH="${PRJ_APP}/${APP_NAME}"
+    CONF_PATH="${PRJ_APP}/etc/${APP_NAME}.conf"
+    SERVICE_PATH="${PRJ_APP}/etc/${APP_NAME}.service"
+    DEBIAN_DIR="${BUILD_DIR}/DEBIAN"
+}
+
+# Clean all build artifacts
+do_clean() {
+    rm -rf ${BUILD_DIR} ${OUTPUT_DEB}
+}
+
+do_fetch() {
+    # Validate required input files
+    if [ ! -f ${BINARY_PATH} ]; then
+        echo "ERROR: Binary not found at ${BINARY_PATH}" >&2
+        exit 1
+    fi
+    if [ ! -f ${CONF_PATH} ]; then
+        echo "ERROR: Config file not found at ${CONF_PATH}" >&2
+        exit 1
+    fi
+    if [ ! -f ${SERVICE_PATH} ]; then
+        echo "ERROR: Service file not found at ${SERVICE_PATH}" >&2
+        exit 1
+    fi
+
+    # ensure clean state before fetch
+    do_clean
+
+    mkdir -p ${DEBIAN_DIR}
+    install -D -m 755 ${BINARY_PATH} ${BUILD_DIR}/usr/bin/${APP_NAME}
+    install -D -m 644 ${CONF_PATH} ${BUILD_DIR}/etc/${APP_NAME}.conf
+    install -D -m 644 ${SERVICE_PATH} ${BUILD_DIR}/lib/systemd/system/${APP_NAME}.service
+}
+
+do_conf() {
+    cat > ${DEBIAN_DIR}/control <<EOF
+Package: ${APP_NAME}
+Version: ${VERSION}
+Section: utils
+Priority: optional
+Architecture: ${ARCH}
+Maintainer: Guowenxue <guowenxue@gmail.com>
+Description: Custom ${APP_NAME} daemon for Raspberry Pi
+ This package provides the ${APP_NAME} service.
+Depends: systemd
+EOF
+
+    cat > ${DEBIAN_DIR}/postinst <<EOF
+#!/bin/sh
+set -e
+if [ "\$1" = "configure" ]; then
+    systemctl daemon-reload
+    systemctl enable --now ${APP_NAME}.service
+fi
+EOF
+    chmod +x ${DEBIAN_DIR}/postinst
+
+    cat > ${DEBIAN_DIR}/prerm <<EOF
+#!/bin/sh
+set -e
+if [ "\$1" = "remove" ]; then
+    systemctl stop ${APP_NAME}.service || true
+    systemctl disable ${APP_NAME}.service || true
+fi
+EOF
+    chmod +x ${DEBIAN_DIR}/prerm
+
+    echo /etc/${APP_NAME}.conf > ${DEBIAN_DIR}/conffiles
+}
+
+do_build() {
+    dpkg-deb --build ${BUILD_DIR} ${OUTPUT_DEB}
+}
+
+# Parse command-line options
+do_args() {
+    local clean_only=0
+
+    while getopts "c" opt; do
+        case $opt in
+            c)
+                clean_only=1
+                ;;
+            \?)
+                echo "Usage: $0 [-c] [APP_NAME]" >&2
+                exit 1
+                ;;
+        esac
+    done
+
+    shift $((OPTIND - 1))
+    if [ $# -gt 0 ]; then
+        APP_NAME="$1"
+        PRJ_APP=$PRJ_PATH/${APP_NAME}
+    fi
+
+    do_export
+
+    if [ "$clean_only" = "1" ]; then
+        do_clean
+        exit
+    fi;
+}
+
+main() {
+    do_args "$@"
+    do_fetch
+    do_conf
+    do_build
+}
+
+main "$@"
diff --git a/project/deb/makefile b/project/deb/makefile
new file mode 100644
index 0000000..6bbeff8
--- /dev/null
+++ b/project/deb/makefile
@@ -0,0 +1,14 @@
+
+PRJ_PATH=$(shell pwd)
+
+DEB_FILES := $(wildcard *.deb)
+DEB_DIRS  := $(patsubst %.deb,%,$(DEB_FILES))
+
+all:
+	@./build.sh ${APP_NAME}
+
+clean:
+	@./build.sh -c ${APP_NAME}
+
+distclean: clean
+	@rm -rf ${DEB_FILES} ${DEB_DIRS}
diff --git a/project/lightd/makefile b/project/lightd/makefile
index 2920afa..e56ef2f 100644
--- a/project/lightd/makefile
+++ b/project/lightd/makefile
@@ -12,7 +12,9 @@
 #*******************************************************************************
 
 PRJ_PATH=$(shell pwd)
-APP_NAME = lightd 
+APP_NAME=$(shell basename ${PRJ_PATH})
+DEB_NAME=${APP_NAME}.deb
+DEB_PATH=${PRJ_PATH}/deb
 
 BUILD_ARCH=$(shell uname -m)
 ifneq ($(findstring $(BUILD_ARCH), "x86_64" "i386"),)
@@ -45,6 +47,7 @@
 
 all: entry subdir
 	${CROSS_COMPILE}gcc ${CFLAGS} ${SRCFILES} -o ${APP_NAME} ${LDFLAGS}
+	@make gendeb
 
 entry:
 	@echo "Building ${APP_NAME} on ${BUILD_ARCH}"
@@ -53,16 +56,21 @@
 	@for dir in ${libs} ; do if [ ! -e $${dir} ] ; then ln -s ../$${dir}; fi; done
 	@for dir in ${libs} ;  do CFLAGS="${CFLAGS}" make -C $${dir} ; done
 
+gendeb:
+	@ if [ ! -L ${DEB_PATH} ]; then ln -s ../deb ${DEB_PATH}; fi
+	@make -C ${DEB_PATH} APP_NAME=${APP_NAME}
+
 install:
-	cp ${APP_NAME} /tftp
+	cp ${APP_NAME} ${DEB_PATH}/${DEB_NAME} /tftp
 
 clean:
 	@for dir in ${SRCS} ; do if [ -e $${dir} ] ; then make clean -C $${dir}; fi; done
+	@make clean -C ${DEB_PATH} APP_NAME=${APP_NAME}
 	@rm -f ${APP_NAME}
 
 distclean:
 	@for dir in ${libs} ; do if [ -e $${dir} ] ; then make clean -C $${dir}; fi; done
-	@for dir in ${libs} ; do if [ -e $${dir} ] ; then unlink $${dir}; fi; done
+	@for dir in ${libs} ; do if [ -L $${dir} ] ; then unlink $${dir}; fi; done
 	@rm -f ${APP_NAME}
 	@rm -f cscope.* tags
 
diff --git a/project/thingsboard/makefile b/project/thingsboard/makefile
index f204d55..78bbe8f 100644
--- a/project/thingsboard/makefile
+++ b/project/thingsboard/makefile
@@ -12,7 +12,9 @@
 #*******************************************************************************
 
 PRJ_PATH=$(shell pwd)
-APP_NAME = thingsboard
+APP_NAME=$(shell basename ${PRJ_PATH})
+DEB_NAME=${APP_NAME}.deb
+DEB_PATH=${PRJ_PATH}/deb
 
 BUILD_ARCH=$(shell uname -m)
 ifneq ($(findstring $(BUILD_ARCH), "x86_64" "i386"),)
@@ -45,6 +47,7 @@
 
 all: entry subdir
 	${CROSS_COMPILE}gcc ${CFLAGS} ${SRCFILES} -o ${APP_NAME} ${LDFLAGS}
+	@make gendeb
 
 entry:
 	@echo "Building ${APP_NAME} on ${BUILD_ARCH}"
@@ -53,16 +56,21 @@
 	@for dir in ${libs} ; do if [ ! -e $${dir} ] ; then ln -s ../$${dir}; fi; done
 	@for dir in ${libs} ;  do CFLAGS="${CFLAGS}" make -C $${dir} ; done
 
+gendeb:
+	@ if [ ! -L ${DEB_PATH} ]; then ln -s ../deb ${DEB_PATH}; fi
+	@make -C ${DEB_PATH} APP_NAME=${APP_NAME}
+
 install:
-	cp ${APP_NAME} /tftp
+	cp ${APP_NAME} ${DEB_PATH}/${DEB_NAME} /tftp
 
 clean:
 	@for dir in ${SRCS} ; do if [ -e $${dir} ] ; then make clean -C $${dir}; fi; done
+	@make clean -C ${DEB_PATH} APP_NAME=${APP_NAME}
 	@rm -f ${APP_NAME}
 
 distclean:
 	@for dir in ${libs} ; do if [ -e $${dir} ] ; then make clean -C $${dir}; fi; done
-	@for dir in ${libs} ; do if [ -e $${dir} ] ; then unlink $${dir}; fi; done
+	@for dir in ${libs} ; do if [ -L $${dir} ] ; then unlink $${dir}; fi; done
 	@rm -f ${APP_NAME}
 	@rm -f cscope.* tags
 

--
Gitblit v1.9.1