#!/bin/bash

PROG_NAME_="`basename $0`"
DEF_SRC_="http://hg.grml.org"

function printUsage
{
    cat <<EOT
Usage: $PROG_NAME_ <url / repository>"

$PROG_NAME_ is a script to clone upstream packages using mq from a mirror.
Default repository source: $DEF_SRC_
EOT
    exit 1
}

if (( $# != 1 )); then
    echo "W: give the repository you want to clone"
    printUsage
fi

NAME_="`basename $1`"
DURL_="`dirname $1`"
if [[ $DURL_ == "." ]]; then
    DURL_="$DEF_SRC_"
fi
URL_="${DURL_}/${NAME_}"

# clone source
if [ ! -d "$NAME_" ]; then
    hg clone ${URL_} || exit 1
fi

# clone possibel mq
output_="`hg clone ${URL_}.mq 2>&1`"
ret_=$?
if [[ $ret_ != "0" ]]; then
    if echo "$output_" |grep -q "does not appear to be an hg repository!$"; then
        exit 0
    fi
    echo "$output_" 1>&2
    exit $ret_
fi
ln -s ../../${NAME_}.mq ${NAME_}/.hg/patches || exit 1

# vim: filetype=sh
