#!/bin/sh

function usage() {
	echo "${0##*/} <config-file-name>"
	echo
	echo "Use <config-file-name> from '/etc/ntools/borg/' folder"
	exit 1
}
cfg="$1"
if [[ -z "$cfg" ]]; then
	usage
fi
valid='0-9a-zA-Z_-'
if [[ "$cfg" =~ [^$valid] ]]; then
	echo "Invalid config name."
	exit 1
fi
cfgfile="/etc/ntools/borg/$cfg"
if [[ -f "$cfgfile" ]]; then
	. "$cfgfile"
else
	echo "Config file not found."
	exit 1
fi

DATE=`date +%Y%m%d-%H%M`
BACKUPNAME="$NAME-$DATE"
BORGPARAMS=''

if [[ -n "$HOST" ]]; then
	BORG_REPO="$HOST:$REPOPATH" 
else
	BORG_REPO="$REPOPATH"
fi

export LANG="ru_RU.UTF-8"
export BORG_REPO

if [[ -n "$PASSFN" ]]; then
	export BORG_PASSCOMMAND="cat $PASSFN"
fi

if [[ -n "$COMPRESSION" ]]; then
	BORGPARAMS="$BORGPARAMS --compression $COMPRESSION"
fi

if [[ $VERBOSE == '1' ]]; then
	BORGVERBOSE="-p -v"
	ZABBIXVERBOSE="-vv"
fi

if [[ -n "$PATTERNFILE" ]]; then
	if [[ -n "$STDINPROG" ]]; then
		echo "PATTERNFILE and STDINPROG can not both be defined."
		exit 1
	else
		BORGBACKUP="nice -n 19 borg $BORGVERBOSE create --json --patterns-from $PATTERNFILE --exclude-caches $BORGPARAMS ::$BACKUPNAME"
	fi
else
	if [[ -n "$STDINPROG" ]]; then
		if [[ -z "$STDINNAME" ]]; then
			STDINNAME="$BACKUPNAME"
		else
			MATCH='*'
			if [[ "$STDINNAME" =~ "$MATCH" ]]; then
				STDINNAME=$(echo "$STDINNAME" | sed "s/$MATCH/$DATE/")
			fi
		fi
		BORGBACKUP="$STDINPROG | nice -n 19 borg $BORGVERBOSE create --json --stdin-name $STDINNAME $BORGPARAMS ::$BACKUPNAME -"
	else
		echo "PATTERNFILE or STDINPROG must be defined."
		exit 1
	fi
fi

RESULT=$(eval "$BORGBACKUP")
ERR=$?
if [ "$ERR" -gt "0" ]; then
	if [ "$ERR" -gt "1" ]; then
		echo "Fatal error"
		exit 1
	fi
fi

if [[ -n "$ZABBIXCONF" ]]; then
	ZABBIXTARGET="-c $ZABBIXCONF"
	if [[ -z "$ZABBIXHOST" ]]; then
		ZABBIXHOST=$(cat "$ZABBIXCONF" | grep "^Hostname" | cut -f2 -d=)
	fi
else
	if [[ -n "$ZABBIXSERVER" ]]; then
		ZABBIXTARGET="-z $ZABBIXSERVER"
	fi
fi
if [[ -z "$ZABBIXHOST" ]]; then
	ZABBIXHOST=$(hostname --long | tr '[:upper:]' '[:lower:]')
fi
if [[ -n "$ZABBIXTARGET" ]]; then
	OUTPUT=$(echo "$RESULT" | jq -r --arg DELTA $(date +%s -d "1970-01-01T00:00:00") "(
	\"$ZABBIXHOST borg[$cfg,name] \"+.archive.name,
	\"$ZABBIXHOST borg[$cfg,startdate] \"+(.archive.start|strptime(\"%Y-%m-%dT%H:%M:%S.000000\")|mktime+("\$DELTA"|tonumber)|tostring),
	\"$ZABBIXHOST borg[$cfg,enddate] \"+(.archive.end|strptime(\"%Y-%m-%dT%H:%M:%S.000000\")|mktime+("\$DELTA"|tonumber)|tostring),
	\"$ZABBIXHOST borg[$cfg,duration] \"+(.archive.duration|tostring),
	\"$ZABBIXHOST borg[$cfg,nfiles] \"+(.archive.stats.nfiles|tostring),
	\"$ZABBIXHOST borg[$cfg,original_size] \"+(.archive.stats.original_size|tostring),
	\"$ZABBIXHOST borg[$cfg,unique_csize] \"+(.cache.stats.unique_csize|tostring)
	)" | /usr/bin/zabbix_sender $ZABBIXVERBOSE $ZABBIXTARGET -i - 2>&1 )
else
	echo "Warning! No target set for zabbix_sender."
fi

if [[ $VERBOSE == '1' ]]; then
	echo "Zabbix hostname: $ZABBIXHOST"
	echo $OUTPUT
fi

nice -n 19 borg prune $PRUNEPARAMS --glob-archives "$NAME-*"
