#!/bin/sh
set -ex

# This script tells MoaT-Link to continue with the next log file.
# By default it writes a full dump daily.

cd "$DEST"
D=$(date +"$DATE")
test -d $D || mkdir -p $D
cd $D

x=$(ls|sort -rn|head -1)
if test -n "$x" ; then
	x=$(expr $(basename $x .moat) + 1)
	I="-s state =False"
else
	x=0
	I="-s state =True"
fi

export PYTHONPATH
moat link -p /run/moat/link/socket cmd s.log -s path "~$DEST/$D/$x.moat" $i
if test $x -eq 0 ; then
	# Now, delete some old backups
	cd "$DEST"

	# delete hourly after ten days but keep the day's first backup
	find . -maxdepth 1 ! -name \*-00 -mtime +10 -print0|xargs -0r rm -rf
	# delete daily after 45 days but keep the month's first backup
	find . -maxdepth 1 ! -name \*01-00 -mtime +45 -print0|xargs -0r rm -rf
	# delete monthly (i.e. the rest) after a year or so
	find . -maxdepth 1 -mtime +400 -print0|xargs -0r rm -rf
fi
