#!/bin/bash

OPT_RECURSIVE_=''

function printUsage()
{
    local prog_="`basename $0`"
    cat <<EOT
Usage: "$prog_" [OPTIONS] <directory>

$prog_ is a prog suitable for cron to verify a directory of mercurial repositories.

OPTIONS:
   -r         search recursive for mercurial repositories
EOT
}

while getopts "rh" opt; do
    case "$opt" in
        r) OPT_RECURSIVE_='-r' ;;
        h) printUsage; exit 0 ;;
        ?) printUsage; exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

DIR_=${1:-./}
cd "$DIR_"
DIR_="`pwd`"

for i in `reposearch -t hg $OPT_RECURSIVE_`; do
    cd "$i"
    out_="`hg verify 2>&1`"
    ret_=$?
    if [[ $ret_ != 0 ]]; then
        if [[ $i == '.' ]]; then
            i="$(basename `hg root`)"
        fi
        echo "${i}:"
        echo "$out_"
    fi
    cd "$DIR_"
done
