#!/bin/sh

set -e

MOCK_CONFIGS="fc3-i386 fc3-x86_64 fc4-i386 fc4-x86_64 fc5-i386 fc5-x86_64 fedora-development-i386 fedora-development-x86_64 rhel3-i386 rhel3-x86_64 rhel4-i386 rhel4-x86_64 sles9-i386 sles9-x86_64 sles10-i386 sles10-x86_64"

# CHANGME: 
REPO_DIR=~/repo/software/
REPO_HOST=servername.domain.com

# CHANGME: 
SSH_KEY="-i /home/user/.ssh/id_dsa"
export RSYNC_RSH="ssh -x -a -l user $SSH_KEY"

CURDIR=$(cd $(dirname $0); pwd)

# uncomment if you are running squid
#export http_proxy=http://localhost:3128/
ext=some_ext
DEBUG=0
MOCKDEBUG=
OUTPUT_DIR=

help() {
    echo "mockbuild.sh [-p http_proxy] [-e mock_unique_ext] [-d] FILE.src.rpm"
    exit 1
}

while getopts "p:c:e:do:" Option
do
  case $Option in
      p) 
        export http_proxy=$OPTARG 
        ;;
      c)
        MOCK_CONFIGS="$OPTARG"
        ;;
      e)
        ext=$OPTARG
        ;;
      d)
        DEBUG=1
        MOCKDEBUG=--debug
        set -x
        ;;
      o)
        OUTPUT_DIR=$OPTARG
        ;;
      *) 
        help
        ;;
  esac
done
shift $(($OPTIND - 1))
# Move argument pointer to next.


if [ -z "$PASSPHRASE" ]; then
    echo "Must supply passphrase."
    exit 1
fi

# default promotion if no args provided
PROMOTE=$*
if [ -z "$PROMOTE" ]; then
    PROMOTE=$CURDIR/../../autobuild/srpms/*.src.rpm 
fi

TEMP_DIR=$(mktemp -d /tmp/promote-$$-$RANDOM-XXXXXX )
if [ $? -ne 0 ]; then
    echo "Could not create secure temporary directory to work in. Exiting..."
    exit 1
fi
trap "rm -rf $TEMP_DIR" QUIT EXIT TERM HUP

if [ -z "$OUTPUT_DIR" ]; then
    OUTPUT_DIR=$TEMP_DIR/repo/
fi

# copy srpms to dir
cp $PROMOTE $TEMP_DIR

# run mock build for each config. Run a few in parallel.
max_parallel=3
for i in $MOCK_CONFIGS
do
    echo "Building RPMS for config: $i"
    outdir=$OUTPUT_DIR/${i%-*}
    mkdir -p $outdir/{RPMS,SRPMS} 2>/dev/null || true
    mock -r $i $MOCKDEBUG --uniqueext=$ext --resultdir=$outdir/RPMS $TEMP_DIR/*.src.rpm --autocache &
    printed=0
    echo "    There are $(jobs | wc -l) children."
    while [ $( jobs | wc -l ) -ge $max_parallel ]; do 
        [ $printed -eq 0 ] && echo "    waiting for some process to finish..."
        printed=1
        sleep 1 
    done
done

echo "All build processes started. Waiting for build processes to finish."
wait

# clean mock build roots -- in background...
for i in $MOCK_CONFIGS
do
    echo "Cleaning buildroot for config: $i"
    mock -r $i --uniqueext=$ext clean &
    printed=0
    echo "    There are $(jobs | wc -l) children."
    while [ $( jobs | wc -l ) -ge $max_parallel ]; do 
        [ $printed -eq 0 ] && echo "    waiting for some process to finish..."
        printed=1
        sleep 1 
    done
done &

## move srpms and sign all RPMS
for i in $MOCK_CONFIGS
do
    outdir=$OUTPUT_DIR/${i%-*}
    # other config may have moved it, so don't fail here...
    mv $outdir/RPMS/*.src.rpm $outdir/SRPMS 2>/dev/null || true

    for k in $outdir/*/*.rpm
    do
        if ! rpm -K $k | grep -qi gpg; then
            expect -f - -- $k <<-'EOF'
                set timeout -1
                spawn sh -c "rpm --define '_signature gpg' --define '_gpg_name libsmbios' --addsign $argv"
                match_max 100000
                expect -exact "Enter pass phrase: "
                send -- "$env(PASSPHRASE)\r"
                expect eof
EOF
        fi
    done
done

# copy real repo in. This overwrites any rpms whose version did not change.
rsync -avz $REPO_HOST:$REPO_DIR/. $OUTPUT_DIR

## yummify it
for i in $MOCK_CONFIGS
do
    echo "Refreshing metadata for directory: $i"
    outdir=$OUTPUT_DIR/${i%-*}
    [ -d $outdir/RPMS/ ] || continue
    find $outdir -depth -name .olddata -exec rm -rf {} \;
    if which repomanage > /dev/null 2>&1 ; then
        repomanage --old $outdir | xargs -r rm 
    fi
    yum-arch $outdir
    createrepo $outdir
    if which repoview > /dev/null 2>&1 ; then
        repoview -q $outdir
    fi
done

# sync back to real repo
rsync --delete -zv -rlt $OUTPUT_DIR $REPO_HOST:$REPO_DIR/.
