#!/bin/bash

REPO_URL=root@paratools08.rrp.net:/var/ftp/hpclinux/12.2

function expect_script
{
cat <<EOF
spawn rpm --resign $@
expect -exact "Enter pass phrase: "
send -- "${GPGPASS}\r"
expect eof
exit
EOF
}

# Get GPG passphrase
read -s -p "Enter passphrase: " GPGPASS
GPG="gpg --batch --passphrase=$GPGPASS"

# Check passphrase
tmpfile=`mktemp`
($GPG -a --sign $tmpfile && echo "Passphrase accepted." ) || exit 1
rm $tmpfile

# Rebuild repodata
rm -rf repodata
createrepo -d .

# Sign repo
pushd repodata
$GPG --list-key
$GPG -a --detach-sign repomd.xml || exit 1
$GPG -a --export 'ParaTools, Inc.' > repomd.xml.key || exit 1
popd

# Sign RPMS
cp repodata/repomd.xml.key RPM-GPG-KEY
expect_script noarch/*.rpm x86_64/*.rpm | /usr/bin/expect -f -

# Upload to hpclinux.com/repo/latest
rsync -rltDvzL --delete . $REPO_URL

