#!/bin/bash
# Coding assistance from Claude Code
#
# Build the stanford-server RPM.  Run this script from the top-level
# directory of the stanford-server repository (the directory that
# contains the redhat2/ directory).

set -e

PACKAGE=stanford-server
VERSION=179

if [ ! -d redhat2 ]; then
    echo "Error: redhat2/ directory not found." >&2
    echo "Run this script from the top-level repository directory." >&2
    exit 1
fi

if [ ! -f usr/sbin/key-system ]; then
    echo "Error: usr/sbin/key-system not found." >&2
    echo "Run this script from the top-level repository directory." >&2
    exit 1
fi

rm -rf ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

# Create the source tarball from the repository contents.
tar czf ~/rpmbuild/SOURCES/${PACKAGE}-${VERSION}.tar.gz \
    --transform="s,^,${PACKAGE}-${VERSION}/," \
    --exclude=.git \
    --exclude=debian \
    --exclude=redhat \
    --exclude=redhat2 \
    --exclude=t \
    --exclude='*.tar.gz' \
    usr/ etc/ set-version.sh

cp redhat2/LICENSE ~/rpmbuild/SOURCES/
cp redhat2/${PACKAGE}.spec ~/rpmbuild/SPECS/

rpmbuild -ba ~/rpmbuild/SPECS/${PACKAGE}.spec
