|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# --------------------------------------------------------------------------- |
| 5 | +# Configuration |
| 6 | +# --------------------------------------------------------------------------- |
| 7 | +VER_MAJOR="0" |
| 8 | +VER_MINOR=$(grep -oP '<MinorVersionNr Value="\K[^"]+' src/adminhelperd.lpi) |
| 9 | +VER_REV=$(grep -oP '<RevisionNr Value="\K[^"]+' src/adminhelperd.lpi) |
| 10 | +VER_BUILD=$(grep -oP '<BuildNr Value="\K[^"]+' src/adminhelperd.lpi) |
| 11 | + |
| 12 | +export app_VER="${VER_MAJOR}.${VER_MINOR}.${VER_REV}" |
| 13 | +export app_VERdeb="${VER_BUILD}" |
| 14 | + |
| 15 | +MAINTAINER_NAME="Renat Suleymanov" |
| 16 | +MAINTAINER_EMAIL="mail@Renat.Su" |
| 17 | +PACKAGE_NAME="tgadmin" |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Paths |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 23 | + |
| 24 | +# Build always happens in Linux fs to avoid NTFS chmod issues in WSL |
| 25 | +BUILD_DIR="/tmp/${PACKAGE_NAME}_build" |
| 26 | + |
| 27 | +STAGING_DIR="${BUILD_DIR}/debian" |
| 28 | +DEB_NAME="${PACKAGE_NAME}_${app_VER}-${app_VERdeb}_amd64.deb" |
| 29 | +OUTPUT_DEB="${BUILD_DIR}/${DEB_NAME}" |
| 30 | + |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# Detect WSL |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | +detect_wsl() { |
| 35 | + grep -qi microsoft /proc/version 2>/dev/null || \ |
| 36 | + grep -qi wsl /proc/version 2>/dev/null |
| 37 | +} |
| 38 | + |
| 39 | +# Where to copy the finished .deb. |
| 40 | +# Set via env: WINDOWS_OUTPUT_DIR="/mnt/c/Users/..." ./build-deb.sh |
| 41 | +if [ -n "${WINDOWS_OUTPUT_DIR:-}" ]; then |
| 42 | + COPY_TARGET="${WINDOWS_OUTPUT_DIR}" |
| 43 | +else |
| 44 | + COPY_TARGET="${SCRIPT_DIR}" |
| 45 | +fi |
| 46 | + |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | +# Dependencies' checking |
| 49 | +# --------------------------------------------------------------------------- |
| 50 | +check_deps() { |
| 51 | + local missing=() |
| 52 | + for cmd in dpkg-deb gzip du awk grep jq; do |
| 53 | + command -v "$cmd" &>/dev/null || missing+=("$cmd") |
| 54 | + done |
| 55 | + if [ ${#missing[@]} -gt 0 ]; then |
| 56 | + echo "ERROR: missing required tools: ${missing[*]}" >&2 |
| 57 | + echo "Install with: sudo apt-get install dpkg-dev gzip jq" >&2 |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +# --------------------------------------------------------------------------- |
| 63 | +# Validate tgadmin.json before building |
| 64 | +# --------------------------------------------------------------------------- |
| 65 | +validate_config() { |
| 66 | + local config="${SCRIPT_DIR}/src/tgadmin.json" |
| 67 | + if [ ! -f "${config}" ]; then |
| 68 | + echo "ERROR: src/tgadmin.json not found" >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + if ! jq empty "${config}" 2>/dev/null; then |
| 72 | + echo "ERROR: src/tgadmin.json is not valid JSON" >&2 |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +# --------------------------------------------------------------------------- |
| 78 | +# Main |
| 79 | +# --------------------------------------------------------------------------- |
| 80 | +check_deps |
| 81 | +validate_config |
| 82 | + |
| 83 | +echo "==> Building ${PACKAGE_NAME} v${app_VER}-${app_VERdeb}" |
| 84 | +echo "==> Build dir (Linux fs): ${BUILD_DIR}" |
| 85 | + |
| 86 | +# Recreate build-dir in /tmp from scratch |
| 87 | +rm -rf "${BUILD_DIR}" |
| 88 | +mkdir -p "${BUILD_DIR}" |
| 89 | + |
| 90 | +# Copy staging from sources in /tmp — here chmod works correctly |
| 91 | +cp -r "${SCRIPT_DIR}/debian" "${STAGING_DIR}" |
| 92 | + |
| 93 | +# Include db_schema.sql — used by postinst to initialize the database |
| 94 | +mkdir -p "${STAGING_DIR}/usr/share/${PACKAGE_NAME}" |
| 95 | +cp "${SCRIPT_DIR}/src/db_schema.sql" "${STAGING_DIR}/usr/share/${PACKAGE_NAME}/db_schema.sql" |
| 96 | + |
| 97 | +# Include tgadmin.json as default config |
| 98 | +# postinst will fill in DB credentials automatically |
| 99 | +mkdir -p "${STAGING_DIR}/etc/${PACKAGE_NAME}" |
| 100 | +cp "${SCRIPT_DIR}/src/tgadmin.json" "${STAGING_DIR}/etc/${PACKAGE_NAME}/tgadmin.json" |
| 101 | + |
| 102 | +# Set permissions |
| 103 | +find "${STAGING_DIR}" -type d -exec chmod 0755 {} \; |
| 104 | +find "${STAGING_DIR}" -type f -exec chmod 0644 {} \; |
| 105 | +find "${STAGING_DIR}/usr/bin" -type f -exec chmod 0755 {} \; |
| 106 | + |
| 107 | +# Maintainer scripts must be executable |
| 108 | +chmod 0755 "${STAGING_DIR}/DEBIAN/postinst" |
| 109 | +chmod 0755 "${STAGING_DIR}/DEBIAN/postrm" |
| 110 | + |
| 111 | +# Config file should not be world-readable (contains credentials after install) |
| 112 | +chmod 0640 "${STAGING_DIR}/etc/${PACKAGE_NAME}/tgadmin.json" |
| 113 | + |
| 114 | +# Version and size in control |
| 115 | +echo "Version: ${app_VER}.0-${app_VERdeb}" >> "${STAGING_DIR}/DEBIAN/control" |
| 116 | +SIZE_IN_KB="$(du -s "${STAGING_DIR}" | awk '{print $1}')" |
| 117 | +echo "Installed-Size: ${SIZE_IN_KB}" >> "${STAGING_DIR}/DEBIAN/control" |
| 118 | + |
| 119 | +# Changelog |
| 120 | +CHANGELOG="${STAGING_DIR}/usr/share/doc/${PACKAGE_NAME}/changelog.Debian" |
| 121 | +DATE=$(date -R) |
| 122 | +{ |
| 123 | + echo "${PACKAGE_NAME} (${app_VER}-${app_VERdeb}) unstable; urgency=medium" |
| 124 | + echo "" |
| 125 | + echo " * fixes" |
| 126 | + echo "" |
| 127 | + echo " -- ${MAINTAINER_NAME} <${MAINTAINER_EMAIL}> ${DATE}" |
| 128 | +} >> "${CHANGELOG}" |
| 129 | +gzip -9 -n "${CHANGELOG}" |
| 130 | + |
| 131 | +# Package building |
| 132 | +dpkg-deb --root-owner-group --build "${STAGING_DIR}" "${OUTPUT_DEB}" |
| 133 | + |
| 134 | +echo "==> Package built: ${OUTPUT_DEB}" |
| 135 | + |
| 136 | +# Copy ready .deb to needed place |
| 137 | +mkdir -p "${COPY_TARGET}" |
| 138 | +cp "${OUTPUT_DEB}" "${COPY_TARGET}/" |
| 139 | +echo "==> Copied to: ${COPY_TARGET}/${DEB_NAME}" |
| 140 | + |
| 141 | +echo "==> Done." |
0 commit comments