RaspberrPi project source code
guowenxue
16 hours ago 14ef00efc0b93e6301d046ccc8b3e90f9ac60ca4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 "$@"