name: Stable release builds

on:
  push:
    branches: [ release ]

env:
  GODOT_VERSION: 4.1.1
  EXPORT_NAME: Pixelorama
  TAG: v1.0
  BUTLER_API_KEY: ${{ secrets.BUTLER_API_KEY }}

jobs:
  export-windows:
    name: Windows Export 🗔
    runs-on: ubuntu-latest
    container:
      image: docker://barichello/godot-ci:4.1.1
    steps:
      - name: Setup WINE, rcedit and NSIS 🍷
        run: |
          dpkg --add-architecture i386 && apt-get update && apt-get install -y wine-stable && apt-get install -y wine32 && apt-get install -y nsis
          chown root:root -R ~
          wget https://github.com/electron/rcedit/releases/download/v1.1.1/rcedit-x64.exe
          mkdir -v -p ~/.local/share/rcedit
          mv rcedit-x64.exe ~/.local/share/rcedit
          godot --headless --quit
          echo 'export/windows/wine = "/usr/bin/wine"' >> ~/.config/godot/editor_settings-4.tres
          echo 'export/windows/rcedit = "/github/home/.local/share/rcedit/rcedit-x64.exe"' >> ~/.config/godot/editor_settings-4.tres
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup 💻
        run: |
          mkdir -v -p build/windows-64bit build/windows-32bit ~/.local/share/godot/export_templates
          mv /root/.local/share/godot/export_templates/${GODOT_VERSION}.stable ~/.local/share/godot/export_templates/${GODOT_VERSION}.stable
      - name: Windows Build 🔧
        run: |
          godot --headless -v --export-release "Windows Desktop 64-bit" ./build/windows-64bit/$EXPORT_NAME.exe
          godot --headless -v --export-release "Windows Desktop 32-bit" ./build/windows-32bit/$EXPORT_NAME.exe
      - name: Copy pixelorama_data folder 📁
        run: |
          rm ./pixelorama_data/.gdignore
          cp -R ./pixelorama_data ./build/windows-64bit
          cp -R ./pixelorama_data ./build/windows-32bit
          cp -R ./pixelorama_data ./build
      - name: Zip 🗜️
        working-directory: ./build
        run: |
          zip -r Pixelorama.Windows-64bit.zip windows-64bit
          zip -r Pixelorama.Windows-32bit.zip windows-32bit
      - name: Build installer 🔧
        run: |
          python3 ./installer/utils/po2nsi.py -i ./installer/pixelorama.nsi -o ./installer/pixelorama_loc.nsi -p ./installer/po -l "English" -v
          makensis ./installer/pixelorama_loc.nsi
          mkdir ./build/installer
          mv ./installer/${EXPORT_NAME}_${TAG}_setup.exe ./build/installer
      - name: Upload Release Assets to itch.io 🎮
        run: |
          butler push ./build/windows-64bit ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:windows-64 --userversion ${{env.TAG}}
          butler push ./build/windows-32bit ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:windows-32 --userversion ${{env.TAG}}
          butler push ./build/installer ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:windows-installer --userversion ${{env.TAG}}
      - name: Upload Release Asset 🚀
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ./build/*
          tag: ${{env.TAG}}
          overwrite: true
          file_glob: true

  export-linux:
    name: Linux Export 🐧
    runs-on: ubuntu-latest
    container:
      image: docker://barichello/godot-ci:4.1.1
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup 💻
        run: |
          mkdir -v -p build/linux-64bit build/linux-32bit ~/.local/share/godot/export_templates
          mv /root/.local/share/godot/export_templates/${GODOT_VERSION}.stable ~/.local/share/godot/export_templates/${GODOT_VERSION}.stable
      - name: Linux Build 🔧
        run: |
          godot --headless -v --export-release "Linux/X11 64-bit" ./build/linux-64bit/$EXPORT_NAME.x86_64
          godot --headless -v --export-release "Linux/X11 32-bit" ./build/linux-32bit/$EXPORT_NAME.x86
      - name: Give execute permission ☑️
        run: |
          chmod +x ./build/linux-64bit/$EXPORT_NAME.x86_64
          chmod +x ./build/linux-32bit/$EXPORT_NAME.x86
      - name: Copy pixelorama_data folder 📁
        run: |
          rm ./pixelorama_data/.gdignore
          cp -R ./pixelorama_data ./build/linux-64bit
          cp -R ./pixelorama_data ./build/linux-32bit
      - name: Create tar.gz archive 🗜️
        run: |
          cd build
          tar zcvf Pixelorama.Linux-64bit.tar.gz linux-64bit
          tar zcvf Pixelorama.Linux-32bit.tar.gz linux-32bit
      - name: Upload Release Assets to itch.io 🎮
        run: |
          butler push ./build/Pixelorama.Linux-64bit.tar.gz ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:linux-64 --userversion ${{env.TAG}}
          butler push ./build/Pixelorama.Linux-32bit.tar.gz ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:linux-32 --userversion ${{env.TAG}}
      - name: Upload Release Asset 🚀
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ./build/Pixelorama.Linux-*.tar.gz
          tag: ${{env.TAG}}
          overwrite: true
          file_glob: true

  export-mac:
    name: Mac Export 🍎
    runs-on: macos-latest
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup environment 🔧
        run: |
          export GODOT_VERSION=${GODOT_VERSION}
          export EXPORT_NAME=Pixelorama
      - name: Download and extract export templates 💾
        run: |
          mkdir -v -p "/Users/runner/Library/Application Support/Godot/export_templates/${GODOT_VERSION}.stable"
          curl -O https://downloads.tuxfamily.org/godotengine/${GODOT_VERSION}/Godot_v${GODOT_VERSION}-stable_export_templates.tpz
          unzip -a Godot_v${GODOT_VERSION}-stable_export_templates.tpz
          mv ./templates/* "/Users/runner/Library/Application Support/Godot/export_templates/${GODOT_VERSION}.stable"
      - name: Download Godot headless binary 🤖
        run: |
          wget https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_macos.universal.zip
          unzip -a Godot_v${GODOT_VERSION}-stable_macos.universal.zip
      - name: Setup 💻
        run: mkdir -v -p ./build/mac
      - name: Mac Build 🔧
        run: |
          chown runner ./Godot.app/Contents/MacOS/Godot
          chmod +x ./Godot.app/Contents/MacOS/Godot
          ./Godot.app/Contents/MacOS/Godot --headless -v --export-release "macOS" ./build/mac/Pixelorama.zip
      - name: Make application executable 🔧
        run: |
          unzip -a ./build/mac/Pixelorama.zip -d ./build/mac
          chmod +x ./build/mac/Pixelorama.app/Contents/MacOS/Pixelorama
          rm ./build/mac/Pixelorama.zip
      - name: Fix application icon 🖼
        run: sips -s format icns ./build/mac/Pixelorama.app/Contents/Resources/icon.icns --out ./build/mac/Pixelorama.app/Contents/Resources/icon.icns
      - name: Copy pixelorama_data folder 📁
        run: |
          cp -R ./pixelorama_data ./build/mac/Pixelorama.app/Contents/Resources
          rm ./build/mac/Pixelorama.app/Contents/Resources/pixelorama_data/.gdignore
      - name: Ad-hoc signing ✍️
        run: codesign -s - --force --deep ./build/mac/Pixelorama.app
      - name: Create DMG archive 🔧
        run: hdiutil create -srcfolder ./build/mac -fs HFS+ -volname Pixelorama ./build/mac/Pixelorama.dmg
      - name: Upload Release Assets to itch.io 🎮
        run: |
          curl -L -o butler.zip https://broth.itch.ovh/butler/darwin-amd64/LATEST/archive/default
          unzip butler.zip
          chmod +x butler
          ./butler push ./build/mac/Pixelorama.dmg ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:mac --userversion ${{env.TAG}}
      - name: Upload Release Asset 🚀
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ./build/mac/Pixelorama.dmg
          asset_name: ${{env.EXPORT_NAME}}[Mac].dmg
          tag: ${{env.TAG}}
          overwrite: true
    
  export-web:
    name: Web Export 🌐
    runs-on: ubuntu-latest
    container:
      image: docker://barichello/godot-ci:4.1.1
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup 💻
        run: |
          mkdir -v -p build/web ~/.local/share/godot/templates
          mv /root/.local/share/godot/templates/${GODOT_VERSION}.stable ~/.local/share/godot/templates/${GODOT_VERSION}.stable
      - name: Web Build 🔧
        run: godot --headless -v --export-release "Web" ./build/web/index.html
      - name: Install rsync 📚
        run: |
          apt-get update && apt-get install -y rsync
      - name: Upload Release Assets to itch.io 🎮
        run: |
          butler push ./build/web ${{ secrets.ITCHIO_USERNAME }}/${{ secrets.ITCHIO_GAME }}:web --userversion ${{env.TAG}}
      # Workaround for SharedArrayBuffer support on GitHub Pages
      # See: https://github.com/godotengine/godot-docs/issues/7084
      - name: Enable SharedArrayBuffer for GitHub Pages
        run: |
          cd ./build/web
          apt-get install -y curl
          curl -fsSL https://github.com/gzuidhof/coi-serviceworker/raw/master/coi-serviceworker.js > coi-serviceworker.js
          sed -i 's#\(		<script src="index.js"></script>\)#		<script src="coi-serviceworker.js"></script>\n\1#g' index.html
      - name: Deploy 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          single-commit: true
          folder: build/web # The folder the action should deploy.
