name: Update Homebrew Formula

on:
  release:
    types:
      - published

jobs:
  update-homebrew:
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Get release info
        id: release
        run: |
          echo "tag_name=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
          echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Wait for release assets to be available
        run: |
          TAG_NAME="${{ steps.release.outputs.tag_name }}"

          # Function to check if a file exists
          check_file() {
            local url="$1"
            local filename="$2"
            echo "Checking if $filename is available..."
            if curl --output /dev/null --silent --head --fail "$url"; then
              echo "✓ $filename is available"
              return 0
            else
              echo "✗ $filename is not yet available"
              return 1
            fi
          }

          # List of files to check
          declare -a files=(
            "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-macos-64.tar.gz"
            "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-macos-arm64-v8a.tar.gz"
            "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-linux-64.tar.gz"
            "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-linux-arm64-v8a.tar.gz"
          )

          # Wait for all files to be available (max 10 minutes)
          max_attempts=60
          attempt=1

          while [ $attempt -le $max_attempts ]; do
            echo "Attempt $attempt/$max_attempts - Checking release assets..."
            all_available=true

            for url in "${files[@]}"; do
              filename=$(basename "$url")
              if ! check_file "$url" "$filename"; then
                all_available=false
                break
              fi
            done

            if [ "$all_available" = true ]; then
              echo "All release assets are available!"
              break
            fi

            if [ $attempt -eq $max_attempts ]; then
              echo "Timeout: Not all release assets are available after $max_attempts attempts"
              exit 1
            fi

            echo "Waiting 10 seconds before next check..."
            sleep 10
            ((attempt++))
          done

      - name: Download release assets and calculate SHA256 checksums
        id: checksums
        run: |
          VERSION="${{ steps.release.outputs.version }}"
          TAG_NAME="${{ steps.release.outputs.tag_name }}"

          # Download binary files from releases and calculate SHA256
          mkdir -p downloads

          # macOS Intel
          wget -O downloads/nginx-ui-macos-64.tar.gz "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-macos-64.tar.gz"
          MACOS_INTEL_SHA256=$(sha256sum downloads/nginx-ui-macos-64.tar.gz | cut -d' ' -f1)

          # macOS ARM
          wget -O downloads/nginx-ui-macos-arm64-v8a.tar.gz "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-macos-arm64-v8a.tar.gz"
          MACOS_ARM_SHA256=$(sha256sum downloads/nginx-ui-macos-arm64-v8a.tar.gz | cut -d' ' -f1)

          # Linux Intel
          wget -O downloads/nginx-ui-linux-64.tar.gz "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-linux-64.tar.gz"
          LINUX_INTEL_SHA256=$(sha256sum downloads/nginx-ui-linux-64.tar.gz | cut -d' ' -f1)

          # Linux ARM
          wget -O downloads/nginx-ui-linux-arm64-v8a.tar.gz "https://github.com/${{ github.repository }}/releases/download/$TAG_NAME/nginx-ui-linux-arm64-v8a.tar.gz"
          LINUX_ARM_SHA256=$(sha256sum downloads/nginx-ui-linux-arm64-v8a.tar.gz | cut -d' ' -f1)

          echo "macos_intel_sha256=$MACOS_INTEL_SHA256" >> $GITHUB_OUTPUT
          echo "macos_arm_sha256=$MACOS_ARM_SHA256" >> $GITHUB_OUTPUT
          echo "linux_intel_sha256=$LINUX_INTEL_SHA256" >> $GITHUB_OUTPUT
          echo "linux_arm_sha256=$LINUX_ARM_SHA256" >> $GITHUB_OUTPUT

      - name: Generate Homebrew Formula
        id: formula
        run: |
          VERSION="${{ steps.release.outputs.version }}"

          cat > nginx-ui.rb << 'EOF'
          class NginxUi < Formula
            desc     "Yet another Nginx Web UI"
            homepage "https://github.com/0xJacky/nginx-ui"
            version  "${{ steps.release.outputs.version }}"
            license  "AGPL-3.0"

            on_macos do
              on_intel do
                url     "https://github.com/0xJacky/nginx-ui/releases/download/v#{version}/nginx-ui-macos-64.tar.gz"
                sha256  "${{ steps.checksums.outputs.macos_intel_sha256 }}"
              end
              on_arm do
                url     "https://github.com/0xJacky/nginx-ui/releases/download/v#{version}/nginx-ui-macos-arm64-v8a.tar.gz"
                sha256  "${{ steps.checksums.outputs.macos_arm_sha256 }}"
              end
            end

            on_linux do
              on_intel do
                url     "https://github.com/0xJacky/nginx-ui/releases/download/v#{version}/nginx-ui-linux-64.tar.gz"
                sha256  "${{ steps.checksums.outputs.linux_intel_sha256 }}"
              end
              on_arm do
                url     "https://github.com/0xJacky/nginx-ui/releases/download/v#{version}/nginx-ui-linux-arm64-v8a.tar.gz"
                sha256  "${{ steps.checksums.outputs.linux_arm_sha256 }}"
              end
            end

            def install
              bin.install "nginx-ui"

              # Create configuration directory
              (etc/"nginx-ui").mkpath

              # Create default configuration file if it doesn't exist
              config_file = etc/"nginx-ui/app.ini"
              unless config_file.exist?
                config_file.write <<~EOS
                  [app]
                  PageSize = 10

                  [server]
                  Host = 0.0.0.0
                  Port = 9000
                  RunMode = release

                  [cert]
                  HTTPChallengePort = 9180

                  [terminal]
                  StartCmd = login
                EOS
              end

              # Create data directory
              (var/"nginx-ui").mkpath
            end

            def post_install
              # Ensure correct permissions
              (var/"nginx-ui").chmod 0755
            end

            service do
              run [opt_bin/"nginx-ui", "serve", "--config", etc/"nginx-ui/app.ini"]
              keep_alive true
              working_dir var/"nginx-ui"
              log_path var/"log/nginx-ui.log"
              error_log_path var/"log/nginx-ui.err.log"
            end

            test do
              assert_match version.to_s, shell_output("#{bin}/nginx-ui --version")
            end
          end
          EOF

          echo "Generated Homebrew Formula:"
          cat nginx-ui.rb

      - name: Checkout homebrew-tools repository
        uses: actions/checkout@v4
        with:
          repository: 0xJacky/homebrew-tools
          path: homebrew-tools
          token: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}

      - name: Update Formula file
        run: |
          # Copy the generated formula to the correct location
          mkdir -p homebrew-tools/Formula/
          cp nginx-ui.rb homebrew-tools/Formula/nginx-ui.rb

      - name: Verify Formula
        run: |
          cd homebrew-tools
          # Basic syntax check
          ruby -c Formula/nginx-ui.rb
          echo "Formula syntax is valid"

      - name: Create Pull Request to homebrew-tools
        uses: peter-evans/create-pull-request@v7
        with:
          token: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
          path: homebrew-tools
          branch: update-nginx-ui-${{ steps.release.outputs.version }}
          delete-branch: true
          title: 'nginx-ui ${{ steps.release.outputs.version }}'
          body: |
            Update nginx-ui to version ${{ steps.release.outputs.version }}

            **Release Notes:**
            - Version: ${{ steps.release.outputs.version }}
            - Release URL: https://github.com/${{ github.repository }}/releases/tag/${{ steps.release.outputs.tag_name }}

            **Checksums (SHA256):**
            - macOS Intel: ${{ steps.checksums.outputs.macos_intel_sha256 }}
            - macOS ARM: ${{ steps.checksums.outputs.macos_arm_sha256 }}
            - Linux Intel: ${{ steps.checksums.outputs.linux_intel_sha256 }}
            - Linux ARM: ${{ steps.checksums.outputs.linux_arm_sha256 }}

            ---

            This PR was automatically generated by GitHub Actions.
          commit-message: 'nginx-ui ${{ steps.release.outputs.version }}'
          committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
          add-paths: |
            Formula/nginx-ui.rb
