#!/bin/bash -e

[ \! -t 1 ] && mono=1

# read args, split by :

n=0
while [ "$1" != : -a $# -gt 0 ]
do
        CMD[$n]=$1
        n=$(($n + 1))
        shift
done
if [ "$1" != : ]
then
        echo usage: `basename $0` command : dir ... >&2
        exit 1
fi
shift

col () 
{ 
    c=$1;
    con "$c";
    shift;
    echo -n "$@";
    coff "$c"
}

con () 
{ 
    local c=$1;
    if [ -z "$mono" -a -n "$c" ]; then echo -n '[1;3'$1'm'; fi
}

coff () 
{ 
    c=$1;
    if [ -z "$mono" -a \( $# = 0 -o -n "$c" \) ]; then echo -n '[0m'; fi
}

for A; do
	if [ \! "$quiet" = 1 ]; then
		col 2 " $A"; echo
	fi
	pushd "$A" >/dev/null
	ROOT="`darcs_root`" || true
	if [ -z "$ROOT" -a "$init" = 1 ]; then
		ROOT=.
	fi
	if [ -n "$ROOT" ]; then
		cd "`p "$ROOT"`"
	fi
	if [ -n "$prefix" ]; then
		"${CMD[@]}" | prepend "$A"$'\t'
	else
		"${CMD[@]}"
	fi
	popd >/dev/null
	if [ \! "$quiet" = 1 ]; then
		echo
	fi
done
