#!/bin/sh
set -e

# ===== Проверка аргументов =====
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 /path/to/LicenseManager"
    exit 1
fi

BIN_PATH="$(realpath "$1")"

if [ ! -f "$BIN_PATH" ]; then
    echo "Error: binary not found: $BIN_PATH"
    exit 1
fi

# ===== Параметры пакета =====
APP_NAME="licensemanager"
BIN_NAME="LicenseManager"
VERSION="0.1"
ARCH="$(dpkg --print-architecture)"

PKG_DIR="${APP_NAME}_${VERSION}_${ARCH}"

# ===== Сборка =====
echo "==> Cleaning"
rm -rf "$PKG_DIR" *.deb

echo "==> Creating package structure"
mkdir -p "$PKG_DIR/DEBIAN"
mkdir -p "$PKG_DIR/usr/bin"
mkdir -p "$PKG_DIR/usr/share/applications"
mkdir -p "$PKG_DIR/usr/share/icons/hicolor/256x256/apps"

echo "==> Copy control file"
cp control "$PKG_DIR/DEBIAN/control"

echo "==> Copy binary"
cp "$BIN_PATH" "$PKG_DIR/usr/bin/$BIN_NAME"
chmod 0755 "$PKG_DIR/usr/bin/$BIN_NAME"

echo "==> Copy desktop file"
cp LicenseManager.desktop \
   "$PKG_DIR/usr/share/applications/LicenseManager.desktop"

echo "==> Copy icon"
cp icon.png \
   "$PKG_DIR/usr/share/icons/hicolor/256x256/apps/licensemanager.png"

echo "==> Building deb package"
dpkg-deb --build "$PKG_DIR"

echo "==> Done:"
ls -lh *.deb

