#!/bin/sh
# Filename:      hg-snapshot-script
# Purpose:       automatically track changed files using mercurial
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2 or any later version.
################################################################################
# Basic instructions:
# -------------------
# cd /etc
# hg init
# chmod og-rwx .hg
#
# cat > .hgignore << EOF
# syntax: regexp
# (^)*.dpkg-new
# (^)*.dpkg-old
# (^)blkid.tab(|.old)
# (^)mtab
# (^)adjtime
# \..*.swp
# # add other files if necessary, depends on your setup...
# EOF
#
# cat >> /etc/apt/apt.conf << EOF
# DPkg {
#   Pre-Invoke  {"cd /etc ; ./apt/hg-snapshot-script pre";};
#   Post-Invoke {"cd /etc ; ./apt/hg-snapshot-script post";};
# }
# EOF
#
# hg add
# hg ci -m "initial checkin"
#
# See http://michael-prokop.at/blog/2007/03/14/maintain-etc-with-mercurial-on-debian/
# for more details....
################################################################################

set -e

caller=$(ps axww | mawk '/aptitude|apt-get/ {for (i=5; i<=NF ; i++) printf ("%s ",$i); printf ("\n") }' | head -1)

hg addremove 1>/dev/null
STATUS="$(hg st)"

if [ -z "$STATUS" ] ; then
   echo "hg-snapshot-script: nothing to be done"
else
   case "$1" in
        pre)
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME before: $caller"
          ;;
        post)
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME after: $caller"
          ;;
	*)
           echo "hg-snapshot-script: found changed files:"
           hg st
           hg ci -m "snapshot from $LOGNAME on $(date '+%Y-%m-%d - %H:%M:%S')"
	  ;;
   esac
fi

## END OF FILE #################################################################
