#!/usr/bin/bash

source /root/variable.sh
#http://172.30.0.1:8081/bot

error_fatal (){
	[[ -n $2 ]] && tg_mess "$1" "$2"
	echo $(tput setab 1)
	printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
	echo " "$1" " | systemd-cat -t backups.sh -p emerg
	# stdout -> stderr
	#>&2 echo " "$1" "
	printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
	echo $(tput sgr 0) $'\n'
	exit 3
}

print_log () {
	echo $1 | systemd-cat -t backups.sh -p info
}

tg_mess () {
	curl -s -o /dev/null -X POST -H "Content-Type: application/json" \
		-d "{\"chat_id\": $2,
		\"text\": \"$1\",
		\"disable_notification\": false}" \
		${TG_URL}${BOT_API}/sendMessage
}

tg_send () {
	SENDED=false
	while [ "$SENDED" == false ]; do
		if [[ -n $2 ]]; then
			LOG=$(curl -F document=@"$1" \
			${TG_URL}${BOT_API}/sendDocument?chat_id=$2 2>/dev/null)
		else
			LOG=$(curl -s -F "message_thread_id=${TOPIC_ID}" -F document=@"$1" \
				${TG_URL}${BOT_API}/sendDocument?chat_id=$(echo 'LTEwMDE5NTM1NzgyNDgK' | base64 -d) 2>/dev/null)
		fi
		case $(echo $LOG | jq '.ok') in
			true)
				echo $LOG | jq '.result.document.file_name' | awk '{print "✅TG "$1}' | systemd-cat -t backups.sh -p info
				SENDED=true
				;;
			false)
				SENDED=false
				;;
		esac
		if [ "$SENDED" == false ]; then
			SLEEP_TIME=$(echo $LOG | jq '.parameters.retry_after')
			echo "sleep "$SLEEP_TIME | systemd-cat -t backups.sh -p info 
			sleep $SLEEP_TIME
		fi
	done
return 0
}

# Single file backup
######################################################
upload_pers () {
if [[ -f "$1" ]]; then
	yes | cp -i $1 $2 && tg_send $2 $CHAT_ID_PERS && rm -f $2 || error_fatal "🔥$(hostname) TG WG send error" $CHAT_ID_PERS
fi
}


systemctl is-active wg-quick@wg0 || error_fatal "🔥ERROR wg0 inactive"

# SIGUR backups
######################################################
if [[ -n $BACK_skud ]]; then
	FILE_NAME="/root/$(hostname)-$(date +%y%m%d).sql"
	mysqldump -P 3306 $SQL_PASS -B TC-DB-MAIN TC-DB-LOG > $FILE_NAME || \
		error_fatal "🔥$(hostname) mysqldump" $CHAT_ID_PERS
	tg_send $FILE_NAME || error_fatal "🔥$(hostname) TG SIGUR send error" $CHAT_ID_PERS
	print_log "✅Sigur backup complite" && rm -f $FILE_NAME
fi

# WG backups
######################################################
upload_pers /etc/wireguard/wg0.conf "/root/$(hostname)-wg0.conf"

# Variable backups
######################################################
upload_pers /root/variable.sh "/root/$(hostname)-variable.sh"

# Hosts backups
######################################################
upload_pers /etc/hosts "/etc/$(hostname)-hosts"

# Oxi router backups
######################################################
upload_pers /root/oxidized/config/router.db "/root/oxidized/$(hostname)-oxi-router.db"

# Oxi backups
######################################################
if [[ "$(ls -A /root/oxidized/config/configs/)" ]]; then
	for SCRIPT_F in /root/oxidized/config/configs/* ; do
		tg_send $SCRIPT_F $CHAT_ID_PERS || error_fatal "🔥$(hostname) Oxi ${SCRIPT_F} send error" $CHAT_ID_PERS
	done
fi

# RTSP-proxy backups
######################################################
upload_pers /var/www/cam/cam.index.html "/var/www/cam/$(hostname)-cam.index.html"
upload_pers /root/rtsp-proxy/mediamtx.yml "/root/rtsp-proxy/$(hostname)-mediamtx.yml"

tg_mess "✅$(hostname) complite" $CHAT_ID_PERS