#!/bin/bash

# Script to update libostree-dev README with latest build information
# This works around the Forgejo workflow input field issue

set -e

# Configuration
REPO_URL="https://git.raines.xyz/robojerk/libostree-dev"
WORKFLOW_RUN_ID="11"  # Latest successful build
BUILD_DATE=$(date -u '+%Y-%m-%d %H:%M:%S UTC')
VERSION="2025.2-1~noble1"

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${BLUE}Updating libostree-dev README with latest build information...${NC}"

# Create the updated README content
cat > README.md << 'EOF'
# libostree-dev Backport

Compiles a backport of libostree-dev for Ubuntu Noble.

## Source and Target

- **Source**: Ubuntu "Questing Quokka" (questing) - libostree 2025.2-1
- **Target**: Ubuntu "Noble Numbat" (noble) - Ubuntu 24.04 LTS

## For Debian

- **Source**: Debian sid
- **Target**: Debian trixie

## Usage

This repository contains the CI/CD workflow and scripts to build a backport of libostree 2025.2-1 for Ubuntu Noble, which is required for bootc compatibility.

## 📦 Download Latest Build

**Last Built**: 2025-07-21 07:35:00 UTC  
**Version**: 2025.2-1~noble1  
**Target**: Ubuntu Noble (24.04 LTS)
**Build ID**: [11](https://git.raines.xyz/robojerk/libostree-dev/actions/runs/11)

### ⚠️ Important Note

The CI/CD workflow successfully builds the packages, but there are currently issues with the Forgejo Package Registry uploads. The packages are built successfully but not yet accessible via direct download links.

### Current Status

- ✅ **Build Process**: Working correctly
- ✅ **Package Creation**: All packages built successfully
- ❌ **Package Registry**: Upload issues being resolved
- 🔄 **Manual Download**: Available via workflow artifacts

### How to Get the Packages

#### Option 1: Download from Workflow Artifacts (Current)
Since the package registry uploads are having issues, you can download the packages directly from the workflow artifacts:

1. **Go to the Actions page**: https://git.raines.xyz/robojerk/libostree-dev/actions
2. **Find the latest successful build** (Build #11)
3. **Click on the build** to view details
4. **Download the artifacts** (if available) or check the build logs for package locations

#### Option 2: Build Locally (Recommended)
The most reliable way to get the packages is to build them locally:

```bash
# Clone the repository
git clone https://git.raines.xyz/robojerk/libostree-dev.git
cd libostree-dev

# Run the backport script
./libostree-dev_noble_backport.sh
```

#### Option 3: Manual Build Process
If you prefer to build manually:

```bash
# Install build dependencies
sudo apt update
sudo apt install -y devscripts build-essential wget git

# Create build directory
mkdir -p /opt/Projects/ostree-backport-noble
cd /opt/Projects/ostree-backport-noble

# Download source from Ubuntu Questing
wget http://archive.ubuntu.com/ubuntu/pool/universe/o/ostree/ostree_2025.2-1.dsc
wget http://archive.ubuntu.com/ubuntu/pool/universe/o/ostree/ostree_2025.2.orig.tar.xz
wget http://archive.ubuntu.com/ubuntu/pool/universe/o/ostree/ostree_2025.2-1.debian.tar.xz

# Extract and build
dpkg-source -x ostree_2025.2-1.dsc
cd ostree-2025.2
dch --newversion "2025.2-1~noble1" --distribution "noble-backports" -b "Backport for bootc compatibility"
sudo apt build-dep ./
dpkg-buildpackage -us -uc -b
```

### Available Packages

The build creates these packages:
- `gir1.2-ostree-1.0_2025.2-1~noble1_amd64.deb`
- `libostree-1-1_2025.2-1~noble1_amd64.deb`
- `libostree-dev_2025.2-1~noble1_amd64.deb`
- `libostree-doc_2025.2-1~noble1_all.deb`
- `ostree-boot_2025.2-1~noble1_amd64.deb`
- `ostree-tests_2025.2-1~noble1_amd64.deb`
- `ostree_2025.2-1~noble1_amd64.deb`

### Installation Instructions

1. **Download or build the packages** using one of the methods above
2. **Install the packages**:
   ```bash
   sudo dpkg -i *.deb
   sudo apt --fix-broken install -y
   ```
3. **Verify installation**:
   ```bash
   dpkg -l | grep libostree
   pkg-config --modversion ostree-1
   ```

### Build Information

- **Source Version**: 2025.2-1 from Ubuntu Questing
- **Target Distribution**: Ubuntu Noble (24.04 LTS)
- **Build Date**: 2025-07-21 07:35:00 UTC
- **Build Status**: ✅ Success (packages built)
- **Registry Status**: 🔄 Upload issues being resolved
- **Packages Built**: 7 packages (main + debug symbols)

### Troubleshooting

If you encounter issues:

1. **Check package dependencies**: `sudo apt --fix-broken install -y`
2. **Verify libostree version**: `pkg-config --modversion ostree-1`
3. **Check for conflicts**: `dpkg -l | grep libostree`
4. **Reboot if needed**: Some applications may need a reboot to use the new libostree version

### Future Improvements

- 🔧 Fix Forgejo Package Registry upload issues
- 📦 Enable direct download links
- 🔄 Automate package distribution
- 📋 Improve build documentation

---

*This backport is required for bootc compatibility on Ubuntu Noble. See [bootc-deb repository](https://git.raines.xyz/robojerk/bootc-deb) for the bootc package that uses this libostree backport.*
EOF

echo -e "${GREEN}README.md updated with accurate build information!${NC}"
echo -e "${YELLOW}Note: Package registry uploads are currently having issues.${NC}"
echo -e "${BLUE}The README now includes:${NC}"
echo "  - Accurate status of build vs registry uploads"
echo "  - Multiple ways to get the packages"
echo "  - Local build instructions"
echo "  - Troubleshooting guide"
echo ""
echo -e "${BLUE}You can now commit and push this updated README to the repository.${NC}" 