#!/bin/sh

ME=`whoami`
if [ "$ME" != "root" ]; then
    echo "executing sudo $0 $*"
    exec sudo $0 $*
fi

echo "simple.be snapshot backup 2.0.3 for mac"
echo "make sure external disk is mounted"
echo "then press any key to continue .."
echo
read -n1 continue


stamp=`date -u "+%Y-%m-%d-%H%M%SZ"`
diskname="external"
hostname=`hostname -s`
mountpoint="/Volumes/$diskname"
where="$mountpoint/backups/$hostname"
new="$where/backup.new"
zero="$where/backup.0"

if [ ! -d $mountpoint ]; then
echo "external disk volume is not mounted"
read continue
exit
fi

if [ ! -d $where ]; then
echo "backups folder does not exist"
read continue
exit
fi

cd $where
echo "starting snapshot backup $stamp"
umask 022

################# the command ##############

echo zero is $zero

nice -n 14 rsync -avH --delete --link-dest=$zero /Users /System/Library/Fonts $new

## echo "If the backup looks good, hit any key to continue."
## read continue

if [ -d $new ]; then
    
    echo "new backup exists"
    echo $stamp > $new/.timestamp

    echo "rotating old backups"
    mv backup.3 backup.4
    mv backup.2 backup.3
    mv backup.1 backup.2
    mv backup.0 backup.1
    mv backup.new backup.0
    nice -n 14 rm -rf backup.4

else
    echo "backup.new does not exist"
fi

echo "snapshot backup complete"
echo "hit Apple-Q to quit terminal"

