#!/bin/sh -e
# Simple backup with borg
if test $# -eq 0
then borg list --format "{barchive:40} Created {time}{NL}"
	 exit $?
fi
while test $# -gt 0; do
	case "$1" in
		(mount) moul backup >/dev/null 2>&1
			cd /mnt/backup
			mkdir -p backups
			borg mount borg backups
			cd backups
			"$SHELL"
			cd ..
			borg umount backups
			rm -d backups
			exit 0;;
		(tree) borg list --short "::$2" | tree --fromfile . | less -F
			   exit $?;;
		(-n) run=arg-test; shift;;
		(*_) prefix=$1; shift;;
		(*) break;;
	esac
done
name="$(echo "$1" |
	sed -e 's|/$||' \
		-e 's|20\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)|\1\2\3|' \
		-e 's|\([0-9]\{6\}\)[-_]\(.*\)|\2_\1|' \
		-e 's|\(\w\+Drive\)|cloud_\L\1|i')"
test $# -gt 1 && shift
${run:-borg} create --exclude-caches --progress --stats "::$prefix$name" "$@"

