name: Installer
on: push
jobs:
  # Windows_installer_MXE:
  #   runs-on: ubuntu-20.04
  #   outputs:
  #     THID: ${{ steps.build.outputs.THID_out }}
  #     git_branch: ${{ steps.build.outputs.git_branch }}
  #   env:
  #     THDIR: /home/runner/work/therion
  #   steps:
  #     - uses: actions/checkout@v4
  #       with:
  #         fetch-depth: 0
  #     - name: install dependencies
  #       run: |
  #         sudo dpkg --add-architecture i386
  #         sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 86B72ED9
  #         sudo add-apt-repository 'deb [arch=amd64] https://mirror.mxe.cc/repos/apt focal main'
  #         sudo rm -f /etc/apt/sources.list.d/microsoft-prod.list
  #         sudo apt -qq update
  #         sudo apt install -yqq --allow-downgrades libgd3/focal libpcre2-8-0/focal libpcre2-16-0/focal libpcre2-32-0/focal libpcre2-posix2/focal
  #         sudo apt purge -yqq libmono* moby* mono* php* libgdiplus libpcre2-posix3 libzip4
  #         sudo apt install -y wine32
  #         sudo apt install -y ninja-build
  #         sudo apt install -y mxe-i686-w64-mingw32.static-binutils mxe-i686-w64-mingw32.static-bzip2 mxe-i686-w64-mingw32.static-expat mxe-i686-w64-mingw32.static-freetype-bootstrap mxe-i686-w64-mingw32.static-gcc mxe-i686-w64-mingw32.static-gettext mxe-i686-w64-mingw32.static-glib mxe-i686-w64-mingw32.static-harfbuzz mxe-i686-w64-mingw32.static-jpeg \
  #                             mxe-i686-w64-mingw32.static-libiconv mxe-i686-w64-mingw32.static-libpng mxe-i686-w64-mingw32.static-tiff mxe-i686-w64-mingw32.static-vtk mxe-i686-w64-mingw32.static-wxwidgets mxe-i686-w64-mingw32.static-xz mxe-i686-w64-mingw32.static-zlib mxe-i686-w64-mingw32.static-proj
  #     - name: build and create the installation package
  #       id: build
  #       run: |
  #         export PATH=/usr/lib/mxe/usr/bin:$PATH
  #         mkdir -p $HOME/.wine/drive_c/windows
  #         echo -e "mpost-path ${THDIR}/therion-batteries/bin/windows/mpost.exe\npdftex-path ${THDIR}/therion-batteries/bin/windows/pdftex.exe\nidentify-path ${THDIR}/therion-batteries/bin/identify.exe\nconvert-path ${THDIR}/therion-batteries/bin/convert.exe\n" > $HOME/.wine/drive_c/windows/therion.ini
  #         wget -qO - https://github.com/therion/therion-batteries/archive/master.tar.gz | tar -xz && mv therion-batteries-master ../therion-batteries
  #         if ${{startsWith(github.ref, 'refs/tags/v')}}; then THID=${GITHUB_REF##*/}; else THID=$(git rev-parse --short HEAD); fi
  #         echo "THID_out=$THID" >> $GITHUB_OUTPUT
  #         BRANCH_FULL=$(git branch -r --contains ${{ github.ref }})
  #         BRANCH=${BRANCH_FULL##*/}
  #         echo "git_branch=$BRANCH" >> $GITHUB_OUTPUT
  #         mkdir ../therion.bin
  #         cd ../therion.bin
  #         i686-w64-mingw32.static-cmake -G Ninja \
  #             -DMXE_USE_CCACHE=OFF \
  #             -DCMAKE_CROSSCOMPILING_EMULATOR=wine \
  #             -DBUILD_SHARED_LIBS=OFF ../therion
  #         ninja
  #         cp -r ../therion-batteries/_proj/{proj-$(awk -F "=" '/version/{print $2}' innosetup.ini),proj-datumgrid}/ ../therion-batteries/lib/
  #         ninja samples
  #         cd ../therion-batteries
  #         wine InnoSetup/ISCC.exe therion.iss
  #         cd ../therion.bin
  #         mv therion-setup.exe therion-setup-$THID.exe
  #         mv thbook/thbook.pdf thbook-$THID.pdf
  #     - uses: 'actions/upload-artifact@v4'
  #       with:
  #         name: therion-setup-mxe-${{ steps.build.outputs.THID_out }}
  #         path: |
  #           ${{ env.THDIR }}/therion.bin/therion-setup-${{ steps.build.outputs.THID_out }}.exe
  #           ${{ env.THDIR }}/therion.bin/thbook-${{ steps.build.outputs.THID_out }}.pdf
  Windows_installer:
    runs-on: windows-latest
    strategy:
      matrix:
        config:
          - { msystem: UCRT64, arch: ucrt-x86_64 }
    defaults:
      run:
        shell: msys2 {0}
    outputs:
      THID: ${{ steps.build.outputs.THID_out }}
      git_branch: ${{ steps.build.outputs.git_branch }}
    env:
      THDIR: d:/a/therion
    steps:
      - uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.config.msystem }}
          update: true
          install: make git python mingw-w64-${{ matrix.config.arch }}-freetype mingw-w64-${{ matrix.config.arch }}-cmake mingw-w64-${{ matrix.config.arch }}-proj mingw-w64-${{ matrix.config.arch }}-shapelib mingw-w64-${{ matrix.config.arch }}-vtk mingw-w64-${{ matrix.config.arch }}-wxWidgets3.2 mingw-w64-${{ matrix.config.arch }}-gcc mingw-w64-${{ matrix.config.arch }}-make mingw-w64-${{ matrix.config.arch }}-bwidget mingw-w64-${{ matrix.config.arch }}-fmt mingw-w64-${{ matrix.config.arch }}-catch
      - name: prevent git EOL conversion
        run: git config --global core.autocrlf input
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Fetch tags (relevant for forks only)
        if: github.repository != 'therion/therion'
        run: git fetch https://github.com/therion/therion tag 'v*'
      - name: build and create the installation package
        id: build
        run: |
          reg add HKCU\\Software\\Therion //v InstallDir //t REG_SZ //d "${THDIR}/therion-batteries"
          mkdir -p $HOME/.therion
          echo -e "mpost-path ${THDIR}/therion-batteries/bin/windows/mpost.exe\npdftex-path ${THDIR}/therion-batteries/bin/windows/pdftex.exe\nidentify-path ${THDIR}/therion-batteries/bin/identify.exe\nconvert-path ${THDIR}/therion-batteries/bin/convert.exe\n" > $HOME/.therion/therion.ini
          wget -qO - https://github.com/therion/therion-batteries/archive/master.tar.gz | tar -xzf - && mv therion-batteries-master ../therion-batteries
          if ${{startsWith(github.ref, 'refs/tags/v')}}; then THID=${GITHUB_REF##*/}; FID=${THID#?}; else THID=$(git rev-parse --short HEAD); FID=$(git rev-parse HEAD); fi
          echo "THID_out=$THID" >> $GITHUB_OUTPUT
          BRANCH_FULL=$(git branch -r --contains ${{ github.ref }})
          BRANCH=${BRANCH_FULL##*/}
          echo "git_branch=$BRANCH" >> $GITHUB_OUTPUT
          mkdir ../therion.bin
          cd ../therion.bin
          cmake -G "MSYS Makefiles" -DCMAKE_CXX_FLAGS="-Werror" -DUSE_BUNDLED_SHAPELIB=ON ../therion
          cmake --build . -j 4
          cp -r ../therion-batteries/_proj/{proj-$(awk -F "=" '/version/{print $2}' innosetup.ini),proj-datumgrid}/ ../therion-batteries/lib/
          cmake --build . -t samples deploy -- -j 4
          cd ../therion-batteries
          InnoSetup/ISCC.exe therion.iss
          cd ../therion.bin
          mv therion-setup.exe therion-setup-$THID.exe
          mv thbook/thbook.pdf thbook-$THID.pdf
          curl -L https://github.com/${{ github.repository }}/archive/$THID.tar.gz -o therion-$FID.tar.gz
          curl -L https://github.com/${{ github.repository }}/archive/$THID.zip -o therion-$FID.zip
          sha256sum thbook-$THID.pdf therion-$FID.tar.gz therion-$FID.zip therion-setup-$THID.exe > sha256-$THID.txt
      - uses: actions/upload-artifact@v4
        with:
          name: therion-setup-msys2-${{ steps.build.outputs.THID_out }}
          path: |
            ${{ env.THDIR }}/therion.bin/therion-setup-${{ steps.build.outputs.THID_out }}.exe
            ${{ env.THDIR }}/therion.bin/thbook-${{ steps.build.outputs.THID_out }}.pdf
            ${{ env.THDIR }}/therion.bin/sha256-${{ steps.build.outputs.THID_out }}.txt
  Release:
    runs-on: ubuntu-latest
    needs: Windows_installer
    if: startsWith(github.ref, 'refs/tags/v') && needs.Windows_installer.outputs.git_branch == 'master' && github.event_name == 'push'
    steps:
      - name: get the upload url
        run: |
          URL=$(curl -s 'https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ needs.Windows_installer.outputs.THID }}' | python3 -c "import sys, json; print(json.load(sys.stdin)['id'])")
          URL="https://uploads.github.com/repos/${{ github.repository }}/releases/${URL}/assets{?name,label}"
          echo "URL=$URL" >> $GITHUB_ENV
      - name: Download binaries
        uses: actions/download-artifact@v4
        with:
          name: therion-setup-msys2-${{ needs.Windows_installer.outputs.THID }}
      - name: Upload installer
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ env.URL }}
          asset_path: therion-setup-${{ needs.Windows_installer.outputs.THID }}.exe
          asset_name: therion-setup-${{ needs.Windows_installer.outputs.THID }}.exe
          asset_content_type: application/exe
      - name: Upload thbook
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ env.URL }}
          asset_path: thbook-${{ needs.Windows_installer.outputs.THID }}.pdf
          asset_name: thbook-${{ needs.Windows_installer.outputs.THID }}.pdf
          asset_content_type: application/pdf
      - name: Upload checksums
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ env.URL }}
          asset_path: sha256-${{ needs.Windows_installer.outputs.THID }}.txt
          asset_name: sha256-${{ needs.Windows_installer.outputs.THID }}.txt
          asset_content_type: text/plain
