#!/bin/sh

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

set -eux

server="${1-scripts.mit.edu}"

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

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

tmp=$($ssh "root@$server" "\
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@$server:$tmp/repodata/" "$new"
mv -b -T -- "$new" "$yum/repodata"
rm -rf -- "$yum/repodata~"

$ssh "root@$server" "\
set -eux
rm -rf -- '$tmp'
"
