#!/bin/bash

##
# Constants
##
ABS_VERSION="%%ABS_VERSION%%"

##
# Configurable params (via cmdline)
##
# Location of the CVS repo with PKGBUILDs
CVS_REPO_LOCATION=/home/cvs-community
# Directory where the rsync-able ABS tree is stored
ABS_DIR=/home/abs/rsync

function usage()
{
	echo "Usage: $0 [abs dir] [cvs repo location]"
	echo "       $0 (-v | --version)"
	echo "       $0 (-h | --help)"
	echo 
	echo "Takes community cvs repo structure of form \$repo/\$group/\$pkgname"
  echo "with CVS tags CURRENT and CURRENT-64 to indicate architecture, and"
	echo "converts it to the ABS structure of \$arch/\$repo/\$group/\$pkgname"
	echo "then syncs ABS tree in $ABS_DIR with extracted structure."
	echo
	echo "Options:"
	echo "   -h | --help - show this message"
	echo "   -v | --version - show version information"
	echo
	echo "Parameters:"
	echo "   [abs dir] - dir containing abs tree to update. Defaults to"
	echo "               $ABS_DIR"
	echo "   [cvs repo location] - URL to cvs repo containing PKGBUILDs"
}

version() {
	echo "community2abs $ABS_VERSION"
	echo
	echo "Copyright (C) 2008 Aaron Griffin <aaron@archlinux.org>."
	echo
	echo "This is free software; see the source for copying conditions."
	echo "There is NO WARRANTY, to the extent permitted by law."
}

##
# Simple params
##
if [ "$1" = "-h" -o "$1" = "--help" ]; then
	usage
	exit 0
fi

if [ "$1" = "-v" -o "$1" = "--version" ]; then
	version
	exit 0
fi

if [ "$1" != "" ]; then
	ABS_DIR=$1
fi

if [ "$2" != "" ]; then
	CVS_REPO_LOCATION=$2
fi

##
# Check config
##
if [ "$ABS_DIR" = "" ]; then
	echo "No ABS_DIR specified!"
	exit 1
elif [ ! -d "$ABS_DIR" ]; then
	echo "ABS_DIR not a directory."
	exit 1
fi

if [ "$CVS_REPO_LOCATION" = "" ]; then
	echo "No CVS repo location specified!"
	exit 1
fi

##
# BEGIN!
##
cd $ABS_DIR
ABS_DIR=$(pwd)

for arch in i686 x86_64; do
    if [ ! -d $ABS_DIR/$arch ]; then
      mkdir $ABS_DIR/$arch
    fi
    cd $ABS_DIR/$arch
    if [ -d community ]; then
        cd community
        cvs -q up
    else
        if [ "$arch" = "i686" ]; then
            cvstag=CURRENT
        else
            cvstag=CURRENT-64
        fi
        cvs -d${CVS_REPO_LOCATION} -q checkout -r $cvstag community
    fi
done

