#!/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 "$@"
|