#! /bin/sh
# very simple version control
# 
# a version has a tree of files, a description of the changes since the
# last version, and a timestamp for when it was recorded.
# 
# The version database for a project in `pwd` is "`pwd`.changes".
#
# a project can have a "dontdiff" file in its database to limit the files 
# that are compared with diff
# 
# for more functionality, use diff and friends on the stuff in the
# "database", since stuff there is just regular files.
# 

b=`pwd`
b=`basename $b`

db="`pwd`/../$b.changes"
if test -d $db/trees; then
	new=no
else
	new=yes
fi
	
mkdir -p $db/trees
mkdir -p $db/desc
mkdir -p $db/ts
n=`ls $db/trees | wc -l`

if test "$new" = "yes" && test ! -r $db/dontdiff; then
	# create default dontdiff file
	cat > $db/dontdiff <<'EOF'
*~
EOF
fi

# you can override the diff command and its options in the environment
if test ! "$diff"; then
	diff=diff
	test -r $db/dontdiff && diff="$diff -X $db/dontdiff"
fi

case "$1" in
	rec)
		n=`expr $n + 1`
		cp -a . $db/trees/$n
		date +%Y%m%d-%H%M%S > $db/ts/$n
		${EDITOR:-vi} $db/desc/$n
		;;
	diff)
		shift
		if test $# = 2; then
			sh -c "cd $db/trees && $diff -uprN $1 $2"
		else
			$diff -uprN $db/trees/$n .
		fi
		;;
	log)
		i=$n
		while test $i -gt 0; do
			printf "%d\t%s\n\t%s\n" \
				$i "`cat $db/ts/$i`" "`sed 1q $db/desc/$i`"
			i=`expr $i - 1`
		done
		;;
	*)
		echo "usage: `basename $0` {rec|diff|log}" 1>&2
		exit 1
		;;
esac
