name: snapshots-windows

on: [workflow_call]

jobs:
  snapshots-windows:
    runs-on: windows-latest
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - name: Activity check
        shell: bash
        run: |
          DATEUTC=$((curl -sL https://api.github.com/repos/exult/exult/commits | jq -r '[.[]][0]' | jq -r '.commit.committer.date') || date --utc -Iminutes)
          TIMESTAMP=$(date --utc -d "$DATEUTC" +%s)
          DAYS=$(( ($(date --utc -d "0:00" +%s) - $TIMESTAMP) / 86400 ))
          if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
            echo EXULT_REPO_ALIVE=true >> $GITHUB_ENV
            echo event is workflow_dispatch. Force building snapshot not doing activity check
          else
            if [ $DAYS -gt 0 ]; then
              echo EXULT_REPO_ALIVE=false >> $GITHUB_ENV
              echo "No activity in 24 hours before midnight UTC not building snapshot. Last activity at $DATEUTC."
              mkdir -p windows-snapshot
              echo "No activity" >> windows-snapshot/Windows-not-built
            else
              echo EXULT_REPO_ALIVE=true >> $GITHUB_ENV
            fi
          fi
      - name: Install dependencies
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        uses: msys2/setup-msys2@v2
        with:
          msystem: MSYS
          update: true
          install: |
            base-devel git p7zip zip
            mingw-w64-clang-x86_64-toolchain mingw-w64-clang-x86_64-binutils mingw-w64-clang-x86_64-ntldd mingw-w64-clang-x86_64-SDL2
            mingw-w64-clang-x86_64-fluidsynth mingw-w64-clang-x86_64-libtimidity mingw-w64-clang-x86_64-libogg mingw-w64-clang-x86_64-libvorbis
            mingw-w64-clang-x86_64-munt-mt32emu mingw-w64-clang-x86_64-libpng mingw-w64-clang-x86_64-zlib mingw-w64-clang-x86_64-SDL2_image
            mingw-w64-clang-x86_64-gtk3 mingw-w64-clang-x86_64-adwaita-icon-theme mingw-w64-clang-x86_64-libxml2 mingw-w64-clang-x86_64-freetype
            mingw-w64-clang-x86_64-gimp mingw-w64-clang-x86_64-icu
            mingw-w64-i686-toolchain mingw-w64-i686-binutils mingw-w64-i686-ntldd mingw-w64-i686-SDL2
            mingw-w64-i686-fluidsynth mingw-w64-i686-libtimidity mingw-w64-i686-libogg mingw-w64-i686-libvorbis
            mingw-w64-i686-libpng mingw-w64-i686-zlib 
            mingw-w64-i686-gtk3 mingw-w64-i686-adwaita-icon-theme mingw-w64-i686-libxml2 mingw-w64-i686-freetype
            mingw-w64-i686-gimp mingw-w64-i686-autotools mingw-w64-i686-cmake 
      - name: Checkout munt
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        uses: actions/checkout@v4
        with:
          repository: munt/munt
          ref: libmt32emu_2_7_2
          path: libmt32emu
      - name: Install mingw32/mt32emu
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        env:
          MSYSTEM: MINGW32
        run: |
          cd $GITHUB_WORKSPACE
          mkdir -p munt
          cd munt
          cmake --version
          cmake ./../libmt32emu -Dmunt_WITH_MT32EMU_SMF2WAV=FALSE -Dmunt_WITH_MT32EMU_QT=FALSE -DCMAKE_INSTALL_PREFIX=$MINGW_PREFIX
          ninja install
      - name: Download and manually install SDL2_image-devel-2.8.8-mingw.tar.gz for mingw32 i686
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        env:
          MSYSTEM: MINGW32
          ARCH: i686
        run: |
         curl -L https://github.com/libsdl-org/SDL_image/releases/download/release-2.8.8/SDL2_image-devel-2.8.8-mingw.tar.gz -o SDL2_image-devel-2.8.8-mingw.tar.gz
         echo Checking sha256 hash 
         echo 67446b12f14e4667b76a5721c91e419363e65b74b66cde8057edb29add90cd42 *SDL2_image-devel-2.8.8-mingw.tar.gz | sha256sum.exe -c
         tar xfz SDL2_image-devel-2.8.8-mingw.tar.gz
         cp -r -v SDL2_image-2.8.8/i686-w64-mingw32/* $MINGW_PREFIX
         rm -r -f SDL2_image-2.8.8 SDL2_image-devel-2.8.8-mingw.tar.gz
      - name: Download, build and install icu4c 77.1 for mingw32 i686
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        env:
          MSYSTEM: MINGW32
          ARCH: i686
        run: |
         curl -L https://github.com/unicode-org/icu/releases/download/release-77-1/icu4c-77_1-src.tgz -o icu4c-77_1-src.tgz
         echo Checking sha256 hash 
         echo 588e431f77327c39031ffbb8843c0e3bc122c211374485fa87dc5f3faff24061 *icu4c-77_1-src.tgz | sha256sum.exe -c
         tar xfz icu4c-77_1-src.tgz
         cd icu/source 
         autoreconf -vfi
         ./configure \
         --enable-shared \
         --with-data-packaging=dll \
         --disable-tests \
         --disable-samples \
         --enable-release
         make -j
         make -j install 
         cd ../..
         rm -r -f icu4c-77_1-src.tgz icu 
      - name: Checkout code for i686 build
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        uses: actions/checkout@v4
        with:
          path: exult-i686
      - name: Build i686
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        env:
          MSYSTEM: MINGW32
          ARCH: i686
        run: |
          SNAPSHOT_PATH=$(cygpath -m ~/Exult-dist)
          cd $GITHUB_WORKSPACE/exult-$ARCH
          mkdir -p ${SNAPSHOT_PATH}
          make -j 2 -f Makefile.mingw SNAPSHOT=1 DISTPATH=${SNAPSHOT_PATH} DEBUG_LEVEL=-g1 dist studiodist toolsdist plugindist
          for ff in bgkeyring islefaq sifixes; do
            pushd content/${ff}
            make -j 2 -f Makefile.mingw ${ff}dir=${SNAPSHOT_PATH}/${ff} install
            popd
          done
          echo "EXULT_SNAPSHOT_PATH=${SNAPSHOT_PATH}" >> $GITHUB_ENV
          echo "MSYSHOME=${HOME}" >> $GITHUB_ENV
      - name: Checkout code for x86_64 build
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        uses: actions/checkout@v4
        with:
          path: exult-x86_64
      - name: Build x86_64
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        env:
          MSYSTEM: CLANG64
          ARCH: x86_64
        run: |
          SNAPSHOT_PATH=$(cygpath -m ~/Exult-dist)
          cd $GITHUB_WORKSPACE/exult-$ARCH
          mkdir -p ${SNAPSHOT_PATH}
          make -j 2 -f Makefile.mingw SNAPSHOT=1 DISTPATH=${SNAPSHOT_PATH} DEBUG_LEVEL=-g1 dist studiodist toolsdist plugindist aseprite-plugindist
          echo "EXULT_SNAPSHOT_PATH=${SNAPSHOT_PATH}" >> $GITHUB_ENV
      - name: Create installers
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          cd ${{ env.EXULT_SNAPSHOT_PATH }}
          "/c/Program Files (x86)/Inno Setup 6/ISCC.exe" exult_installer.iss
          "/c/Program Files (x86)/Inno Setup 6/ISCC.exe" exult_studio_installer.iss
          "/c/Program Files (x86)/Inno Setup 6/ISCC.exe" exult_tools_installer.iss
          "/c/Program Files (x86)/Inno Setup 6/ISCC.exe" exult_shpplugin_installer.iss
          "/c/Program Files/7-Zip/7z.exe" -tzip a Keyring.zip -mx9 -mmt ./bgkeyring/*
          "/c/Program Files/7-Zip/7z.exe" -tzip a SFisland.zip -mx9 -mmt ./islefaq/*
          "/c/Program Files/7-Zip/7z.exe" -tzip a Sifixes.zip -mx9 -mmt ./sifixes/*
          "/c/Program Files/7-Zip/7z.exe" -tzip a exult_shp_win64.aseprite-extension -mx9 -mmt ./AsepritePlugin-x86_64/*
          echo "GITHUB_REF=v1.9.0.$(date --utc +'%Y%m%d')" >> $GITHUB_ENV
      - name: Save artifacts
        if: ${{ env.EXULT_REPO_ALIVE == 'true' }}
        run: |
          cd $GITHUB_WORKSPACE
          mkdir -p windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/Exult.exe ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/ExultStudio.exe ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/ExultTools.exe ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/Gimp30Plugin.exe ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/Keyring.zip ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/SFisland.zip ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/Sifixes.zip ./windows-snapshot
          cp ${{ env.EXULT_SNAPSHOT_PATH }}/exult_shp_win64.aseprite-extension ./windows-snapshot
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: "windows snapshot"
          path: windows-snapshot
      - name: Upload unstripped executable artifacts
        uses: actions/upload-artifact@v4
        with:
          name: "Unstripped Windows executables"
          path: |
           exult-i686/Exult.exe 
           exult-x86_64/Exult.exe
          compression-level: 9
