#!/bin/bash

if [ ! -e ".snapshot" ]
then
    mkdir .snapshot
fi

if [ ! -d ".snapshot" ]
then
    echo ".snapshot is not a directory!!!"
    exit 1
fi

if [ ! -e ".snapshot/current" ]
then
    mkdir .snapshot/tmpsnapshot
    ln -s tmpsnapshot .snapshot/current
fi

if [ ! -e ".snapshot/comments" ]
then
    touch .snapshot/comments
fi


date=`date "+%Y-%b-%d_%H-%M-%S"`

echo $date $* >> .snapshot/comments

# arrrg, relative directories are relative to the destination...
# makes sense when you think about it but damn frustrating
rsync -aP --exclude ".snapshot/" --link-dest="../current" . .snapshot/snap_$date
rm .snapshot/current
ln -s snap_$date .snapshot/current

if [ -e ".snapshot/tmpsnapshot" ]
then
    rmdir .snapshot/tmpsnapshot
fi

