#!/bin/sh
#
# add a file to the viwl CVS repository
#


# check for config
if test -e "/etc/viwl.conf"; then
    # use global config
    . /etc/viwl.conf
fi

if test -e "~/.viwl"; then
    # use personal config
    . ~/.viwl
fi

file=$1

if test "x$file" = "x"; then
    echo "usage: $0 <file to checkin>"
fi

if test -d "$file"; then
    echo "argument must be a file!"
    exit
fi


dir=`dirname $file`
file=`basename $file`

#
# check if $dir is absolute or relative, make it absolute
# if it is not
absolute=`echo $dir | grep "^/"`
if test "x$dir" = "x."; then
    dir=""
fi

if test "x$absolute" = "x"; then
    # no, relative
    dir="`pwd`$dir"
fi

#
# remove leading slash
dir=`echo $dir | sed 's/^\///'`

# add dir to repository, if needed
CUR_DIR="$WORK_DIR"
perl -e '$_=shift; s/^\///; print join "\n", split /\//; print "\n"' "$dir" | while read subdir; do
    if test ! -d "$CUR_DIR/$subdir"; then
	# subdir never checked in, do this now
	mkdir "$CUR_DIR/$subdir"
	cvs -d $CVS_ROOT add "$CUR_DIR/$subdir"
    fi
    CUR_DIR="$CUR_DIR/$subdir"
done

if test -e "$WORK_DIR/$dir/$file"; then
    echo "File $file is already checked in"
    exit
fi

#
# copy our file to the repository
cp -a "/$dir/$file" "$WORK_DIR/$dir/"

#
# check it in
cvs -d $CVS_ROOT add "$WORK_DIR/$dir/$file"

#
# and commit it
cvs -d $CVS_ROOT commit -m initial "$WORK_DIR/$dir/$file"

#
# done, report it
echo "/$dir/$file will now maintained via CVS"