#!/bin/sh
. /usr/libexec/haproxy-manager/common.sh

BASE="$(backup_dir)"
[ ! -d "$BASE" ] || BASE="$(resolve_storage_dir "$BASE")" || exit 1
LAST=""
[ ! -r "$BASE/LAST" ] || LAST="$(basename "$(cat "$BASE/LAST")")"

count=0
for dir in $(find "$BASE" -maxdepth 1 -type d -name '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]' 2>/dev/null | sort -r); do
	[ -d "$dir" ] || continue
	id="$(basename "$dir")"
	current=0
	[ "$id" != "$LAST" ] || current=1
	files=0
	for file in firewall uhttpd haproxy_manager haproxy.cfg; do
		[ ! -f "$dir/$file" ] || files=$((files + 1))
	done
	printf 'backup\t%s\t%s\t%s\n' "$id" "$current" "$files"
	count=$((count + 1))
	[ "$count" -lt "$BACKUP_LIMIT" ] || break
done
