#!/usr/bin/env python3
"""
解包DEB脚本 - 使用dpkg解包，通过系统通知提示结果。
用法: extractdeb <DEB文件路径>
"""

import sys
import os
import subprocess


def notify(title, message, icon="emblem-checked"):
    subprocess.run([
        "notify-send",
        "--app-name=解包DEB",
        title,
        message,
        f"--icon={icon}"
    ])


def main():
    try:
        if len(sys.argv) < 2:
            notify("", "失败", "dialog-error")
            sys.exit(1)

        name = sys.argv[1]
        if name.startswith("file://"):
            name = name[7:]

        if os.path.isdir(name):
            sys.exit(0)

        dest_dir = name.rsplit(".", 1)[0] if "." in os.path.basename(name) else name

        subprocess.run(["dpkg", "-x", name, dest_dir], check=True)
        subprocess.run(["dpkg", "-e", name, os.path.join(dest_dir, "DEBIAN")], check=True)

        notify("", "成功", "emblem-checked")

    except Exception:
        notify("", "失败", "dialog-error")
        sys.exit(1)


if __name__ == "__main__":
    main()
