File build-vendor.sh of Package ghostty

#!/usr/bin/env bash
set -euo pipefail

# This script builds the vendor archive for ghostty
# Run from the ghostty package directory

echo "Building vendor archive for ghostty..."

# Check if _service file exists
if [ ! -f "_service" ]; then
  echo "Error: _service file not found"
  exit 1
fi

# Parse _service file to get download parameters
echo "Parsing _service file..."

# Function to download a file from _service
download_from_service() {
  local service_num=$1
  local protocol=$(grep -oP '(?<=<param name="protocol">)[^<]+' _service | sed -n "${service_num}p")
  local host=$(grep -oP '(?<=<param name="host">)[^<]+' _service | sed -n "${service_num}p")
  local url_path=$(grep -oP '(?<=<param name="path">)[^<]+' _service | sed -n "${service_num}p")
  
  local download_url="${protocol}://${host}${url_path}"
  local filename="${url_path##*/}"
  
  echo "Download URL: $download_url"
  echo "Filename: $filename"
  
  if [ -f "$filename" ]; then
    echo "✓ File already exists: $filename"
    return 0
  fi
  
  echo "Downloading $filename..."
  curl -fL --retry 5 --retry-delay 1 --connect-timeout 10 --max-time 300 \
    -o "$filename" "$download_url"
  
  if [ ! -f "$filename" ]; then
    echo "Error: Download failed for $filename"
    return 1
  fi
  
  echo "✓ Downloaded: $(ls -lh "$filename" | awk '{print $5}')"
}

# Download ghostty source (first service)
download_from_service 1

# Download Zig compiler x86_64 (second service)
download_from_service 2

# Download Zig compiler aarch64 (third service)
download_from_service 3

# Find the ghostty source filename
GHOSTTY_FILENAME=$(grep -oP '(?<=<param name="path">)[^<]+' _service | head -1)
GHOSTTY_FILENAME="${GHOSTTY_FILENAME##*/}"

# Extract the tarball first
echo "Extracting source tarball..."
tar -xzf "$GHOSTTY_FILENAME"

# Find the extracted source directory
echo "Finding extracted source directory..."
SOURCE_DIR=$(find . -maxdepth 1 -type d -name "ghostty-*" -print -quit)

if [ -z "$SOURCE_DIR" ]; then
  echo "Error: Could not find ghostty-* directory after extraction"
  echo "Contents:"
  ls -la
  exit 1
fi

# Remove leading ./ from find output
SOURCE_DIR="${SOURCE_DIR#./}"
echo "Found source directory: $SOURCE_DIR"

echo "Found source directory: $SOURCE_DIR"
cd "$SOURCE_DIR"

# Run the make-vendor script
if [ ! -f "../make-vendor.sh" ]; then
  echo "Error: make-vendor.sh not found"
  exit 1
fi

echo "Running make-vendor.sh to build vendor archive..."
# First, pre-download the correct themes file as a workaround
echo "Pre-downloading correct ghostty-themes.tgz..."
mkdir -p vendor
CORRECT_THEMES_URL="https://github.com/mbadolato/iTerm2-Color-Schemes/releases/download/release-20251020-150521-589c0ea/ghostty-themes.tgz"
if curl -fL --retry 3 --retry-delay 1 -o vendor/ghostty-themes.tgz "$CORRECT_THEMES_URL"; then
  echo "✓ Downloaded correct themes file"
else
  echo "⚠ Warning: Could not download themes file (non-fatal)"
fi

# Download libvaxis (git dependencies not handled by make-vendor.sh)
echo "Pre-downloading libvaxis..."
LIBVAXIS_URL="https://github.com/rockorager/libvaxis/archive/1f41c121e8fc153d9ce8c6eb64b2bbab68ad7d23.tar.gz"
if curl -fL --retry 3 --retry-delay 1 -o vendor/1f41c121e8fc153d9ce8c6eb64b2bbab68ad7d23.tar.gz "$LIBVAXIS_URL"; then
  echo "✓ Downloaded libvaxis"
else
  echo "❌ Error: Could not download libvaxis (required)"
  exit 1
fi

# Download zigimg (libvaxis dependency)
echo "Pre-downloading zigimg..."
ZIGIMG_URL="https://github.com/TUSF/zigimg/archive/31268548fe3276c0e95f318a6c0d2ab10565b58d.tar.gz"
if curl -fL --retry 3 --retry-delay 1 -o vendor/31268548fe3276c0e95f318a6c0d2ab10565b58d.tar.gz "$ZIGIMG_URL"; then
  echo "✓ Downloaded zigimg"
else
  echo "❌ Error: Could not download zigimg (required)"
  exit 1
fi

# Download zg (libvaxis dependency)
echo "Pre-downloading zg..."
ZG_URL="https://codeberg.org/atman/zg/archive/4a002763419a34d61dcbb1f415821b83b9bf8ddc.tar.gz"
if curl -fL --retry 3 --retry-delay 1 -o vendor/4a002763419a34d61dcbb1f415821b83b9bf8ddc.tar.gz "$ZG_URL"; then
  echo "✓ Downloaded zg"
else
  echo "❌ Error: Could not download zg (required)"
  exit 1
fi

# Run make-vendor.sh (will skip already downloaded files)
bash ../make-vendor.sh

# Check if vendor.tar.zst was created
if [ ! -f "vendor.tar.zst" ]; then
  echo "Error: vendor.tar.zst not created"
  exit 1
fi

echo "✓ vendor.tar.zst created: $(ls -lh vendor.tar.zst | awk '{print $5}')"

# Move vendor archive to parent directory
mv vendor.tar.zst ../

cd ..
echo ""
echo "✅ Vendor archive build complete!"
ls -lh vendor.tar.zst
openSUSE Build Service is sponsored by