name: Build libostree Backport

on:
  push:
    branches: [ main, master ]
  pull_request:
    branches: [ main, master ]
  workflow_dispatch:

env:
  UBUNTU_VERSION: "24.04"
  OSTREE_VERSION: "2025.2"
  BACKPORT_SUFFIX: "~noble1"

jobs:
  build-libostree-backport:
    name: Build libostree Backport
    runs-on: ubuntu-latest
    container:
      image: ubuntu:latest
    steps:
      - name: Setup build environment
        run: |
          apt update -y
          apt install -y devscripts build-essential wget git

      - name: Checkout repository manually
        run: |
          # Clone the repository manually instead of using actions/checkout
          git clone https://git.raines.xyz/robojerk/libostree-dev.git /tmp/libostree-dev
          cp -r /tmp/libostree-dev/* .
          cp -r /tmp/libostree-dev/.* . 2>/dev/null || true

      - name: Install curl and jq for early testing
        run: |
          apt-get update -y
          apt-get install -y curl jq

      - name: Debug - Check ACCESS_TOKEN (safe)
        run: |
          echo "=== Debugging ACCESS_TOKEN ==="
          echo "Token exists: ${{ secrets.FORGEJO_TOKEN != '' }}"
          echo "Token length: ${#FORGEJO_TOKEN}"
          echo "Token first 4 chars: $(echo "$FORGEJO_TOKEN" | cut -c1-4)..."
          echo "Token last 4 chars: ...$(echo "$FORGEJO_TOKEN" | rev | cut -c1-4 | rev)"
          echo "Environment variable name: FORGEJO_TOKEN"
          echo "Available secrets:"
          env | grep -i token || echo "No token env vars found"
        env:
          FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}

      - name: Test API endpoints
        run: |
          echo "=== Testing Forgejo API endpoints with ACCESS_TOKEN ==="
          
          # Test 1: Check Forgejo version and capabilities
          echo "Testing Forgejo version..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/version" | jq . 2>/dev/null || echo "Version endpoint failed"
          
          echo ""
          echo "=== Testing user info ==="
          
          # Test 2: Check user info
          echo "Testing user info..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/user" | jq . 2>/dev/null || echo "User endpoint failed"
          
          echo ""
          echo "=== Testing repository info ==="
          
          # Test 3: Check repository info
          echo "Testing repository info..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/repos/robojerk/libostree-dev" | jq . 2>/dev/null || echo "Repository endpoint failed"
          
          echo ""
          echo "=== Testing package registry endpoints ==="
          
          # Test 4: Check if package registry is enabled
          echo "Testing package registry availability..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/user/packages" | jq . 2>/dev/null || echo "User packages endpoint failed"
          
          echo ""
          echo "=== Testing repository packages ==="
          
          # Test 5: Check repository packages
          echo "Testing repository packages..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/repos/robojerk/libostree-dev/packages" | jq . 2>/dev/null || echo "Repository packages endpoint failed"
          
          echo ""
          echo "=== Testing package registry types ==="
          
          # Test 6: Check available package types
          echo "Testing Debian package registry..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/packages/robojerk/debian" | jq . 2>/dev/null || echo "Debian packages endpoint failed"
          
          echo "Testing Generic package registry..."
          curl -s -H "Authorization: Bearer ${{ secrets.FORGEJO_TOKEN }}" \
               "https://git.raines.xyz/api/v1/packages/robojerk/generic" | jq . 2>/dev/null || echo "Generic packages endpoint failed"

      - name: Test package upload with steam.deb
        run: |
          echo "=== Testing package upload with steam.deb ==="
          
          # Check if steam.deb exists
          if [ ! -f "steam.deb" ]; then
            echo "❌ steam.deb not found, skipping upload test"
            echo "Available files:"
            ls -la *.deb 2>/dev/null || echo "No .deb files found"
            exit 0
          fi
          
          # Test Debian Package Registry upload
          echo "Testing Debian Package Registry upload..."
          response=$(curl -s -w "%{http_code}" --user "robojerk:${{ secrets.ACCESS_TOKEN }}" \
               --upload-file "steam.deb" \
               "https://git.raines.xyz/api/packages/robojerk/debian/pool/noble/main/upload")
          
          http_code=$(echo "$response" | tail -c 4)
          echo "Debian Package Registry HTTP Response Code: $http_code"
          
          if [ "$http_code" = "201" ]; then
            echo "✅ Debian Package Registry upload SUCCESS"
          else
            echo "❌ Debian Package Registry upload FAILED (HTTP $http_code)"
          fi
          
          echo ""
          echo "Testing Generic Package Registry upload..."
          response=$(curl -s -w "%{http_code}" --user "robojerk:${{ secrets.ACCESS_TOKEN }}" \
               --upload-file "steam.deb" \
               "https://git.raines.xyz/api/packages/robojerk/generic/steam/1.0.0.83/steam.deb")
          
          http_code=$(echo "$response" | tail -c 4)
          echo "Generic Package Registry HTTP Response Code: $http_code"
          
          if [ "$http_code" = "201" ]; then
            echo "✅ Generic Package Registry upload SUCCESS"
          else
            echo "❌ Generic Package Registry upload FAILED (HTTP $http_code)"
          fi

      - name: Add source repositories
        run: |
          echo "deb-src http://us.archive.ubuntu.com/ubuntu/ noble main universe" | tee /etc/apt/sources.list.d/noble-sources.list
          apt update -y

      - name: Create backport directory
        run: |
          mkdir -p /opt/Projects/ostree-backport-noble
          cd /opt/Projects/ostree-backport-noble

      - name: Download libostree source
        run: |
          cd /opt/Projects/ostree-backport-noble
          POOL_URL="http://archive.ubuntu.com/ubuntu/pool/universe/o/ostree/"
          wget "${POOL_URL}ostree_${OSTREE_VERSION}-1.dsc"
          wget "${POOL_URL}ostree_${OSTREE_VERSION}.orig.tar.xz"
          wget "${POOL_URL}ostree_${OSTREE_VERSION}-1.debian.tar.xz"

      - name: Extract and modify source
        run: |
          cd /opt/Projects/ostree-backport-noble
          dpkg-source -x "ostree_${OSTREE_VERSION}-1.dsc"
          cd "ostree-${OSTREE_VERSION}"
          dch --newversion "${OSTREE_VERSION}-1${BACKPORT_SUFFIX}" \
            --distribution "noble-backports" \
            -b \
            "Backport libostree ${OSTREE_VERSION}-1 from Ubuntu questing for bootc compatibility."

      - name: Install build dependencies
        run: |
          cd /opt/Projects/ostree-backport-noble/ostree-${OSTREE_VERSION}
          export DEBIAN_FRONTEND=noninteractive
          export APT_LISTCHANGES_FRONTEND=none
          apt-get update
          apt-get build-dep -y --no-install-recommends ./

      - name: Build libostree backport
        run: |
          cd /opt/Projects/ostree-backport-noble/ostree-${OSTREE_VERSION}
          dpkg-buildpackage -us -uc -b

      - name: List built packages
        run: |
          echo "Built libostree packages:"
          ls -la /opt/Projects/ostree-backport-noble/*.deb

      - name: Create release assets
        run: |
          mkdir -p release-assets
          cp /opt/Projects/ostree-backport-noble/*.deb release-assets/
          
          # Create a summary file
          echo "libostree Backport Build Summary" > release-assets/BUILD_SUMMARY.txt
          echo "===============================" >> release-assets/BUILD_SUMMARY.txt
          echo "Build Date: $(date)" >> release-assets/BUILD_SUMMARY.txt
          echo "Ubuntu Version: ${UBUNTU_VERSION}" >> release-assets/BUILD_SUMMARY.txt
          echo "Ostree Version: ${OSTREE_VERSION}" >> release-assets/BUILD_SUMMARY.txt
          echo "Backport Suffix: ${BACKPORT_SUFFIX}" >> release-assets/BUILD_SUMMARY.txt
          echo "" >> release-assets/BUILD_SUMMARY.txt
          echo "Built Packages:" >> release-assets/BUILD_SUMMARY.txt
          ls -la release-assets/*.deb 2>/dev/null || echo "No packages found" >> release-assets/BUILD_SUMMARY.txt
          
          # Create package list for download links
          echo "Package List:" > release-assets/PACKAGES.txt
          ls -1 release-assets/*.deb 2>/dev/null | sed 's|.*/||' >> release-assets/PACKAGES.txt
          
          echo "Release assets created:"
          ls -la release-assets/

      - name: Create download instructions
        run: |
          cat > release-assets/INSTALL.md << EOF
          # libostree 2025.2-1 Backport Installation
          
          ## Quick Install
          
          \`\`\`bash
          # Download and install the main package
          wget https://git.raines.xyz/robojerk/libostree-dev/actions/runs/\${{ github.run_id }}/artifacts
          sudo dpkg -i libostree-dev_2025.2-1~noble1_amd64.deb
          sudo apt-get install -f
          \`\`\`
          
          ## Verification
          
          \`\`\`bash
          # Check if libostree 2025.2-1 is installed
          pkg-config --modversion ostree-1
          # Should output: 2025.2
          \`\`\`
          
          ## Packages Included
          
          EOF
          
          ls -1 release-assets/*.deb 2>/dev/null | sed 's|.*/||' | while read package; do
            echo "- \`$package\`" >> release-assets/INSTALL.md
          done
          
          echo "" >> release-assets/INSTALL.md
          echo "Build completed on: $(date)" >> release-assets/INSTALL.md



      - name: Upload to Debian Package Registry (Primary)
        run: |
          echo "=== Attempting Debian Package Registry upload with ACCESS_TOKEN ==="
          
          for deb_file in release-assets/*.deb; do
            if [ -f "$deb_file" ]; then
              echo "Uploading $deb_file to Debian Package Registry..."
              filename=$(basename "$deb_file")
              echo "File: $filename"
              
              # Use the confirmed working Basic Auth method
              response=$(curl -s -w "%{http_code}" \
                               --user "robojerk:${{ secrets.ACCESS_TOKEN }}" \
                               --upload-file "$deb_file" \
                               "https://git.raines.xyz/api/packages/robojerk/debian/pool/noble/main/upload")
              
              http_code=$(echo "$response" | tail -c 4)
              echo "HTTP Response Code: $http_code"
              
              if [ "$http_code" = "201" ]; then
                echo "✅ Debian Package Registry upload SUCCESS for $deb_file"
              else
                echo "❌ Debian Package Registry upload FAILED for $deb_file (HTTP $http_code)"
              fi
            fi
          done

      - name: Upload to Generic Package Registry (Fallback)
        run: |
          echo "=== Attempting Generic Package Registry upload (fallback) with ACCESS_TOKEN ==="
          
          for deb_file in release-assets/*.deb; do
            if [ -f "$deb_file" ]; then
              echo "Uploading $deb_file to Generic Package Registry..."
              filename=$(basename "$deb_file")
              package_name=$(echo "$filename" | cut -d'_' -f1)
              version=$(echo "$filename" | cut -d'_' -f2 | cut -d'~' -f1)
              
              echo "Package: $package_name"
              echo "Version: $version"
              echo "File: $filename"
              
              # Use the confirmed working Basic Auth method
              response=$(curl -s -w "%{http_code}" \
                               --user "robojerk:${{ secrets.ACCESS_TOKEN }}" \
                               --upload-file "$deb_file" \
                               "https://git.raines.xyz/api/packages/robojerk/generic/$package_name/$version/$filename")
              
              http_code=$(echo "$response" | tail -c 4)
              echo "HTTP Response Code: $http_code"
              
              if [ "$http_code" = "201" ]; then
                echo "✅ Generic Package Registry upload SUCCESS for $deb_file"
              else
                echo "❌ Generic Package Registry upload FAILED for $deb_file (HTTP $http_code)"
              fi
            fi
          done

      - name: Check uploaded packages and provide assignment instructions
        run: |
          echo "=== Package Upload Summary ==="
          echo ""
          echo "📦 Check the HTTP response codes above to see if uploads succeeded"
          echo "🔍 HTTP 201 = Success, HTTP 401 = Authentication failed, HTTP 403 = Permission denied"
          echo ""
          echo "📋 If uploads succeeded (HTTP 201), to make packages visible in your repository:"
          echo "1. Go to: https://git.raines.xyz/robojerk/-/packages"
          echo "2. Find the uploaded packages (they may be under 'debian' or 'generic')"
          echo "3. Click on each package → Settings → Assign to repository"
          echo "4. Select 'libostree-dev' repository"
          echo ""
          echo "🔗 After assignment, packages will appear at:"
          echo "   https://git.raines.xyz/robojerk/libostree-dev/packages"
          echo ""
          echo "📝 Packages that were attempted:"
          for deb_file in release-assets/*.deb; do
            if [ -f "$deb_file" ]; then
              filename=$(basename "$deb_file")
              echo "   📦 $filename"
            fi
          done
          echo ""
          echo "🎯 Next steps:"
          echo "   - Check HTTP response codes above"
          echo "   - If HTTP 201, manually assign packages to repository via web UI"
          echo "   - If HTTP 401/403, check token permissions" 