name: "godot-ci export"
on:
  push:
    branches:
      - stable

jobs:
  export_game:
    runs-on: ubuntu-latest
    name: Export Game
    steps:
      - name: Checkout
        uses: https://github.com/actions/checkout@v3.3.0
      - name: Install Packages
        id: wine_install
        run: |
          apt update -y && apt upgrade -y
          apt install -y wine64 nodejs p7zip-full ca-certificates tree jq curl zip 
          echo "WINE_PATH=$(which wine64)" >> $GITHUB_OUTPUT
      - name: Download Godot build-libs
        run: |
          mkdir -p /root/.local/share/godot/
          wget -nv https://github.com/godotengine/godot/releases/download/4.0.2-stable/Godot_v4.0.2-stable_linux.x86_64.zip -O "/root/.local/share/godot/godot.zip"
          wget -nv https://github.com/godotengine/godot/releases/download/4.0.2-stable/Godot_v4.0.2-stable_export_templates.tpz -O /root/.local/share/godot/godot_templates.tpz
      - name: Setup Godot build-libs
        run: |
          7z x /root/.local/share/godot/godot.zip -o/root/.local/share/godot/godot_executable -y
          chmod +x /root/.local/share/godot/godot_executable/Godot_v4.0.2-stable_linux.x86_64
          unzip /root/.local/share/godot/godot_templates.tpz -d /root/.local/share/godot
          mv /root/.local/share/godot/templates /root/.local/share/godot/4.0.2.stable
          mkdir -p /root/.local/share/godot/export_templates
          mv /root/.local/share/godot/4.0.2.stable /root/.local/share/godot/export_templates
          /root/.local/share/godot/godot_executable/Godot_v4.0.2-stable_linux.x86_64 --version
      - name: Export for Web
        run: |
          mkdir -p ./builds/web/
          /root/.local/share/godot/godot_executable/Godot_v4.0.2-stable_linux.x86_64 /var/lib/actions/project.godot -q --headless --export-release "Web" ./builds/web/Game.html
      - name: Package for Web
        run: |
          cd builds
          tar -czvf web.zip -C web/ .
      - name: Export for Linux
        run: |
          mkdir -p ./builds/linux/
          /root/.local/share/godot/godot_executable/Godot_v4.0.2-stable_linux.x86_64 /var/lib/actions/project.godot -q --headless --export-release "Linux/X11" ./builds/linux/Game.x86_64
      - name: Package for Linux
        run: |
          cd builds
          tar -czvf linux.tar.gz -C linux/ .
      - name: Export for Windows
        run: |
          mkdir -p ./builds/windows/
          /root/.local/share/godot/godot_executable/Godot_v4.0.2-stable_linux.x86_64 /var/lib/actions/project.godot -q --headless --export-release "Windows Desktop" ./builds/windows/Game.exe
      - name: Package for Windows
        run: |
          cd builds
          cd windows; zip -r ../windows.zip *
          cd ..
      - name: Create Release
        run: |
          echo "Getting last Release Tag"
          VERSION=$(curl -X 'GET'   'https://git.euph.dev/api/v1/repos/${{ github.repository }}/releases?page=1&limit=1'   -H 'accept: application/json'   -H 'Authorization: token ${{ secrets.RELEASE_TOKEN }}' )
          echo $VERSION | jq -r '.[0].tag_name'
          VERSION=$(echo $VERSION | jq -r '.[0].tag_name' | awk -F. -v OFS=. '{$NF += 1 ; print}')
          echo "Generate new Release with tag: $VERSION"
          ID=$(curl -X 'POST' \
          'https://git.euph.dev/api/v1/repos/${{ github.repository }}/releases' \
          -H 'accept: application/json' \
          -H 'Content-Type: application/json' \
          -H 'Authorization: token ${{ secrets.RELEASE_TOKEN }}' \
          -d "{
            \"body\": \"Automated Build\",
            \"draft\": true,
            \"name\": \"Automated Release $VERSION\",
            \"prerelease\": true,
            \"tag_name\": \"$VERSION\"
          }")
          ID=$(echo $ID | jq -r '.id')
          echo $ID Upload Web
          curl -X 'POST' \
          "https://git.euph.dev/api/v1/repos/${{ github.repository }}/releases/$ID/assets?name=Web.zip" \
          -H 'accept: application/json' \
          -H 'Content-Type: multipart/form-data' \
          -H 'Authorization: token ${{ secrets.RELEASE_TOKEN }}' \
          -F 'attachment=@./builds/web.zip;type=application/gzip'
          echo $ID Upload Linux
          curl -X 'POST' \
          "https://git.euph.dev/api/v1/repos/${{ github.repository }}/releases/$ID/assets?name=Linux.tar.gz" \
          -H 'accept: application/json' \
          -H 'Content-Type: multipart/form-data' \
          -H 'Authorization: token ${{ secrets.RELEASE_TOKEN }}' \
          -F 'attachment=@./builds/linux.tar.gz;type=application/gzip'
          echo $ID Upload Windows
          curl -X 'POST' \
          "https://git.euph.dev/api/v1/repos/${{ github.repository }}/releases/$ID/assets?name=Windows.zip" \
          -H 'accept: application/json' \
          -H 'Content-Type: multipart/form-data' \
          -H 'Authorization: token ${{ secrets.RELEASE_TOKEN }}' \
          -F 'attachment=@./builds/windows.zip;type=application/gzip'
