# This workflow will build a release-like distribution when manually dispatched

name: Build

on:
  push:
    paths:
      - '.github/workflows/build.yml'
      - 'setup.py'
      - 'requirements.txt'
      - '*.iss'
  pull_request:
    paths:
      - '.github/workflows/build.yml'
      - 'setup.py'
      - 'requirements.txt'
      - '*.iss'
  workflow_dispatch:

env:
  ENEMIZER_VERSION: 7.1
  APPIMAGETOOL_VERSION: 13

jobs:
  # build-release-macos: # LF volunteer

  build-win-py38: # RCs will still be built and signed by hand
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install python
        uses: actions/setup-python@v5
        with:
          python-version: '3.8'
      - name: Download run-time dependencies
        run: |
          Invoke-WebRequest -Uri https://github.com/Ijwu/Enemizer/releases/download/${Env:ENEMIZER_VERSION}/win-x64.zip -OutFile enemizer.zip
          Expand-Archive -Path enemizer.zip -DestinationPath EnemizerCLI -Force
          choco install innosetup --version=6.2.2 --allow-downgrade
      - name: Build
        run: |
          python -m pip install --upgrade pip
          python setup.py build_exe --yes
          if ( $? -eq $false ) {
            Write-Error "setup.py failed!"
            exit 1
          }
          $NAME="$(ls build | Select-String -Pattern 'exe')".Split('.',2)[1]
          $ZIP_NAME="Archipelago_$NAME.7z"
          echo "$NAME -> $ZIP_NAME"
          echo "ZIP_NAME=$ZIP_NAME" >> $Env:GITHUB_ENV
          New-Item -Path dist -ItemType Directory -Force
          cd build
          Rename-Item "exe.$NAME" Archipelago
          7z a -mx=9 -mhe=on -ms "../dist/$ZIP_NAME" Archipelago
          Rename-Item Archipelago "exe.$NAME"  # inno_setup.iss expects the original name
      - name: Build Setup
        run: |
          & "${env:ProgramFiles(x86)}\Inno Setup 6\iscc.exe" inno_setup.iss /DNO_SIGNTOOL
          if ( $? -eq $false ) {
            Write-Error "Building setup failed!"
            exit 1
          }
          $contents = Get-ChildItem -Path setups/*.exe -Force -Recurse
          $SETUP_NAME=$contents[0].Name
          echo "SETUP_NAME=$SETUP_NAME" >> $Env:GITHUB_ENV
      - name: Check build loads expected worlds
        shell: bash
        run: |
          cd build/exe*
          mv Players/Templates/meta.yaml .
          ls -1 Players/Templates | sort > setup-player-templates.txt
          rm -R Players/Templates
          timeout 30 ./ArchipelagoLauncher "Generate Template Options" || true
          ls -1 Players/Templates | sort > generated-player-templates.txt
          cmp setup-player-templates.txt generated-player-templates.txt \
            || diff setup-player-templates.txt generated-player-templates.txt
          mv meta.yaml Players/Templates/
      - name: Test Generate
        shell: bash
        run: |
          cd build/exe*
          cp Players/Templates/Clique.yaml Players/
          timeout 30 ./ArchipelagoGenerate
      - name: Store 7z
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.ZIP_NAME }}
          path: dist/${{ env.ZIP_NAME }}
          compression-level: 0  # .7z is incompressible by zip
          if-no-files-found: error
          retention-days: 7  # keep for 7 days, should be enough
      - name: Store Setup
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.SETUP_NAME }}
          path: setups/${{ env.SETUP_NAME }}
          if-no-files-found: error
          retention-days: 7  # keep for 7 days, should be enough

  build-ubuntu2004:
    runs-on: ubuntu-20.04
    steps:
      # - copy code below to release.yml -
      - uses: actions/checkout@v4
      - name: Install base dependencies
        run: |
          sudo apt update
          sudo apt -y install build-essential p7zip xz-utils wget libglib2.0-0
          sudo apt -y install python3-gi libgirepository1.0-dev  # should pull dependencies for gi installation below
      - name: Get a recent python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Install build-time dependencies
        run: |
          echo "PYTHON=python3.11" >> $GITHUB_ENV
          wget -nv https://github.com/AppImage/AppImageKit/releases/download/$APPIMAGETOOL_VERSION/appimagetool-x86_64.AppImage
          chmod a+rx appimagetool-x86_64.AppImage
          ./appimagetool-x86_64.AppImage --appimage-extract
          echo -e '#/bin/sh\n./squashfs-root/AppRun "$@"' > appimagetool
          chmod a+rx appimagetool
      - name: Download run-time dependencies
        run: |
          wget -nv https://github.com/Ijwu/Enemizer/releases/download/$ENEMIZER_VERSION/ubuntu.16.04-x64.7z
          7za x -oEnemizerCLI/ ubuntu.16.04-x64.7z
      - name: Build
        run: |
          # pygobject is an optional dependency for kivy that's not in requirements
          # charset-normalizer was somehow incomplete in the github runner
          "${{ env.PYTHON }}" -m venv venv
          source venv/bin/activate
          "${{ env.PYTHON }}" -m pip install --upgrade pip PyGObject charset-normalizer
          python setup.py build_exe --yes bdist_appimage --yes
          echo -e "setup.py build output:\n `ls build`"
          echo -e "setup.py dist output:\n `ls dist`"
          cd dist && export APPIMAGE_NAME="`ls *.AppImage`" && cd ..
          export TAR_NAME="${APPIMAGE_NAME%.AppImage}.tar.gz"
          (cd build && DIR_NAME="`ls | grep exe`" && mv "$DIR_NAME" Archipelago && tar -cv Archipelago | gzip -8 > ../dist/$TAR_NAME && mv Archipelago "$DIR_NAME")
          echo "APPIMAGE_NAME=$APPIMAGE_NAME" >> $GITHUB_ENV
          echo "TAR_NAME=$TAR_NAME" >> $GITHUB_ENV
      # - copy code above to release.yml -
      - name: Build Again
        run: |
          source venv/bin/activate
          python setup.py build_exe --yes
      - name: Check build loads expected worlds
        shell: bash
        run: |
          cd build/exe*
          mv Players/Templates/meta.yaml .
          ls -1 Players/Templates | sort > setup-player-templates.txt
          rm -R Players/Templates
          timeout 30 ./ArchipelagoLauncher "Generate Template Options" || true
          ls -1 Players/Templates | sort > generated-player-templates.txt
          cmp setup-player-templates.txt generated-player-templates.txt \
            || diff setup-player-templates.txt generated-player-templates.txt
          mv meta.yaml Players/Templates/
      - name: Test Generate
        shell: bash
        run: |
          cd build/exe*
          cp Players/Templates/Clique.yaml Players/
          timeout 30 ./ArchipelagoGenerate
      - name: Store AppImage
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.APPIMAGE_NAME }}
          path: dist/${{ env.APPIMAGE_NAME }}
          if-no-files-found: error
          retention-days: 7
      - name: Store .tar.gz
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.TAR_NAME }}
          path: dist/${{ env.TAR_NAME }}
          compression-level: 0  # .gz is incompressible by zip
          if-no-files-found: error
          retention-days: 7
