#!/usr/bin/env bash
# Builds the standalone Mercator and Post-Atlas from Mercator.spec and Post-Atlas.spec (build.md).
#
# The libraries the build needs go into a Python environment of the build's own, the folder .build-venv beside this
# script, never into the system's Python. Debian, Ubuntu, Fedora and others refuse a plain "pip install" there
# ("externally-managed-environment", PEP 668), and until 1.3.0 this script went on after that refusal, found no
# pyinstaller, and still said "Success!". Now the first step that fails stops the script and says so, and "Success!" is
# printed only once both programs are found in dist (check_built).
set -e
cd "$(dirname "$0")"

echo atlas-tools build script
echo build script version 1.4.0
echo ===============================================

# The Python to build with: python3 where there is one (Linux, macOS), else python (Git Bash on Windows).
PYTHON=""
for candidate in python3 python; do
    if command -v "$candidate" >/dev/null 2>&1; then
        PYTHON="$candidate"
        break
    fi
done
if [ -z "$PYTHON" ]; then
    echo "No Python found: install Python 3.9 or newer first."
    exit 1
fi

# On Linux PyInstaller needs objdump, which is in the package binutils and not on every system: said now, before
# anything is downloaded, not after (found building in a plain Ubuntu 24.04, 2026-11-16).
if [ "$(uname -s)" = "Linux" ] && ! command -v objdump >/dev/null 2>&1; then
    echo "The build needs 'objdump', which this system does not have. It is in the package binutils:"
    echo "On Debian, Ubuntu and Mint:   sudo apt install binutils"
    echo "On Fedora:                    sudo dnf install binutils"
    echo "On Arch:                      sudo pacman -S binutils"
    echo "Then run this script again."
    exit 1
fi

# ... and the shared library of Python itself, which Debian's and Ubuntu's python3 comes without (same finding).
if [ "$(uname -s)" = "Linux" ] && ! "$PYTHON" -c '
import ctypes, sys
ctypes.CDLL("libpython%d.%d.so.1.0" % sys.version_info[:2])
' >/dev/null 2>&1; then
    VERSION="$("$PYTHON" -c 'import sys; print("%d.%d" % sys.version_info[:2])')"
    echo "The build needs the shared library of Python $VERSION (libpython$VERSION.so), which this system does not have."
    echo "On Debian, Ubuntu and Mint:   sudo apt install libpython$VERSION"
    echo "Then run this script again."
    exit 1
fi

VENV=.build-venv
if [ ! -d "$VENV" ]; then
    echo "Making the build's own Python environment in $VENV ..."
    if ! "$PYTHON" -m venv "$VENV"; then
        rm -rf "$VENV"
        echo
        echo "The environment could not be made - most likely this Python has no 'venv' module."
        echo "On Debian, Ubuntu and Mint install it with:   sudo apt install python3-venv"
        echo "On Fedora it comes with python3; on Arch with python."
        echo "Then run this script again."
        exit 1
    fi
fi
# The environment's own Python: bin/ on Linux and macOS, Scripts/ on Windows.
if [ -x "$VENV/bin/python" ]; then
    VENV_PYTHON="$VENV/bin/python"
else
    VENV_PYTHON="$VENV/Scripts/python.exe"
fi

# After a build: the program must really be there - its own file, and beside it the folder of what it needs. PyInstaller
# ending without an error is not taken as proof (since 1.4.0).
check_built() {
    local name="$1" folder="dist/$1" program=""
    for candidate in "$folder/$name" "$folder/$name.exe"; do
        if [ -f "$candidate" ] && [ -s "$candidate" ]; then
            program="$candidate"
        fi
    done
    if [ -z "$program" ]; then
        echo "The build of $name ended without an error, but $folder/$name is not there (or is empty)."
        exit 1
    fi
    if [ ! -d "$folder/_internal" ] || [ -z "$(ls -A "$folder/_internal" 2>/dev/null)" ]; then
        echo "The build of $name ended without an error, but $folder/_internal - the files $name needs - is missing or empty."
        exit 1
    fi
    echo "Built: $program"
}

echo Collecting libs...
"$VENV_PYTHON" -m pip install --upgrade pip
"$VENV_PYTHON" -m pip install pyinstaller pyqt6 scipy Pillow pyqtgraph PyOpenGL
echo ===============================================
echo Building Mercator...
"$VENV_PYTHON" -m PyInstaller Mercator.spec
check_built Mercator
echo Done
echo ===============================================
echo Building Post-Atlas...
"$VENV_PYTHON" -m PyInstaller Post-Atlas.spec
check_built Post-Atlas
echo Done
echo ===============================================
echo 'Success! The executables can be found under the "dist" folder'
