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

cd "$(dirname "$0")"

VERSION=$(python3 - <<'PY'
from version import APP_VERSION
print(APP_VERSION)
PY
)
APP_NAME="GEO-SOP"
BUILD_DIR="build"
DIST_DIR="dist"
PACKAGE_DIR="release"
ICON_PNG="web_app/static/img/geo-sop-icon.png"
ICON_ICNS="${BUILD_DIR}/geo-sop-icon.icns"

if [ ! -d ".venv-build" ]; then
  python3 -m venv .venv-build
fi

source .venv-build/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements-desktop.txt
python -m pip install pyinstaller

rm -rf "${BUILD_DIR}" "${DIST_DIR}"
mkdir -p "${BUILD_DIR}" "${PACKAGE_DIR}"

if [ -f "${ICON_PNG}" ]; then
  ICONSET="${BUILD_DIR}/geo-sop.iconset"
  mkdir -p "${ICONSET}"
  for size in 16 32 64 128 256 512; do
    sips -z "${size}" "${size}" "${ICON_PNG}" --out "${ICONSET}/icon_${size}x${size}.png" >/dev/null
  done
  sips -z 32 32 "${ICON_PNG}" --out "${ICONSET}/icon_16x16@2x.png" >/dev/null
  sips -z 64 64 "${ICON_PNG}" --out "${ICONSET}/icon_32x32@2x.png" >/dev/null
  sips -z 256 256 "${ICON_PNG}" --out "${ICONSET}/icon_128x128@2x.png" >/dev/null
  sips -z 512 512 "${ICON_PNG}" --out "${ICONSET}/icon_256x256@2x.png" >/dev/null
  sips -z 1024 1024 "${ICON_PNG}" --out "${ICONSET}/icon_512x512@2x.png" >/dev/null
  iconutil -c icns "${ICONSET}" -o "${ICON_ICNS}"
fi

PYINSTALLER_ARGS=(
  --noconfirm
  --windowed
  --name "${APP_NAME}"
  --add-data "web_app:web_app"
  --add-data "platforms:platforms"
  --add-data "reference_sentiment:reference_sentiment"
  --add-data "tools:tools"
  --add-data "version.py:."
  --hidden-import flask
  --hidden-import flask_login
  --hidden-import flask_sqlalchemy
  --hidden-import flask_cors
  --hidden-import playwright
  --hidden-import openpyxl
  --hidden-import PIL
  --hidden-import local_paths
  --hidden-import profile_utils
  --hidden-import browser_config
  --hidden-import browser_utils
  --hidden-import config
  --hidden-import utils
  --collect-submodules playwright
  --collect-submodules apscheduler
)

if [ -f "${ICON_ICNS}" ]; then
  PYINSTALLER_ARGS+=(--icon "${ICON_ICNS}")
fi

pyinstaller "${PYINSTALLER_ARGS[@]}" desktop_app.py

PLIST_PATH="${DIST_DIR}/${APP_NAME}.app/Contents/Info.plist"
if [ -f "${PLIST_PATH}" ]; then
  plutil -replace CFBundleShortVersionString -string "${VERSION}" "${PLIST_PATH}"
  plutil -replace CFBundleVersion -string "$(python3 - <<'PY'
from version import BUILD_NUMBER
print(BUILD_NUMBER)
PY
)" "${PLIST_PATH}"
  plutil -replace CFBundleIdentifier -string "com.tukemarketing.geosop" "${PLIST_PATH}"
  codesign --force --deep --sign - "${DIST_DIR}/${APP_NAME}.app"
fi

DMG_PATH="${PACKAGE_DIR}/${APP_NAME}-v${VERSION}-macOS.dmg"
rm -f "${DMG_PATH}"
hdiutil create \
  -volname "${APP_NAME} v${VERSION}" \
  -srcfolder "${DIST_DIR}/${APP_NAME}.app" \
  -ov \
  -format UDZO \
  "${DMG_PATH}"

shasum -a 256 "${DMG_PATH}" > "${DMG_PATH}.sha256"
echo "Built ${DMG_PATH}"
