#!/bin/bash

# Add files to darcs if they match what's in a repo in a different dir

fail () { ret=$1; shift; echo -e "$@"; exit "${ret}"; }

Otherdir=${1?Need other dir}
[[ -d ${Otherdir} ]] || fail 2 "${Otherdir} is not a dir"
darcs status >/dev/null || fail 3 "Not in a darcs repository?"

find . -name '_*' -prune -o -type f -print0 | while read -d '' -r this
do
    other=${Otherdir}/${this#./}
    diff -q "${this}" "${other}" &>/dev/null
    ret=$?
    case $ret in
        0)      darcs add -q "${this}" && echo -e "A\t${this}";;
        1)      echo -e "-\t${this}" ;;
        *)      echo -e "E\t${this}" ;;
    esac
done
