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')
          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
          install: |
            base-devel git p7zip zip
            mingw-w64-x86_64-toolchain mingw-w64-x86_64-binutils mingw-w64-x86_64-ntldd mingw-w64-x86_64-SDL2
            mingw-w64-x86_64-fluidsynth mingw-w64-x86_64-libtimidity mingw-w64-x86_64-libogg mingw-w64-x86_64-libvorbis
            mingw-w64-x86_64-munt-mt32emu mingw-w64-x86_64-libpng mingw-w64-x86_64-zlib mingw-w64-x86_64-SDL2_image
            mingw-w64-x86_64-gtk3 mingw-w64-x86_64-adwaita-icon-theme mingw-w64-x86_64-libxml2 mingw-w64-x86_64-freetype
            mingw-w64-x86_64-gtk2 mingw-w64-x86_64-gimp mingw-w64-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-munt-mt32emu mingw-w64-i686-libpng mingw-w64-i686-zlib mingw-w64-i686-SDL2_image
            mingw-w64-i686-gtk3 mingw-w64-i686-adwaita-icon-theme mingw-w64-i686-libxml2 mingw-w64-i686-freetype
            mingw-w64-i686-gtk2 mingw-w64-i686-gimp mingw-w64-i686-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} 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: MINGW64
          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} dist studiodist toolsdist 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/*
          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 }}/Gimp20Plugin.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
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: "windows snapshot"
          path: windows-snapshot
