#!/usr/bin/env bash
set -euo pipefail

VERSION="${VERSION:-1.0.4}"
ARCH="${ARCH:-amd64}"
AGENT_HOST="${AGENT_HOST:-0.0.0.0}"
AGENT_PORT="${AGENT_PORT:-5010}"
ALLOWED_APT_PACKAGES="${ALLOWED_APT_PACKAGES:-*}"
DEB_COMPRESSION="${DEB_COMPRESSION:-gzip}"
PKG_NAME="local-installer-agent"
BUILD_ROOT="${BUILD_ROOT:-build}"
BUILD_DIR="${BUILD_ROOT}/${PKG_NAME}_${VERSION}_${ARCH}"
OUTPUT_PACKAGE="${BUILD_DIR}.deb"

if [[ ! "$VERSION" =~ ^[a-zA-Z0-9][a-zA-Z0-9._:+~=-]*$ ]]; then
  echo "Invalid VERSION: ${VERSION}" >&2
  exit 1
fi

if [[ ! "$ARCH" =~ ^[a-z0-9][a-z0-9._-]*$ ]]; then
  echo "Invalid ARCH: ${ARCH}" >&2
  exit 1
fi

if [[ "$ALLOWED_APT_PACKAGES" != "*" && ! "$ALLOWED_APT_PACKAGES" =~ ^[a-zA-Z0-9._+-]+(,[a-zA-Z0-9._+-]+)*$ ]]; then
  echo "Invalid ALLOWED_APT_PACKAGES: ${ALLOWED_APT_PACKAGES}" >&2
  echo "Use * for any valid APT package or a comma-separated list without spaces." >&2
  exit 1
fi

rm -rf "${BUILD_DIR}"
rm -f "${OUTPUT_PACKAGE}"

mkdir -p "${BUILD_DIR}/opt/local-installer-agent"
mkdir -p "${BUILD_DIR}/etc/local-installer-agent"
mkdir -p "${BUILD_DIR}/etc/systemd/system"
mkdir -p "${BUILD_DIR}/DEBIAN"

cp -r app "${BUILD_DIR}/opt/local-installer-agent/"
cp requirements.txt "${BUILD_DIR}/opt/local-installer-agent/"

find "${BUILD_DIR}/opt/local-installer-agent/app" \
  \( -type d -name "__pycache__" -o -type f \( -name "*.pyc" -o -name "*.pyo" \) \) \
  -exec rm -rf {} +

cp packaging/systemd/local-installer-agent.service \
   "${BUILD_DIR}/etc/systemd/system/local-installer-agent.service"

cp packaging/DEBIAN/control "${BUILD_DIR}/DEBIAN/control"
cp packaging/DEBIAN/postinst "${BUILD_DIR}/DEBIAN/postinst"
cp packaging/DEBIAN/prerm "${BUILD_DIR}/DEBIAN/prerm"
cp packaging/DEBIAN/postrm "${BUILD_DIR}/DEBIAN/postrm"

sed -i \
  -e "s/^Version:.*/Version: ${VERSION}/" \
  -e "s/^Architecture:.*/Architecture: ${ARCH}/" \
  "${BUILD_DIR}/DEBIAN/control"

chmod 755 "${BUILD_DIR}/DEBIAN/postinst"
chmod 755 "${BUILD_DIR}/DEBIAN/prerm"
chmod 755 "${BUILD_DIR}/DEBIAN/postrm"
chmod 755 "${BUILD_DIR}/DEBIAN"

cat > "${BUILD_DIR}/etc/local-installer-agent/agent.env" <<EOF
AGENT_VERSION=${VERSION}
AGENT_HOST=${AGENT_HOST}
AGENT_PORT=${AGENT_PORT}
ROBOT_PACKAGE_BASE_URL=https://package.pnkr.cloud
ALLOWED_ORIGINS=https://app.pnkr.cloud,https://package.pnkr.cloud,http://localhost:3000,http://127.0.0.1:3000,http://localhost:5173,http://127.0.0.1:5173,http://localhost:8080,http://127.0.0.1:8080
ALLOWED_DOWNLOAD_HOSTS=package.pnkr.cloud
ALLOWED_APT_PACKAGES=${ALLOWED_APT_PACKAGES}
ALLOWED_DOCKER_REGISTRIES=registry.robot.package,docker.io
CACHE_DIR=/var/cache/local-installer-agent/packages
APP_DIR=/opt/robot-apps
LOG_DIR=/var/log/local-installer-agent
DB_PATH=/var/lib/local-installer-agent/agent.db
ALLOW_REMOVE=true
ALLOW_PURGE=true
ALLOW_DOCKER=true
ALLOW_DOCKER_COMPOSE=false
AUTO_INSTALL_DOCKER=true
EOF

dpkg-deb -Z"${DEB_COMPRESSION}" --root-owner-group --build "${BUILD_DIR}"

echo "Built package:"
echo "${OUTPUT_PACKAGE}"

if [ -n "${PUBLISH_DIR:-}" ]; then
  mkdir -p "${PUBLISH_DIR}"
  cp "${OUTPUT_PACKAGE}" "${PUBLISH_DIR}/"
  echo "Published package:"
  echo "${PUBLISH_DIR}/$(basename "${OUTPUT_PACKAGE}")"
fi
