name: Auto Commit Build

on:
  push:
    branches:
      - main
      - no_mixed_color_gc_plate
      - resonatic_circuit

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    env:
      INCLUDE_PATHS: |
        assets/
        MyCTMLib.cfg
        pack.mcmeta
        pack.png
        README.md

      TARGET_BRANCHES: "main no_mixed_color_gc_plate resonatic_circuit"
      ZIP_NAME_main: "Modernity-GTNH-{SHA}-pre.zip"
      ZIP_NAME_no_mixed_color_gc_plate: "Modernity-GTNH-NoMixinColorGCHeavyMaterial.zip"
      ZIP_NAME_resonatic_circuit: "Modernity-GTNH-GTNLResonaticCircuit.zip"

      EXTERNAL_REPOS: |
        OutlinedOres-Modern=https://github.com/Ranzuu/OutlinedOres-Modern
        Just-Productive-Bees=https://github.com/DarkScorpyon/Just-Productive-Bees-
        Just-Forestry-Bees=https://github.com/DarkScorpyon/Just-Forestry-Bees-
      EXTERNAL_ZIP_NAME_OutlinedOres_Modern: "Modernity-GTNH-OutlinedOres.zip"
      EXTERNAL_ZIP_NAME_Just_Productive_Bees: "Modernity-GTNH-Just-Productive-Bees.zip"
      EXTERNAL_ZIP_NAME_Just_Forestry_Bees: "Modernity-GTNH-Just-Forestry-Bees.zip"

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get short commit hash
        id: vars
        run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV

      - name: Get last two release tags from ABKQPO/MyCTMLib
        id: get_tags
        run: |
          response=$(curl -s "https://api.github.com/repos/ABKQPO/MyCTMLib/releases?per_page=2")
          tags=($(echo "$response" | jq -r '.[].tag_name'))
          echo "TAG1=${tags[0]}" >> $GITHUB_ENV
          echo "TAG2=${tags[1]}" >> $GITHUB_ENV
        shell: bash

      - name: Build all branches
        run: |
          for BRANCH in $TARGET_BRANCHES; do
            git checkout $BRANCH
            VAR_NAME="ZIP_NAME_${BRANCH//-/_}"
            ZIP_TEMPLATE="${!VAR_NAME}"
            ZIP_NAME="${ZIP_TEMPLATE//\{SHA\}/$SHORT_SHA}"

            rm -rf zip-root
            mkdir zip-root
            for path in $INCLUDE_PATHS; do
              if [ -e "$path" ]; then
                cp -r --parents "$path" zip-root/
              fi
            done
            cd zip-root
            zip -r "../$ZIP_NAME" ./
            cd ..
          done
        shell: bash
        env:
          INCLUDE_PATHS: ${{ env.INCLUDE_PATHS }}
          SHORT_SHA: ${{ env.SHORT_SHA }}
          ZIP_NAME_main: ${{ env.ZIP_NAME_main }}
          ZIP_NAME_no_mixed_color_gc_plate: ${{ env.ZIP_NAME_no_mixed_color_gc_plate }}
          ZIP_NAME_resonatic_circuit: ${{ env.ZIP_NAME_resonatic_circuit }}

      - name: Download and package external repositories
        run: |
          mkdir external-zips
          while IFS== read -r NAME URL; do
            [ -z "$NAME" ] && continue
            SAFE_NAME="${NAME//-/_}"
            ZIP_VAR="EXTERNAL_ZIP_NAME_${SAFE_NAME}"
            ZIP_NAME="${!ZIP_VAR}"

            echo "Cloning $URL as $NAME ..."
            git clone --depth=1 "$URL" "$NAME"

            ZIP_DIR="external-zips/${NAME}-zip-root"
            mkdir -p "$ZIP_DIR"

            for path in $INCLUDE_PATHS; do
              [ -z "$path" ] && continue
              if [ -e "$NAME/$path" ]; then
                mkdir -p "$(dirname "$ZIP_DIR/$path")"
                cp -r "$NAME/$path" "$ZIP_DIR/$path"
              fi
            done

            cd "$ZIP_DIR"
            zip -r "../../$ZIP_NAME" ./
            cd ../..
          done <<< "$EXTERNAL_REPOS"
        shell: bash
        env:
          INCLUDE_PATHS: ${{ env.INCLUDE_PATHS }}
          EXTERNAL_ZIP_NAME_OutlinedOres_Modern: ${{ env.EXTERNAL_ZIP_NAME_OutlinedOres_Modern }}
          EXTERNAL_ZIP_NAME_Just_Productive_Bees: ${{ env.EXTERNAL_ZIP_NAME_Just_Productive_Bees }}
          EXTERNAL_ZIP_NAME_Just_Forestry_Bees: ${{ env.EXTERNAL_ZIP_NAME_Just_Forestry_Bees }}

      - name: Download latest two MyCTMLib jars
        run: |
          mkdir jars
          for TAG in "$TAG1" "$TAG2"; do
            url="https://github.com/ABKQPO/MyCTMLib/releases/download/${TAG}/MyCTMLib-${TAG}.jar"
            curl -L "$url" -o jars/MyCTMLib-${TAG}.jar
          done
        shell: bash
        env:
          TAG1: ${{ env.TAG1 }}
          TAG2: ${{ env.TAG2 }}

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: Commit ${{ env.SHORT_SHA }}
          tag_name: commit-${{ env.SHORT_SHA }}
          prerelease: false
          make_latest: true
          files: |
            Modernity-GTNH-${{ env.SHORT_SHA }}-pre.zip
            Modernity-GTNH-NoMixinColorGCHeavyMaterial.zip
            Modernity-GTNH-GTNLResonaticCircuit.zip
            Modernity-GTNH-OutlinedOres.zip
            Modernity-GTNH-Just-Productive-Bees.zip
            Modernity-GTNH-Just-Forestry-Bees.zip
            jars/MyCTMLib-${{ env.TAG1 }}.jar
            jars/MyCTMLib-${{ env.TAG2 }}.jar