#!/bin/bash
#
# This helper script adds a package to SVN repository
# and sets svn:ignore property to the content of the
# .cvsignore existing in the given tree.
# Unless the --keep option was given, the .cvsignore
# files are removed from "svn add" set and deleted.
#
# Marius Tomaschewski <mt@suse.de>
#
# $Id: checkin-svn 32 2005-02-07 16:12:08Z schubi $

self=`basename $0`

function usage ()
{
    echo "Usage: ${self} [--test|--show] [--keep] <package directory>"
    echo "Options:"
    echo "  --test | --show   dry run, only show commands"
    echo "  --keep            do not delete .cvsignore files"
    echo "  --help | -h       show this message"
}

if ! svn    --version &>/dev/null ; then
    echo "ERROR: Unable to find the 'svn' subversion command line client!" >&2
    echo "       Please install the subversion package and try again."     >&2
    exit 1
fi
if ! find   --version &>/dev/null ; then
    echo "ERROR: Unable to find the (GNU) 'find' utility!"                 >&2
    echo "       Please install the findutils package and try again."      >&2
    exit 1
fi

show=0
keep=0
while test $# -gt 0 ; do
    case "$1" in
    --test|--show)
	show=1
	shift
    ;;
    --keep)
	keep=1
    	shift
    ;;
    -h|--help)
	usage
	exit 0
    ;;
    -*)
	echo "${self}: invalid option '$1'" >&2
	usage >&2
	exit 1
    ;;
    *)
	break
    ;;
    esac
done

function run ()
{
    test $show -ne 0 && echo $@ || $@
}

pkg_root="$1"
if test -z "${pkg_root}" ; then
    echo "${self}: package directory parameter missed" >&2
    usage >&2
    exit 1
fi
if test ! -d "${pkg_root}" ; then
    echo "${self}: package directory '$1' does not exists" >&2
    exit 1
fi

#
# let's dance...
#
run svn add "${pkg_root}" && \
for fname in $(find "${pkg_root}" -type f -name .cvsignore) ; do
    dname=${fname%.cvsignore}
    run svn propset svn:ignore -F "${fname}" "${dname}" && {
        if test $keep -eq 0 ; then
            run svn revert "${fname}" && \
	    run rm -f "${fname}" || exit 1
        fi
    } || exit 1
done

if test $show -eq 0 ; then
    echo ""
    echo "Package ${pkg_root} added to svn repository - commit using:"
    echo "svn checkin ${pkg_root}"
fi

# EOF
