#!/bin/bash set -e # 配置变量 APP_NAME="ScreenLockDemo" PACKAGE_NAME="screenlockdemo" VERSION="1.0.0" ARCH="amd64" QT_DIR="$HOME/sdk/qt-5.15.2" LINUXDEPLOYQT="$HOME/dev/sunvpack-py/bin/linuxdeployqt" BUILD_DIR="$(pwd)/build" PACKAGE_DIR="$(pwd)/deb_package" echo "=========================================" echo "Qt Screen Lock Demo - 快速 DEB 打包" echo "=========================================" # 检查工具 if [ ! -f "$LINUXDEPLOYQT" ]; then echo "错误: linuxdeployqt 未找到: $LINUXDEPLOYQT" exit 1 fi if [ ! -d "$QT_DIR" ]; then echo "错误: Qt 目录未找到: $QT_DIR" exit 1 fi # 清理并创建目录 echo "[1/6] 清理目录..." rm -rf "$PACKAGE_DIR" mkdir -p "$PACKAGE_DIR" # 编译项目 echo "[2/6] 编译项目..." if [ ! -f "$BUILD_DIR/bin/$APP_NAME" ]; then ./build.sh fi if [ ! -f "$BUILD_DIR/bin/$APP_NAME" ]; then echo "错误: 编译失败" exit 1 fi # 创建 AppDir echo "[3/6] 创建 AppDir..." APPDIR="${PACKAGE_DIR}/AppDir" mkdir -p "${APPDIR}/usr/bin" mkdir -p "${APPDIR}/usr/share/applications" mkdir -p "${APPDIR}/usr/share/icons/hicolor/256x256/apps" cp "$BUILD_DIR/bin/$APP_NAME" "${APPDIR}/usr/bin/" chmod +x "${APPDIR}/usr/bin/$APP_NAME" cp screenlockdemo.desktop "${APPDIR}/usr/share/applications/" # 创建占位图标 echo "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" | base64 -d > "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png" # 收集依赖 echo "[4/6] 收集依赖 (linuxdeployqt)..." export PATH="${QT_DIR}/bin:$PATH" export LD_LIBRARY_PATH="/usr/local/lib:/usr/local/lib64:${QT_DIR}/lib:$LD_LIBRARY_PATH" cd "$PACKAGE_DIR" "$LINUXDEPLOYQT" "${APPDIR}/usr/bin/$APP_NAME" \ -verbose=2 \ -bundle-non-qt-libs \ -no-translations \ -qmake="${QT_DIR}/bin/qmake" || true cd - > /dev/null # 创建 DEB 结构 echo "[5/6] 创建 DEB 包结构..." DEB_DIR="${PACKAGE_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}" mkdir -p "${DEB_DIR}/opt/${PACKAGE_NAME}" mkdir -p "${DEB_DIR}/usr/bin" mkdir -p "${DEB_DIR}/usr/share/applications" mkdir -p "${DEB_DIR}/usr/share/icons/hicolor/256x256/apps" mkdir -p "${DEB_DIR}/DEBIAN" # 复制文件 cp -r "${APPDIR}/usr/bin" "${DEB_DIR}/opt/${PACKAGE_NAME}/" if [ -d "${APPDIR}/usr/lib" ]; then cp -r "${APPDIR}/usr/lib" "${DEB_DIR}/opt/${PACKAGE_NAME}/" fi if [ -d "${APPDIR}/usr/plugins" ]; then cp -r "${APPDIR}/usr/plugins" "${DEB_DIR}/opt/${PACKAGE_NAME}/" fi # 创建启动包装脚本 cat > "${DEB_DIR}/opt/${PACKAGE_NAME}/${PACKAGE_NAME}.sh" << 'WRAPPER_EOF' #!/bin/bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" export LD_LIBRARY_PATH="/usr/local/lib:/usr/local/lib64:${SCRIPT_DIR}/lib:$LD_LIBRARY_PATH" export QT_PLUGIN_PATH="${SCRIPT_DIR}/plugins" exec "${SCRIPT_DIR}/bin/ScreenLockDemo" "$@" WRAPPER_EOF chmod +x "${DEB_DIR}/opt/${PACKAGE_NAME}/${PACKAGE_NAME}.sh" # 创建系统命令链接 cat > "${DEB_DIR}/usr/bin/${PACKAGE_NAME}" << 'LINK_EOF' #!/bin/bash exec /opt/screenlockdemo/screenlockdemo.sh "$@" LINK_EOF chmod +x "${DEB_DIR}/usr/bin/${PACKAGE_NAME}" # 复制 desktop 和图标 cp "${APPDIR}/usr/share/applications/${PACKAGE_NAME}.desktop" "${DEB_DIR}/usr/share/applications/" sed -i "s|Exec=.*|Exec=/usr/bin/${PACKAGE_NAME}|g" "${DEB_DIR}/usr/share/applications/${PACKAGE_NAME}.desktop" sed -i "s|Icon=.*|Icon=${PACKAGE_NAME}|g" "${DEB_DIR}/usr/share/applications/${PACKAGE_NAME}.desktop" cp "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png" "${DEB_DIR}/usr/share/icons/hicolor/256x256/apps/" # 创建 control INSTALLED_SIZE=$(du -sk "${DEB_DIR}/opt" | cut -f1) cat > "${DEB_DIR}/DEBIAN/control" << CONTROL_EOF Package: ${PACKAGE_NAME} Version: ${VERSION} Section: utils Priority: optional Architecture: ${ARCH} Installed-Size: ${INSTALLED_SIZE} Maintainer: Qt Screen Lock Demo Depends: dbus, libx11-6, libxext6, libxrender1, libfontconfig1, libfreetype6, libglib2.0-0, libgl1 Description: Qt Screen Lock Detection Demo Application Qt5-based screen lock detection with bundled dependencies. Features automatic paint event control, real-time animation, and support for multiple desktop environments. CONTROL_EOF # 创建 postinst cat > "${DEB_DIR}/DEBIAN/postinst" << 'POSTINST_EOF' #!/bin/bash set -e if [ -x /usr/bin/update-desktop-database ]; then update-desktop-database -q fi if [ -x /usr/bin/gtk-update-icon-cache ]; then gtk-update-icon-cache -q -f /usr/share/icons/hicolor 2>/dev/null || true fi echo "Screen Lock Demo 已安装。运行: screenlockdemo" exit 0 POSTINST_EOF chmod 755 "${DEB_DIR}/DEBIAN/postinst" # 构建 DEB echo "[6/6] 构建 DEB 包..." find "${DEB_DIR}" -type d -exec chmod 755 {} \; find "${DEB_DIR}/opt/${PACKAGE_NAME}/bin" -type f -exec chmod 755 {} \; 2>/dev/null || true DEB_FILE="${PACKAGE_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}.deb" dpkg-deb --build "${DEB_DIR}" "$DEB_FILE" if [ -f "$DEB_FILE" ]; then echo "=========================================" echo "✓ 打包成功!" echo "=========================================" echo "包文件: $DEB_FILE" echo "大小: $(du -h "$DEB_FILE" | cut -f1)" echo "" echo "安装: sudo dpkg -i $DEB_FILE" echo "卸载: sudo dpkg -r ${PACKAGE_NAME}" echo "" ls -lh "$DEB_FILE" else echo "错误: 打包失败" exit 1 fi