name: Build new release

on:
  workflow_dispatch:

jobs:
  Build:
    runs-on: debian-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Dependencies
        run: |
            apt update
            apt install curl p7zip nsis -y

      - name: Setup dotnet 8 SDK
        run: curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel 8.0

      - name: Build for Windows/Linux
        run: |
          export DOTNET_ROOT=$HOME/.dotnet
          export PATH=$PATH:$HOME/.dotnet:$HOME/.dotnet/tools
          export DOTNET_CLI_TELEMETRY_OPTOUT=true

          echo "VERSION_TAG=$(cat VERSION.txt)" >> "$FORGEJO_ENV"
          echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> "$FORGEJO_ENV"

          dotnet tool install --global Obfuscar.GlobalTool
          dotnet publish -r win-x64 --self-contained true -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true
          dotnet publish -r linux-x64 --self-contained true -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true

          mkdir pkg-linux
          mkdir pkg-win
          cp -r modmenu pkg-linux/
          cp -r modmenu pkg-win/
          cp icon.ico pkg-linux/
          cp icon.ico pkg-win/
          cp assets/config.json pkg-linux/
          cp assets/config.json pkg-win/
          cp bin/Release/net8.0/linux-x64/publish/SplatTool pkg-linux/SplatTool
          cp bin/Release/net8.0/linux-x64/publish/Photino.Native.so pkg-linux/Photino.Native.so
          cp bin/Release/net8.0/linux-x64/publish/libnfd.so pkg-linux/libnfd.so
          cp bin/Release/net8.0/win-x64/publish/SplatTool.exe pkg-win/SplatTool.exe
          cp bin/Release/net8.0/win-x64/publish/Photino.Native.dll pkg-win/Photino.Native.dll
          cp bin/Release/net8.0/win-x64/publish/nfd.dll pkg-win/nfd.dll
          cp bin/Release/net8.0/win-x64/publish/WebView2Loader.dll pkg-win/WebView2Loader.dll

      - name: Package up for Release
        run: |
          cd pkg-linux
          tar -cvzf ../SplatTool-${{ env.VERSION_TAG }}-${{ env.SHORT_SHA }}-x86_64-linux.tar.gz * && cd ..
          cd pkg-win
          7z a ../SplatTool-${{ env.VERSION_TAG }}-${{ env.SHORT_SHA }}-x86_64-win.7z * && cd ..
          makensis "-DVERSION=${{ env.VERSION_TAG }}" "-DSHORT_SHA=${{ env.SHORT_SHA }}" ./SplatTool_Installer.nsi

      - name: Upload Windows Installer
        uses: forgejo/upload-artifact@v4
        with:
          name: SplatTool-Win-Installer
          path: SplatTool-${{ env.VERSION_TAG }}-${{ env.SHORT_SHA }}-Setup.exe

      - name: Upload Windows Portable
        uses: forgejo/upload-artifact@v4
        with:
          name: SplatTool-Win-Portable
          path: SplatTool-${{ env.VERSION_TAG }}-${{ env.SHORT_SHA }}-x86_64-win.7z

      - name: Upload Linux Build
        uses: forgejo/upload-artifact@v4
        with:
          name: SplatTool-Linux
          path: SplatTool-${{ env.VERSION_TAG }}-${{ env.SHORT_SHA }}-x86_64-linux.tar.gz