#!/usr/bin/env bash # -*- coding: utf-8 -*- # ============================================================================= # The Goth Cardgame Collection - One-File Build Script (Linux / ChromeOS / macOS) # Output: dist//TheGothCardgameCollection # Usage: ./build_scripts/build.sh [--current | --all | --clean] # ============================================================================= set -e SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")" PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${PROJECT_ROOT}" APP_NAME="TheGothCardgameCollection" MAIN_FILE="main.py" RES_DIR="resources" GAMES_DIR="games" ICON_FILE="resources/images/game_icon.ico" MODE="${1:---current}" if [ "$MODE" = "--clean" ]; then echo "🧹 Räume temporäre Build-Ordner auf..." rm -rf build .venv_build_* *.spec exit 0 fi UNAME_OUT="$(uname -s)" case "${UNAME_OUT}" in Linux*) OS_NAME="linux" ;; Darwin*) OS_NAME="macos" ;; *) echo "❌ Unbekanntes Unix-System: ${UNAME_OUT}"; exit 1 ;; esac # ---------------------------------------------------- # 0. ROOT / SUDO RECHTE-CHECK FÜR SYSTEM-INSTALLATIONEN # ---------------------------------------------------- NEEDS_SYS_INSTALL=0 if ! command -v python3 &>/dev/null || ! python3 -c "import tkinter" &>/dev/null; then NEEDS_SYS_INSTALL=1 fi if [ "$MODE" = "--all" ] && [ "$OS_NAME" = "linux" ] && ! command -v wine &>/dev/null; then NEEDS_SYS_INSTALL=1 fi if [ "$OS_NAME" = "linux" ] && [ "$NEEDS_SYS_INSTALL" -eq 1 ]; then if [ "${EUID:-$(id -u)}" -ne 0 ]; then echo "🔒 Es fehlen Systempakete (z.B. python3-tk oder wine), für deren Installation Root-Rechte nötig sind." if command -v sudo &>/dev/null; then echo "🔑 Fordere Root-Rechte via sudo an und starte Skript neu..." exec sudo -E bash "$SCRIPT_PATH" "$@" else echo "❌ FEHLER: Keine Root-Rechte (EUID != 0) und 'sudo' ist nicht installiert!" echo " Bitte führe das Skript als root aus: su -c '$SCRIPT_PATH $MODE'" exit 1 fi fi fi # Echten User ermitteln (wichtig, falls Skript via sudo/root läuft, damit Wine & Dateirechte stimmen!) if [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "${SUDO_USER:-}" ]; then REAL_USER="$SUDO_USER" REAL_HOME="$(getent passwd "$SUDO_USER" | cut -d: -f6)" WINE_RUNNER=(sudo -u "$REAL_USER" HOME="$REAL_HOME") else REAL_USER="$(whoami)" REAL_HOME="$HOME" WINE_RUNNER=() fi echo "====================================================" echo " 🦇 Starte Build für $APP_NAME (OS: $OS_NAME | Modus: $MODE)" echo "====================================================" # ---------------------------------------------------- # SYSTEMPAKETE INSTALLIEREN (läuft garantiert als root!) # ---------------------------------------------------- install_linux_pkgs() { if command -v apt-get &>/dev/null; then apt-get update -qq apt-get install -y "$@" elif command -v dnf &>/dev/null; then dnf install -y "$@" elif command -v yum &>/dev/null; then yum install -y "$@" elif command -v zypper &>/dev/null; then zypper --non-interactive install "$@" elif command -v pacman &>/dev/null; then pacman -Sy --noconfirm --needed "$@" else echo "❌ Kein unterstützter Paketmanager (apt/dnf/yum/zypper/pacman) gefunden!" exit 1 fi } if [ "$OS_NAME" = "linux" ] && { ! command -v python3 &>/dev/null || ! python3 -c "import tkinter" &>/dev/null; }; then echo "⚙️ Installiere fehlende Python-/Tkinter-Systempakete..." if command -v apt-get &>/dev/null; then install_linux_pkgs python3 python3-pip python3-venv python3-tk wget elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then install_linux_pkgs python3 python3-pip python3-tkinter wget elif command -v zypper &>/dev/null; then install_linux_pkgs python3 python3-pip python3-tk wget elif command -v pacman &>/dev/null; then install_linux_pkgs python python-pip tk wget fi elif [ "$OS_NAME" = "macos" ] && { ! command -v python3 &>/dev/null || ! python3 -c "import tkinter" &>/dev/null; }; then if [ "${EUID:-$(id -u)}" -eq 0 ]; then echo "❌ Unter macOS darf Homebrew nicht als root ausgeführt werden. Bitte ohne sudo starten." exit 1 fi if command -v brew &>/dev/null; then brew install python@3.11 python-tk@3.11 else echo "❌ Python3/Tkinter fehlt und Homebrew ist nicht installiert." exit 1 fi fi # ---------------------------------------------------- # TEIL 1: LINUX / MACOS EXECUTABLE BAUEN # ---------------------------------------------------- echo "" echo "---> [1] Baue ${OS_NAME} Executable..." # Defekte Venvs automatisch bereinigen for VDIR in ".venv_build_${OS_NAME}" ".venv" "venv"; do if [ -d "$VDIR" ] && [ ! -x "$VDIR/bin/python3" ]; then echo "🧹 Entferne defektes Venv: $VDIR" rm -rf "$VDIR" fi done if [ -x ".venv/bin/python3" ]; then LINUX_PY=".venv/bin/python3" elif [ -x "venv/bin/python3" ]; then LINUX_PY="venv/bin/python3" else LINUX_PY="python3" fi # Falls wir nicht root sind und kein Venv nutzen, pip mit --user ausführen, damit es keine Permission-Errors gibt PIP_USER_FLAG=() if [ "$LINUX_PY" = "python3" ] && [ "${EUID:-$(id -u)}" -ne 0 ]; then PIP_USER_FLAG=(--user) fi $LINUX_PY -m pip install "${PIP_USER_FLAG[@]}" --upgrade pillow pygame pyinstaller --break-system-packages 2>/dev/null \ || $LINUX_PY -m pip install "${PIP_USER_FLAG[@]}" --upgrade pillow pygame pyinstaller ICON_ARG=() [ -f "$ICON_FILE" ] && ICON_ARG=(--icon "$ICON_FILE") $LINUX_PY -m PyInstaller \ --noconfirm \ --noconsole \ --onefile \ --clean \ --distpath "dist/${OS_NAME}" \ --workpath "build/${OS_NAME}" \ --specpath "build/${OS_NAME}" \ "${ICON_ARG[@]}" \ --add-data "${RES_DIR}:${RES_DIR}" \ --add-data "${GAMES_DIR}:${GAMES_DIR}" \ --collect-submodules games \ --collect-submodules controller \ --collect-submodules models \ --collect-submodules view \ --collect-submodules resources \ --hidden-import tkinter \ --hidden-import tkinter.ttk \ --hidden-import tkinter.font \ --hidden-import sqlite3 \ --hidden-import PIL._tkinter_finder \ --name "${APP_NAME}" \ "$MAIN_FILE" chmod +x "dist/${OS_NAME}/${APP_NAME}" rm -rf "build/${OS_NAME}" echo "✔ ${OS_NAME}-Build fertig: dist/${OS_NAME}/${APP_NAME}" # ---------------------------------------------------- # TEIL 2: WINDOWS .EXE BAUEN VIA WINE (nur bei --all unter Linux) # ---------------------------------------------------- if [ "$MODE" = "--all" ] && [ "$OS_NAME" = "linux" ]; then echo "" echo "---> [2] Baue Windows .exe (via Wine)..." if ! command -v wine &>/dev/null; then echo "⚙️ Installiere 'wine' und 'wget' (Root-Rechte bereits geprüft)..." if command -v apt-get &>/dev/null; then dpkg --add-architecture amd64 2>/dev/null || true install_linux_pkgs wine wget else install_linux_pkgs wine wget fi fi if ! command -v wine &>/dev/null; then echo "❌ 'wine' konnte nicht installiert werden!" exit 1 fi WINE_DRIVE_C="${REAL_HOME}/.wine/drive_c" WINE_PY=$(find "$WINE_DRIVE_C" -iname "python.exe" 2>/dev/null | grep -i "Python3" | head -n 1 || true) if [ -z "$WINE_PY" ]; then echo "⚙️ Windows-Python in Wine (${WINE_DRIVE_C}) nicht gefunden. Installiere Python 3.11.9 automatisch..." INSTALLER="/tmp/python-3.11.9-amd64.exe" if [ ! -f "$INSTALLER" ]; then wget -O "$INSTALLER" https://www.python.org/ftp/python/3.11.9/python-3.11.9-amd64.exe fi "${WINE_RUNNER[@]}" wine "$INSTALLER" /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 "${WINE_RUNNER[@]}" wineserver -w || true rm -f "$INSTALLER" WINE_PY=$(find "$WINE_DRIVE_C" -iname "python.exe" 2>/dev/null | grep -i "Python3" | head -n 1 || true) fi if [ -z "$WINE_PY" ]; then echo "❌ Konnte python.exe in ${WINE_DRIVE_C} nicht finden!" exit 1 fi echo "✔ Nutze Wine-Python: $WINE_PY" "${WINE_RUNNER[@]}" wine "$WINE_PY" -m pip install --upgrade pip pillow pygame pyinstaller "${WINE_RUNNER[@]}" wine "$WINE_PY" -m PyInstaller \ --noconfirm \ --noconsole \ --onefile \ --clean \ --distpath "dist/windows" \ --workpath "build/windows" \ --specpath "build/windows" \ "${ICON_ARG[@]}" \ --add-data "${RES_DIR};${RES_DIR}" \ --add-data "${GAMES_DIR};${GAMES_DIR}" \ --collect-submodules games \ --collect-submodules controller \ --collect-submodules models \ --collect-submodules view \ --collect-submodules resources \ --hidden-import tkinter \ --hidden-import tkinter.ttk \ --hidden-import tkinter.font \ --hidden-import sqlite3 \ --hidden-import PIL._tkinter_finder \ --name "${APP_NAME}" \ "$MAIN_FILE" rm -rf "build/windows" echo "✔ Windows-Build fertig: dist/windows/${APP_NAME}.exe" fi # Falls via sudo gestartet, Besitzrechte von dist/ wieder auf den normalen User übertragen if [ "${EUID:-$(id -u)}" -eq 0 ] && [ -n "${SUDO_USER:-}" ]; then chown -R "$SUDO_USER:$SUDO_USER" dist/ 2>/dev/null || true fi echo "" echo "====================================================" echo " ✅ BUILD ERFOLGREICH ABGESCHLOSSEN!" [ -f "dist/${OS_NAME}/${APP_NAME}" ] && echo " 🐧 ${OS_NAME}-Version: dist/${OS_NAME}/${APP_NAME}" [ -f "dist/windows/${APP_NAME}.exe" ] && echo " 🪟 Windows-Version: dist/windows/${APP_NAME}.exe" echo "===================================================="