File boost-custom-update_check.sh of Package boost-custom

#!/bin/bash

# ==============================================================================
# Boost (Custom) Auto-Updater for OBS
# ==============================================================================
#
# 1. Fetches latest version from GitHub releases (boostorg/boost).
# 2. Updates 'Version' and 'Release' in the .spec file.
# 3. Triggers 'osc service manualrun download_files -v' to fetch sources.
# 4. Verifies the downloaded source binary.
# 5. Cleans up old sources.
#
# ==============================================================================

# --- Configuration ---
PACKAGE_NAME="boost-custom"
SOURCE_NAME="boost"
SPEC_FILE="${PACKAGE_NAME}.spec"

# GitHub "Latest" URL - simplest way to resolve the latest stable tag
# This redirects to https://github.com/boostorg/boost/releases/tag/boost-X.Y.Z
LATEST_RELEASE_URL="https://github.com/boostorg/boost/releases/latest"

# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# ------------------------------------------------------------------
# Step 1: Find the latest version from GitHub
# ------------------------------------------------------------------
echo -e "${BLUE}🔍 Checking GitHub for the latest Boost version...${NC}"

# We use curl to get the "Location" header of the latest release redirect.
# This avoids parsing complex HTML and is more robust than some API calls without tokens.
REDIRECT_URL=$(curl -Ls -o /dev/null -w %{url_effective} "$LATEST_RELEASE_URL")

# Extract the tag from the URL (e.g., .../tag/boost-1.90.0 -> boost-1.90.0)
LATEST_TAG=$(basename "$REDIRECT_URL")

# Extract the version number (remove 'boost-' prefix)
# e.g., boost-1.90.0 -> 1.90.0
NEW_VERSION=${LATEST_TAG#boost-}

# Validation: Check if it looks like a version number (X.Y.Z)
if [[ ! "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
    echo -e "${RED}❌ Error: Could not parse version from URL: $REDIRECT_URL${NC}"
    exit 1
fi

echo -e "${GREEN}✅ Latest Boost version detected: $NEW_VERSION (Tag: $LATEST_TAG)${NC}"

# ------------------------------------------------------------------
# Step 2: Check Local Spec File
# ------------------------------------------------------------------
if [ ! -f "$SPEC_FILE" ]; then
    echo -e "${RED}❌ Error: $SPEC_FILE not found.${NC}"
    exit 1
fi

# Extract current version from spec. Matches: Version:    1.89.0
CURRENT_VERSION=$(grep "^Version:" "$SPEC_FILE" | awk '{print $2}' | tr -d '[:space:]')
echo -e "${BLUE}â„šī¸  Current Spec version: $CURRENT_VERSION${NC}"

if [ "$NEW_VERSION" == "$CURRENT_VERSION" ]; then
    echo -e "${GREEN}🎉 System is up-to-date ($CURRENT_VERSION). No action needed.${NC}"
    exit 0
fi

# ------------------------------------------------------------------
# Step 3: Update Spec File
# ------------------------------------------------------------------
echo -e "${BLUE}🚀 Update required! Upgrading from $CURRENT_VERSION to $NEW_VERSION...${NC}"

# 1. Update Version
sed -i "s/^Version:[[:space:]]*.*/Version:        $NEW_VERSION/" "$SPEC_FILE"

# 2. Reset Release to 0
sed -i "s/^Release:[[:space:]]*.*/Release:        0/" "$SPEC_FILE"

echo -e "${GREEN}✅ Spec file updated.${NC}"

# ------------------------------------------------------------------
# Step 4: Run OSC Service to Download Files
# ------------------------------------------------------------------
echo -e "${BLUE}âŦ‡ī¸  Running 'osc service manualrun download_files -v'...${NC}"
echo -e "${BLUE}â„šī¸  (This uses the URLs defined in Source0/Source1 of the spec file)${NC}"

osc service manualrun download_files -v

if [ $? -ne 0 ]; then
    echo -e "${RED}❌ OSC Service run failed!${NC}"
    echo -e "${RED}   Check if the 'cmake' release artifacts exist for version $NEW_VERSION on GitHub.${NC}"
    exit 1
fi

# ------------------------------------------------------------------
# Step 5: Verify and Cleanup
# ------------------------------------------------------------------

# In the spec, Source0 ends with: #/%{source_name}-%{version}.tar.xz
# This instructs osc to rename the downloaded file.
# We expect: boost-1.90.0.tar.xz
EXPECTED_FILE="${SOURCE_NAME}-${NEW_VERSION}.tar.xz"

if [ ! -f "$EXPECTED_FILE" ]; then
    echo -e "${RED}❌ Error: Expected source file $EXPECTED_FILE was not found.${NC}"
    exit 1
fi

# Verify archive integrity (simple tar check)
if ! tar -tf "$EXPECTED_FILE" > /dev/null 2>&1; then
     echo -e "${RED}❌ Error: The downloaded file $EXPECTED_FILE appears corrupted.${NC}"
     exit 1
fi

echo -e "${GREEN}✅ Source file $EXPECTED_FILE verified.${NC}"

# Cleanup old source files (boost-*.tar.xz)
count=0
for f in ${SOURCE_NAME}-*.tar.xz; do
    if [ "$f" != "$EXPECTED_FILE" ] && [ -f "$f" ]; then
        echo -e "${BLUE}đŸ—‘ī¸  Removing old source: $f${NC}"
        rm "$f"
        count=$((count+1))
    fi
done

# Cleanup old sha256sum text files
# Spec defines Source1 renaming to: %{source_name}-%{version}.tar.xz.sha256sum.txt
EXPECTED_SHA="${SOURCE_NAME}-${NEW_VERSION}.tar.xz.sha256sum.txt"
for f in ${SOURCE_NAME}-*.tar.xz.sha256sum.txt; do
    if [ "$f" != "$EXPECTED_SHA" ] && [ -f "$f" ]; then
        echo -e "${BLUE}đŸ—‘ī¸  Removing old checksum file: $f${NC}"
        rm "$f"
        count=$((count+1))
    fi
done

if [ $count -eq 0 ]; then
    echo -e "${BLUE}â„šī¸  No old source files found to clean up.${NC}"
fi

echo -e "${GREEN}✨ Process complete! You can now run: osc addremove && osc ci${NC}"
openSUSE Build Service is sponsored by