#!/bin/bash
# ============================================================================
# Build an InterDime app into a .deb package
# ============================================================================
# Usage: ./build-interdime-app.sh <app-source-dir> <version>
#
# Example: ./build-interdime-app.sh /path/to/interdime-mail 2.0.1
#
# The app source dir should contain:
#   interdime-<name>.py    (entry point)
#   src/                   (Python source)
#   assets/                (icons, etc.)

set -e

APP_DIR="$1"
VERSION="${2:-2.0.0}"
REPO_ROOT="/var/www/repo.interdime.co.uk"

[ -z "$APP_DIR" ] && { echo "Usage: $0 <app-source-dir> [version]"; exit 1; }
[ -d "$APP_DIR" ] || { echo "Error: $APP_DIR not found"; exit 1; }

APP_NAME=$(basename "$APP_DIR")
DEB_NAME="${APP_NAME}_${VERSION}_all"
BUILD_DIR="/tmp/${DEB_NAME}"

echo "Building ${APP_NAME} v${VERSION}..."

# Clean previous build
rm -rf "${BUILD_DIR}"

# Create .deb structure
mkdir -p "${BUILD_DIR}/DEBIAN"
mkdir -p "${BUILD_DIR}/usr/local/share/interdime/apps/${APP_NAME}"
mkdir -p "${BUILD_DIR}/usr/local/bin"
mkdir -p "${BUILD_DIR}/usr/share/applications"
mkdir -p "${BUILD_DIR}/usr/share/pixmaps"

# Copy app files
cp -a "${APP_DIR}/." "${BUILD_DIR}/usr/local/share/interdime/apps/${APP_NAME}/"

# Find the entry point and icon
ENTRY="${APP_DIR}/${APP_NAME}.py"
ICON_NAME=$(ls "${APP_DIR}/assets/"*.svg 2>/dev/null | head -1)
ICON_BASE=$(basename "${ICON_NAME}" .svg 2>/dev/null || echo "${APP_NAME}")

# Create launcher
cat > "${BUILD_DIR}/usr/local/bin/${APP_NAME}" << LAUNCHER
#!/bin/sh
export QT_ENABLE_HIGHDPI_SCALING=1
exec python3 "/usr/local/share/interdime/apps/${APP_NAME}/${APP_NAME}.py" "\$@"
LAUNCHER
chmod +x "${BUILD_DIR}/usr/local/bin/${APP_NAME}"

# Create desktop entry
DISPLAY_NAME=$(echo "$APP_NAME" | sed 's/interdime-/InterDime /; s/\b\(.\)/\u\1/g')
cat > "${BUILD_DIR}/usr/share/applications/${APP_NAME}.desktop" << DESKTOP
[Desktop Entry]
Type=Application
Name=${DISPLAY_NAME}
Exec=${APP_NAME} %U
Icon=${APP_NAME}
Terminal=false
Categories=Utility;
DESKTOP

# Copy icon
if [ -n "${ICON_NAME}" ] && [ -f "${ICON_NAME}" ]; then
    cp "${ICON_NAME}" "${BUILD_DIR}/usr/share/pixmaps/${APP_NAME}.svg"
fi

# Calculate installed size
INSTALLED_SIZE=$(du -sk "${BUILD_DIR}" | cut -f1)

# Create control file
cat > "${BUILD_DIR}/DEBIAN/control" << CONTROL
Package: ${APP_NAME}
Version: ${VERSION}
Section: utils
Priority: optional
Architecture: all
Installed-Size: ${INSTALLED_SIZE}
Depends: python3, python3-pyside6.qtcore, python3-pyside6.qtgui, python3-pyside6.qtwidgets
Maintainer: InterDime Project <dev@interdime.com>
Homepage: https://www.interdime.com
Description: ${DISPLAY_NAME}
 Part of the InterDime OS application suite.
CONTROL

# Build the .deb
dpkg-deb --build "${BUILD_DIR}" "/tmp/${DEB_NAME}.deb"

# Copy to repo pool
cp "/tmp/${DEB_NAME}.deb" "${REPO_ROOT}/pool/main/"
echo "Package built: /tmp/${DEB_NAME}.deb"
echo "Copied to: ${REPO_ROOT}/pool/main/${DEB_NAME}.deb"
echo ""
echo "Run ${REPO_ROOT}/scripts/update-repo.sh to update repository metadata."

# Cleanup
rm -rf "${BUILD_DIR}"
