#!/bin/bash
if [ "$1" != ""  ];then
if [[ $1 == file://* ]]; then
    # 如果是，移除 'file://' 部分并输出结果
    opt="${1#file://}"
else
    # 如果不是，直接输出原始路径
    opt=$1
fi

else
opt=`pwd`
fi
notify-send `basename $opt`开始打包 -i /usr/share/icons/uos-packaging-tools.png

echo "正在检查权限: $opt"
# 增强权限检查逻辑
non_755_files=$(find "$opt" -type f -not -perm 755 -o -type d -not -perm 755 2>/dev/null | wc -l)
if [ $non_755_files -gt 0 ]; then
    echo "发现$non_755_files个文件/目录权限不是755，正在修改..."
    if chmod 755 -R "$opt" 2>/tmp/chmod_error.log; then
        echo "权限修改成功"
    else
        echo "权限修改失败"
    fi
else
    echo "所有文件权限已经是755，无需修改"
fi
# 新增Installed-Size处理逻辑
installed_size=$(du -sk "$opt" | awk '{print $1}')
control_file="$opt/DEBIAN/control"
if [ -f "$control_file" ]; then
    if grep -q "Installed-Size:" "$control_file"; then
        sed -i "s/Installed-Size:.*/Installed-Size: $installed_size/" "$control_file"
    else
        # 判断文件行数是否大于等于3
        if [ $(wc -l < "$control_file") -ge 3 ]; then
            sed -i "3iInstalled-Size: $installed_size" "$control_file"
        else
            sed -i "1iInstalled-Size: $installed_size" "$control_file"
        fi
    fi
    echo "已更新Installed-Size: ${installed_size}KB"
else
    echo "警告: 未找到DEBIAN/control文件"
fi

fakeroot dpkg-deb -Z xz  -b $opt $opt/..
notify-send `basename $opt`打包完成 -i /usr/share/icons/uos-packaging-tools.png
exit
