#! /bin/bash

if [[ "$1" == "" ]]
then
    cat >&2 <<EOF
Usage:
	$0 directory/file

Add 'file' to the svn:ignores list for 'directory'.
'directory/file' must exist.
Also alphabetizes the svn:ignores list and removes blank lines, and outputs
the list as amended.
EOF
    exit 1
fi

function process_file () {
    # Separate the directory and file.
    DIR=${1%/*}
    FILE=${1##*/}

    if [[ "$1" == "$DIR" ]]
    then
	echo 2>&1 "Argument must contain slash: $1"
	status=1
	return
    fi

    if [[ "$DIR" == "" || "$FILE" == "" ]]
    then
	echo 2>&1 "Both file and directory must be non-null: $1"
	status=1
	return
    fi

    # Do the work.
    ( echo $FILE
      svn propget svn:ignore $DIR ) |
    sed -e '/^$/d' |
    sort -u |
    tee /dev/fd/3 |
    svn propset svn:ignore -F /dev/stdin $DIR
}

# Set fd 3 to stdout.
exec 3>&1

status=0

for F in "$@"
do
    process_file "$F"
done


