File telegram-update_check.sh of Package telegram

#!/bin/bash

# ==============================================================================
# Telegram Desktop (tdesktop) Auto-Updater for OBS
# ==============================================================================
#
# 1. Checks GitHub API for the latest 'tdesktop' release tag.
# 2. Updates the .spec file version.
# 3. Triggers 'osc service manualrun download_files' to fetch ALL sources
#    (Source0, Source1, etc.) defined in the spec file.
# 4. Verifies the main source tarball.
# 5. Cleans up old source tarballs.
#
# ==============================================================================

# --- Configuration ---
PACKAGE_NAME="telegram"
SPEC_FILE="${PACKAGE_NAME}.spec"
GITHUB_REPO="telegramdesktop/tdesktop"

# User Agent for GitHub API checks
USER_AGENT="Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"

# 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 Telegram Desktop version...${NC}"

# Get the latest tag from GitHub API
LATEST_TAG=$(curl -s -H "User-Agent: $USER_AGENT" "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')

# Sanitize version: Remove 'v' prefix if present (e.g., v6.3.3 -> 6.3.3)
NEW_VERSION=$(echo "$LATEST_TAG" | sed 's/^v//')

if [ -z "$NEW_VERSION" ]; then
    echo -e "${RED}❌ Error: Could not retrieve latest version from GitHub.${NC}"
    exit 1
fi

echo -e "${GREEN}✅ Latest GitHub Release: $NEW_VERSION${NC}"

# ------------------------------------------------------------------
# Step 2: Check Local Spec File
# ------------------------------------------------------------------
if [ ! -f "$SPEC_FILE" ]; then
    echo -e "${RED}âš ī¸  Warning: $SPEC_FILE not found. (Expected file: $SPEC_FILE)${NC}"
    CURRENT_VERSION="none"
else
    # Extract current version from spec (Matches "Version:        6.3.3")
    CURRENT_VERSION=$(grep "^Version:" "$SPEC_FILE" | awk '{print $2}' | tr -d '[:space:]')
    echo -e "${BLUE}â„šī¸  Current OBS version: $CURRENT_VERSION${NC}"
fi

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

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

if [ -f "$SPEC_FILE" ]; then
    echo -e "${BLUE}📝 Updating $SPEC_FILE...${NC}"

    # 1. Update Version in spec
    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}"
fi

# ------------------------------------------------------------------
# Step 4: Run OSC Service to Download Files
# ------------------------------------------------------------------
echo -e "${BLUE}âŦ‡ī¸  Running 'osc service manualrun download_files -v'...${NC}"

# This command parses the updated spec file and downloads Source0, Source1, Source2, etc.
osc service manualrun download_files -v

if [ $? -ne 0 ]; then
    echo -e "${RED}❌ OSC Service run failed! Check your internet connection or URL validity.${NC}"
    # Optionally revert spec file here if desired, but usually manual fix is better.
    exit 1
fi

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

# The spec defines Source0 as: ...#/%{name}-%{version}.tar.xz
# So the file on disk should be: telegram-VERSION.tar.xz
FILENAME="${PACKAGE_NAME}-${NEW_VERSION}.tar.xz"

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

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

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

# Cleanup old source files (telegram-*.tar.xz)
# This removes previous versions while keeping the new one
count=0
for f in ${PACKAGE_NAME}-*.tar.xz; do
    if [ "$f" != "$FILENAME" ] && [ -f "$f" ]; then
        echo -e "${BLUE}đŸ—‘ī¸  Removing old source: $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