#!/bin/bash

# Cron script for "release".  See "backend/nitemc/crontab.txt"

RSYNC_DIR="/usr/local/spamassassin/automc/rsync/tagged_builds"
REPO_URL="http://svn.apache.org/repos/asf/spamassassin/trunk"
PERL=/usr/bin/perl
SVN=/usr/bin/svn


type="$1"
versfile="$2"

if [ -z "$type" ]; then
  echo "Type not specified" >&2
  exit 1
fi

if [ ! -f $versfile ]; then
  echo "Version file not specified" >&2
  exit 1
fi

subdir=${type}_mass_check

echo "Type: $type"
echo "Version File: $versfile"
echo "Subdir: $subdir"

REV=`tail -1 $versfile | awk '{print $2}'`

echo "Revision: $REV"

set -ex

for retry in 1 2 3 4 5 6 7 8 9 10 ; do

    # sleep on retries
    if [ $retry -gt 1 ]; then sleep 60; fi

    # export the main repo: (through a temporary dir)
    rm -rf $RSYNC_DIR/${subdir}.tmp
    mkdir $RSYNC_DIR/${subdir}.tmp || continue
    $SVN export --non-interactive --force -r $REV $REPO_URL $RSYNC_DIR/${subdir}.tmp/ \
            || continue

    cd $RSYNC_DIR/${subdir}.tmp
    $PERL build/mkrules --out rules > /dev/null || continue
    # create "svn info" file for mass-check
    $SVN info $REPO_URL > masses/svninfo.tmp < /dev/null || continue

    cd $RSYNC_DIR
    # Success, only now install directory in place
    rm -rf $RSYNC_DIR/${subdir}.old &&
    mv -f $RSYNC_DIR/${subdir} $RSYNC_DIR/${subdir}.old &&
    mv -f $RSYNC_DIR/${subdir}.tmp $RSYNC_DIR/${subdir} &&
    exit
done

echo "FAILED to extract to rsync dir" 1>&2
exit 2

