#!/bin/sh

RSYNC=/usr/local/bin/rsync
AWK=awk
COUNT=18

(echo;echo "** Maintaining $COUNT versions of backup."; echo)1>&2

# copy all but final arg (dest directory)
while [ $# -gt 1 ]
do
    ARGS="$ARGS $1"
    shift
done
cd "$1"

# hard link and partial
ARGS="$ARGS -lHy --partial"

# linkdest
for f in `echo "????-??-??-*"`
do
  ARGS="$ARGS --link-dest=../$f/"
done

# append dest directory with date
ARGS="$ARGS `date "+%Y-%m-%d-%H-%M-%S"`/"

# perform copy
echo $RSYNC $ARGS 1>&2
$RSYNC $ARGS

# clean up
MOD=$[ `date +%j` % $COUNT]
while [ `/bin/ls | wc -l` -gt $COUNT ]
do
    ls |
    $AWK 'NR=='$MOD' {print "(echo;echo \"** Deleting "$0".\"; echo) 1>&2; chmod -R u+xwr "$0"; rm -rf "$0}' |
    bash
done
