#!/bin/sh

# Runs createrepo on the primary scripts server and copies the resulting
# metadata to the live yum repository.

set -eux

ssh='ssh -o GSSAPIAuthentication=yes -o GSSAPIDelegateCredentials=no'

yum=/afs/athena.mit.edu/contrib/scripts/yum-repos/rpm-fc$($ssh root@scripts.mit.edu 'lsb_release -rs')
[ -d "$yum" ]

tmp=$($ssh root@scripts.mit.edu "\
set -eux
tmp=\$(mktemp -dt createrepo.XXXXXX)
createrepo -d -o \"\$tmp\" -- '$yum' >&2
printf '%s' \"\$tmp\"
")
[ "$tmp" ]

new=$(mktemp -d -- "$yum/repodata.XXXXXX")
rsync -avz -e "$ssh" -- "root@scripts.mit.edu:$tmp/repodata/" "$new"
mv -b -T -- "$new" "$yum/repodata"
rm -rf -- "$yum/repodata~"

$ssh root@scripts.mit.edu "\
set -eux
rm -rf -- '$tmp'
"
