#!/bin/bash
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ ! -d $mydir ] ; then echo "mydir !" ; exit 4 ; fi

configfile="$mydir/spm_hardlinksave_config.sh"

if [ -f "$configfile" ]
then
	. "$configfile"
else
	echo "No config file find :("
	exit 4
fi

# should run "just" after 0h01 am
# so, “today is yesterday” here
#
TODAY_FORTOUCH="$(date -d yesterday +%Y%m%d2359)"
TODAY_DAYOFWEEK="$(date -d yesterday +%u)"
TODAY_FULLYEAR="$(date -d yesterday +%Y)"
TODAY_MONTH="$(date -d yesterday +%m)"
TODAY_DAYOFMONTH="$(date -d yesterday +%d)"


if_exists_mv ()
{
	[ -e "$2" ] && echo "if_exists_mv destination yet exists" && exit 2
	[ -e "$1" ] && mv "$1" "$2"
}

if_exists_rm ()
{
	[ -e "$1" ] && rm -rf "$1"
}


rotation_months ()
{
	if [ "$TODAY_MONTH" == "01" ] # first moth of the year ?
	then
		local fullyear="$TODAY_FULLYEAR"
		(( fullyear -= 2 ))
		if_exists_mv "$HLSAVES_DIR/11_$MONTHSlabel" "$HLSAVES_DIR/$fullyear"
	else
		if_exists_rm "$HLSAVES_DIR/11_$MONTHSlabel"
	fi
	if_exists_mv "$HLSAVES_DIR/10_$MONTHSlabel" "$HLSAVES_DIR/11_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/9_$MONTHSlabel"  "$HLSAVES_DIR/10_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/8_$MONTHSlabel"  "$HLSAVES_DIR/9_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/7_$MONTHSlabel"  "$HLSAVES_DIR/8_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/6_$MONTHSlabel"  "$HLSAVES_DIR/7_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/5_$MONTHSlabel"  "$HLSAVES_DIR/6_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/4_$MONTHSlabel"  "$HLSAVES_DIR/5_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/3_$MONTHSlabel"  "$HLSAVES_DIR/4_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/2_$MONTHSlabel"  "$HLSAVES_DIR/3_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/1_$MONTHlabel"   "$HLSAVES_DIR/2_$MONTHSlabel"
	if_exists_mv "$HLSAVES_DIR/3_$WEEKSlabel"   "$HLSAVES_DIR/1_$MONTHlabel"
}


rotation_weeks ()
{
	if [ "$TODAY_DAYOFMONTH" -lt 8 ] # first week of the moth ?
	then
		rotation_months
	else
		if_exists_rm "$HLSAVES_DIR/3_$WEEKSlabel"
	fi
	if_exists_mv "$HLSAVES_DIR/2_$WEEKSlabel" "$HLSAVES_DIR/3_$WEEKSlabel"
	if_exists_mv "$HLSAVES_DIR/1_$WEEKlabel"  "$HLSAVES_DIR/2_$WEEKSlabel"
	if_exists_mv "$HLSAVES_DIR/$WEEKDAY"    "$HLSAVES_DIR/1_$WEEKlabel"
}


rotation_days ()
{
	local today="$1"
	local yesterday="$2"

	if_exists_rm "$HLSAVES_DIR/$yesterday" # but should not exists :/

	if [ $today == "$WEEKDAY" ]
	then
		rotation_weeks
	else
		if_exists_rm "$HLSAVES_DIR/$today" # beacause last week
	fi

	echo "cp from $YSTDlabel to $yesterday..."
	cp -al "$HLSAVES_DIR/$YSTDlabel" "$HLSAVES_DIR/$yesterday"
	sync
}


backup_day ()
{
	local daynum="$TODAY_DAYOFWEEK"
	local today
	local yesterday

	case $daynum in
		1) today="$DOW1name" ; yesterday="$DOW7name" ;;
		2) today="$DOW2name" ; yesterday="$DOW1name" ;;
		3) today="$DOW3name" ; yesterday="$DOW2name" ;;
		4) today="$DOW4name" ; yesterday="$DOW3name" ;;
		5) today="$DOW5name" ; yesterday="$DOW4name" ;;
		6) today="$DOW6name" ; yesterday="$DOW5name" ;;
		7) today="$DOW7name" ; yesterday="$DOW6name" ;;
	esac

	[ ! -d "$HLSAVES_DIR/$YSTDlabel" ] && mkdir "$HLSAVES_DIR/$YSTDlabel"

	rotation_days $today $yesterday

	todo_before_backup

		echo "rsync to $YSTDlabel..."
		# rsync -A (ACL) -X (extended attributes) ?

		local EXCLUDE_FILE=""
		if [ "1${EXCLUDE_FROM}" != "1" ]
		then
			[ -s "$mydir/$EXCLUDE_FROM" ] && EXCLUDE_FILE="$mydir/$EXCLUDE_FROM" 
			[ -s "$EXCLUDE_FROM" ] && EXCLUDE_FILE="$EXCLUDE_FROM"
		fi

		if [ "1${EXCLUDE_PATTERN}" != "1" ]
		then
			if [ "1${EXCLUDE_FILE}" != "1" ]
			then
				rsync -a --delete --numeric-ids --exclude="$EXCLUDE_PATTERN" --exclude-from="$EXCLUDE_FILE" "$TOBESAVED_DIR/" "$HLSAVES_DIR/$YSTDlabel/"
			else
				rsync -a --delete --numeric-ids --exclude="$EXCLUDE_PATTERN" "$TOBESAVED_DIR/" "$HLSAVES_DIR/$YSTDlabel/"
			fi
		else
			if [ "1${EXCLUDE_FILE}" != "1" ]
			then
				rsync -a --delete --numeric-ids --exclude-from="$EXCLUDE_FILE" "$TOBESAVED_DIR/" "$HLSAVES_DIR/$YSTDlabel/"
			else
				rsync -a --delete --numeric-ids "$TOBESAVED_DIR/" "$HLSAVES_DIR/$YSTDlabel/"
			fi
		fi

		touch -t "$TODAY_FORTOUCH" "$HLSAVES_DIR/$YSTDlabel"
		sync

	todo_after_backup

	echo "hardlink $YSTDlabel $yesterday..."
	# because rsync --fuzzy seem to be very limited...
	# ↓
	hardlink "$HLSAVES_DIR/$YSTDlabel" "$HLSAVES_DIR/$yesterday"
}

backup_day
