#!/bin/sh
#
# Track changes in /etc/ to make it easy to undo changes or check modifications.

# Based on http://lists.debian.org/debian-devel/2005/02/msg00495.html

set -e

subdir=/etc
ignorefiles="adjtime ld.so.cache"


do_init() {
    # fail early (assume set -e) if the directory is missing
    cd $subdir

    # Install svk
    aptitude install svk
   
    # Initialize a depot in /root/.svk.  This is interactive.  Need to find a way to avoid question.
    # use expect?
#    svk depotmap --init
    expect -d -c 'spawn svk depotmap --init; send "y\r"'

    # Import $subdir making it a working copy
    svk import -m "Initial import." --to-checkout /$subdir $subdir
   
    # Make your depot not that readable by non-root
    chmod -R go-rwx ~/.svk

    tmpfile=/tmp/foo

    svk propget svn:ignore > $tmpfile

    # Remove volatile files from revision control
    for filename in $ignorefiles ; do
	svk rm -K $filename
	echo $filename >> $tmpfile
    done

    # Keep the ignore list sorted, and avoid empty lines
    sort -u < $tmpfile | grep -v '^$'> $tmpfile.new && mv $tmpfile.new $tmpfile

    # Hack to replace the old list with the new list.  the call will
    # be 'cp $tmpfile svk-edit-file', and thus replace instead of
    # editing.
    EDITOR="cp $tmpfile" svk propedit svn:ignore
    rm $tmpfile

    svk commit -m "Remove automatically updated files."
}

do_update() {
    cd $subdir
    svk commit -m "Recent changes."
}

case "$1" in 
    init)
	do_init
	;;
    update)
	do_update
	;;
    *)
	echo "error: incorrect command '$1'."
	exit 1
	;;
esac
