#!/bin/bash

RS=/opt/local/bin/rsnapshot
CONF="/Users/huston/.rsnapshot/rsnapshot.conf"
FLAGS="-V"

usage() {
  echo " "
  echo "$0 - run rsnapshot backups"
  echo " "
  echo "Usage: $0 [OPTION]..."
  echo " monthly, m   Monthly snapshot"
  echo " weekly, w    Weekly snapshot"
  echo " daily, d     Daily snapshot"
  echo " hourly, h    Hourly snapshot"
  echo " sync, s      Sync - copy data, do not rotate snapshots"
  echo " tm           Use Time Machine data as source"
  echo " "
  exit 0
}

if [ $# -eq 0 ] ; then
  usage
fi

while [ $# -gt 0 ] ; do
  case "$1" in
    tm)
      CONF="/Users/huston/.rsnapshot/rsnapshot-tm.conf"
      shift
      ;;
    monthly|m)
      do_monthly=1
      shift
      ;;
    weekly|w)
      do_weekly=1
      shift
      ;;
    daily|d)
      do_daily=1
      shift
      ;;
    hourly|h)
      do_hourly=1
      shift
      ;;
    sync|s)
      do_sync=1
      shift
      ;;
    *)
      echo "Unknown option $1"
      usage
      exit 255
  esac
done

FLAGS="$FLAGS -c $CONF"

START=`date`

if [ $do_monthly ] ; then
  date
  echo " "
  $RS $FLAGS monthly || exit
fi

if [ $do_weekly ] ; then
  date
  echo " "
  $RS $FLAGS weekly || exit
fi

if [ $do_daily ] ; then
  date
  echo " "
  $RS $FLAGS daily || exit
fi

if [ $do_sync ] ; then
  $RS $FLAGS sync || exit
fi

if [ $do_hourly ] ; then
  date
  echo " "
  $RS $FLAGS hourly || exit
fi

echo " "
echo Started $START
echo Ended   `date`
