#!/bin/bash

#REPOSITORY_DIR=/mnt/miraculix/dist-mirrors
REPOSITORY_DIR=$(dirname $0)

REPOSITORIES="
redhat/rhel/4/os redhat/rhel/4/csgfs 
centos/4/csgfs
comoonics/all 
hp/all/x86_64 
mmg/productive"

ARCHS="i386 x86_64 ia64 noarch"

USAGE=1

function usage() {
	echo "$0 [-h|-v] [-d] [-r repositories [-a archs ]]"
   ## Usage
   # ----------------------------------------------------------

   cat <<EOF

   Options:

	-h: print this screen
	-v: print version information
	-d: use defaults
	-r: list of repositories e.g. "redhat/rhel/4/os redhat/rhel/4/csgfs"
	-a: list of archs e.g. "i386 x86_64 noarch"
EOF
   echo "defaults:"
   echo "  REPOSITORIES: $REPOSITORIES"
   echo "  ARCHS:        $ARCHS"
}


function version() {
        myrev=$(echo '$Revision: 1.2 $' | awk '{print $2}')
        mydate=$(echo '$Date: 2007/07/02 20:05:58 $' | awk '{print $2 " " $3}')
        echo "--------------------------------------------------"
        echo "Com.oonics serverless backup "
        echo "by ATIX GmbH 2004-2006"
        echo "http://www.atix.de"
        echo "info@atix.de"
        echo "--------------------------------------------------"
        echo "Version: $myrev"
        echo "Build: $mydate"
        echo "--------------------------------------------------"
}

umask 0002

while getopts ":r:a:dhv" Option
do
  case $Option in
        r) REPOSITORIES=$OPTARG; USAGE=0;;
       	a) ARCHS=$OPTARG; USAGE=0;;
	d) USAGE=0;;
        h) usage; exit 0;;
        v) version; exit 0;;
	*) usage; exit 1;;
  esac
done
shift $(($OPTIND - 1))


if  [ $USAGE -eq 1 ] ; then
	usage
	exit 1
fi

for repository in $REPOSITORIES; do
  for arch in $ARCHS SRPMS; do
    if [ -d $REPOSITORY_DIR/$repository/$arch ]; then
       echo -n "Updateing repository $repository in $arch.."
       createrepo $REPOSITORY_DIR/$repository/$arch &>/dev/null &&
       yum-arch -l $REPOSITORY_DIR/$repository/$arch &> /dev/null &&
       echo "(OK)" || echo "(FAILED)"
    fi
  done
done

