#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename $0)
CMDVER="1.0"
CMDSTR="$CMD v$CMDSTR (2020/02)"

days="360"
f_exec=
f_long=false

usage()
{
	echo "
=== $CMDSTR === SHOW CHANGES BETWEEN THE LAST 2 kusa INVOCATIONS ===

usage: $CMD command [options] [days]    (default: $days)

options:
  -n|--dry-run	dry run
  -x|--execute	execute commands (remove files)
  -l		long list files (in dry-run mode)
" >&2
	exit 127
}


# (MAIN)

while [ $# != 0 ]
do
  case $1 in
    -n|--dry-run0)	f_exec=false ;;
    -x|--execute)	f_exec=true ;;
    -l)			f_long=true ;;
    [0-9]*)		days=$1 ;;
    *)			usage ;;
  esac
  shift
done

echo $days | grep -q '^[0-9][0-9]*' || {
	echo "error: days param not numeric" >&2
	usage
}

[ "X$f_exec" = "X" ] && usage


cmd="find /etc/backup -type f -mtime +$days"

if $f_exec
then
	[ "$(id -u)" != "0" ] && {
		echo "you myst be root to execute this command" >&2
		exit 1
	}
	$cmd -print -delete
	find /etc/backup -depth -type d -exec rmdir {} \; 2>/dev/null
else
	if $f_long
	then
		$cmd -print0 | xargs -0 ls -ld
	else
		$cmd
	fi
fi
exit 0
