260 lines
7.7 KiB
Bash
Executable File
260 lines
7.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
# 颜色定义
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 配置变量
|
|
APP_NAME="ScreenLockDetector"
|
|
PACKAGE_NAME="ScreenLockDetector"
|
|
VERSION="1.2.0"
|
|
ARCH="amd64"
|
|
QT_DIR="$HOME/sdk/qt-5.15.2"
|
|
LINUXDEPLOYQT="${LINUXDEPLOYQT:-$HOME/tools/sunvpack-py/bin/linuxdeployqt}"
|
|
BUILD_DIR="$(pwd)/build"
|
|
PACKAGE_DIR="$(pwd)/package"
|
|
DEB_DIR="${PACKAGE_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}"
|
|
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE}Qt Screen Lock Demo - DEB 打包脚本${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
|
|
# 检查 linuxdeployqt 是否存在
|
|
if [ ! -f "$LINUXDEPLOYQT" ]; then
|
|
echo -e "${RED}错误: linuxdeployqt 未找到: $LINUXDEPLOYQT${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# 检查 Qt 目录是否存在
|
|
if [ ! -d "$QT_DIR" ]; then
|
|
echo -e "${RED}错误: Qt 目录未找到: $QT_DIR${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# 清理之前的打包目录
|
|
echo -e "${YELLOW}[1/8] 清理之前的打包目录...${NC}"
|
|
rm -rf "$PACKAGE_DIR"
|
|
mkdir -p "$PACKAGE_DIR"
|
|
|
|
# 编译项目
|
|
echo -e "${YELLOW}[2/8] 编译项目...${NC}"
|
|
if [ ! -f "$BUILD_DIR/bin/$APP_NAME" ]; then
|
|
echo -e "${BLUE}运行编译脚本...${NC}"
|
|
./build.sh
|
|
fi
|
|
|
|
if [ ! -f "$BUILD_DIR/bin/$APP_NAME" ]; then
|
|
echo -e "${RED}错误: 编译失败,可执行文件未找到${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "${GREEN}✓ 编译成功${NC}"
|
|
|
|
# 创建 AppDir 结构
|
|
echo -e "${YELLOW}[3/8] 创建 AppDir 结构...${NC}"
|
|
APPDIR="${PACKAGE_DIR}/AppDir"
|
|
mkdir -p "${APPDIR}/usr/bin"
|
|
mkdir -p "${APPDIR}/usr/lib"
|
|
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"
|
|
|
|
# 复制libstdc++.so.6
|
|
cp -r "/usr/local/lib64/libstdc++.so.6" "${APPDIR}/usr/lib/"
|
|
|
|
# 创建简单图标(如果没有的话)
|
|
if [ ! -f "icon.png" ]; then
|
|
echo -e "${BLUE}创建默认图标...${NC}"
|
|
# 这里只是创建一个占位符,实际应该有真实的图标
|
|
touch "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png"
|
|
else
|
|
cp icon.png "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png"
|
|
fi
|
|
|
|
# 复制 desktop 文件
|
|
cp screenlockdetector.desktop "${APPDIR}/usr/share/applications/"
|
|
|
|
echo -e "${GREEN}✓ AppDir 结构创建完成${NC}"
|
|
|
|
# 使用 linuxdeployqt 收集依赖
|
|
echo -e "${YELLOW}[4/8] 使用 linuxdeployqt 收集所有依赖...${NC}"
|
|
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"
|
|
|
|
cd - > /dev/null
|
|
|
|
echo -e "${GREEN}✓ 依赖收集完成${NC}"
|
|
|
|
# 创建 DEB 包目录结构
|
|
echo -e "${YELLOW}[5/8] 创建 DEB 包目录结构...${NC}"
|
|
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"
|
|
|
|
# 复制 AppDir 内容到 /opt
|
|
cp -r "${APPDIR}/usr/bin" "${DEB_DIR}/opt/${PACKAGE_NAME}/"
|
|
cp -r "${APPDIR}/usr/lib" "${DEB_DIR}/opt/${PACKAGE_NAME}/"
|
|
|
|
# 如果有 Qt plugins
|
|
if [ -d "${APPDIR}/usr/plugins" ]; then
|
|
cp -r "${APPDIR}/usr/plugins" "${DEB_DIR}/opt/${PACKAGE_NAME}/"
|
|
fi
|
|
|
|
# 如果有 Qt 库目录
|
|
if [ -d "${APPDIR}/usr/lib" ]; then
|
|
mkdir -p "${DEB_DIR}/opt/${PACKAGE_NAME}/lib"
|
|
cp -r "${APPDIR}/usr/lib"/* "${DEB_DIR}/opt/${PACKAGE_NAME}/lib/" 2>/dev/null || true
|
|
fi
|
|
|
|
# 创建启动脚本
|
|
cat > "${DEB_DIR}/opt/${PACKAGE_NAME}/bin/${APP_NAME}.sh" << 'EOF'
|
|
#!/bin/bash
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
APP_DIR="$(dirname "$SCRIPT_DIR")"
|
|
export LD_LIBRARY_PATH="/usr/local/lib:/usr/local/lib64:${APP_DIR}/lib:$LD_LIBRARY_PATH"
|
|
export QT_PLUGIN_PATH="${APP_DIR}/plugins"
|
|
exec "${SCRIPT_DIR}/ScreenLockDetector" "$@"
|
|
EOF
|
|
|
|
chmod +x "${DEB_DIR}/opt/${PACKAGE_NAME}/bin/${APP_NAME}.sh"
|
|
|
|
# 创建 /usr/bin 软链接
|
|
cat > "${DEB_DIR}/usr/bin/${PACKAGE_NAME}" << 'EOF'
|
|
#!/bin/bash
|
|
exec /opt/ScreenLockDetector/bin/ScreenLockDetector.sh "$@"
|
|
EOF
|
|
|
|
chmod +x "${DEB_DIR}/usr/bin/${PACKAGE_NAME}"
|
|
|
|
# 复制 desktop 文件和图标
|
|
sed "s|Exec=.*|Exec=/usr/bin/${PACKAGE_NAME}|g" screenlockdetector.desktop > "${DEB_DIR}/usr/share/applications/${PACKAGE_NAME}.desktop"
|
|
sed -i "s|Icon=.*|Icon=${PACKAGE_NAME}|g" "${DEB_DIR}/usr/share/applications/${PACKAGE_NAME}.desktop"
|
|
|
|
if [ -f "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png" ]; then
|
|
cp "${APPDIR}/usr/share/icons/hicolor/256x256/apps/${PACKAGE_NAME}.png" \
|
|
"${DEB_DIR}/usr/share/icons/hicolor/256x256/apps/"
|
|
fi
|
|
|
|
echo -e "${GREEN}✓ DEB 包目录结构创建完成${NC}"
|
|
|
|
# 创建 DEBIAN/control 文件
|
|
echo -e "${YELLOW}[6/8] 创建控制文件...${NC}"
|
|
|
|
# 计算安装大小 (KB)
|
|
INSTALLED_SIZE=$(du -sk "${DEB_DIR}/opt" | cut -f1)
|
|
|
|
cat > "${DEB_DIR}/DEBIAN/control" << EOF
|
|
Package: ${PACKAGE_NAME}
|
|
Version: ${VERSION}
|
|
Section: utils
|
|
Priority: optional
|
|
Architecture: ${ARCH}
|
|
Installed-Size: ${INSTALLED_SIZE}
|
|
Maintainer: Qt Screen Lock Demo <developer@example.com>
|
|
Depends: dbus, libx11-6, libxext6, libxrender1, libfontconfig1, libfreetype6, libglib2.0-0, libgl1
|
|
Description: Qt Screen Lock Detection Demo Application
|
|
A Qt5-based Linux application that detects Ubuntu system lock/unlock
|
|
events and automatically stops Paint events when the screen is locked.
|
|
.
|
|
Features:
|
|
- Automatic screen lock detection via DBus
|
|
- Automatic paint event control
|
|
- Real-time animation demonstration
|
|
- Support for multiple desktop environments (GNOME, KDE, XFCE)
|
|
- Manual control and status monitoring
|
|
.
|
|
This package includes all required Qt5 libraries and dependencies.
|
|
EOF
|
|
|
|
echo -e "${GREEN}✓ 控制文件创建完成${NC}"
|
|
|
|
# 创建 postinst 脚本(安装后脚本)
|
|
echo -e "${YELLOW}[7/8] 创建安装脚本...${NC}"
|
|
|
|
cat > "${DEB_DIR}/DEBIAN/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 安装完成!"
|
|
echo "可以通过命令 'ScreenLockDetector' 启动,或在应用程序菜单中找到它。"
|
|
|
|
exit 0
|
|
EOF
|
|
|
|
chmod 755 "${DEB_DIR}/DEBIAN/postinst"
|
|
|
|
# 创建 prerm 脚本(卸载前脚本)
|
|
cat > "${DEB_DIR}/DEBIAN/prerm" << 'EOF'
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
exit 0
|
|
EOF
|
|
|
|
chmod 755 "${DEB_DIR}/DEBIAN/prerm"
|
|
|
|
echo -e "${GREEN}✓ 安装脚本创建完成${NC}"
|
|
|
|
# 构建 DEB 包
|
|
echo -e "${YELLOW}[8/8] 构建 DEB 包...${NC}"
|
|
|
|
# 设置正确的权限
|
|
find "${DEB_DIR}" -type d -exec chmod 755 {} \;
|
|
find "${DEB_DIR}/opt/${PACKAGE_NAME}/bin" -type f -exec chmod 755 {} \;
|
|
|
|
DEB_FILE="${PACKAGE_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}.deb"
|
|
|
|
dpkg-deb --build "${DEB_DIR}" "$DEB_FILE"
|
|
|
|
if [ -f "$DEB_FILE" ]; then
|
|
echo -e "${GREEN}========================================${NC}"
|
|
echo -e "${GREEN}✓ DEB 包构建成功!${NC}"
|
|
echo -e "${GREEN}========================================${NC}"
|
|
echo -e "${BLUE}包位置: ${DEB_FILE}${NC}"
|
|
echo -e "${BLUE}包大小: $(du -h "$DEB_FILE" | cut -f1)${NC}"
|
|
echo ""
|
|
echo -e "${YELLOW}安装命令:${NC}"
|
|
echo -e " sudo dpkg -i $DEB_FILE"
|
|
echo ""
|
|
echo -e "${YELLOW}如果有依赖问题,运行:${NC}"
|
|
echo -e " sudo apt-get install -f"
|
|
echo ""
|
|
echo -e "${YELLOW}卸载命令:${NC}"
|
|
echo -e " sudo dpkg -r ${PACKAGE_NAME}"
|
|
echo ""
|
|
echo -e "${YELLOW}查看包信息:${NC}"
|
|
echo -e " dpkg -I $DEB_FILE"
|
|
echo -e " dpkg -c $DEB_FILE"
|
|
echo ""
|
|
else
|
|
echo -e "${RED}错误: DEB 包构建失败${NC}"
|
|
exit 1
|
|
fi
|