#!/bin/bash

set -eu
set -o pipefail

# this is far from a perfect estimate, but it's usually pretty decent

save_directory=""
starting_directory=""
backup_id=""
tar_format=default

# We assume that each file has about 815 bytes of overhead
# I calculated this number by extracting a large save, counting the number of files, counting the number of bytes in the
# tarball, and totalling the length of the files within the tarball.
# (bytes in tarball - total of lengths) / file count == 815.203
nominal_metadatasize_per_file=815

function usage
{
    retval="$1"
	case "$retval" in
		0)
			;;
		*)
			exec 1>&2
			;;
	esac
	echo "Usage: $0"
	echo "--save-directory      Specify the backshift repo. Required"
	echo "--starting-directory  Specify the backshift starting directory. Optional"
	echo "--backup-id           Specify the backshift backup ID. Required"
	echo "--tar-format fmt      gnu or ustar. pax can be expected to fail"
	echo "--help                This stuff"
	echo
	echo 'This script will compute how much disk space is used by a given backshift backup, and use that to give an ETA for'
	echo 'an extraction.'
    exit "$retval"
}

while [ "$#" -ge 1 ]
do
    if [ "$1" = --save-directory ]
    then
        save_directory="$2"
        shift
    elif [ "$1" = --backup-id ]
    then
        backup_id="$2"
        shift
    elif [ "$1" = --starting-directory ]
    then
        starting_directory="$2"
        shift
    elif [ "$1" = --tar-format ]
    then
        tar_format="$2"
        shift
    elif [ "$1" = --help ] || [ "$1" == -h ]
    then
        usage 0
    else
        echo "$0: Illegal option: $1" 1>&2
        usage 1
    fi
    shift
done

if [ "${save_directory:-}" = "" ]
then
    echo "$0: --save-directory is a required option" 1>&2
    usage 1
fi

if [ "${backup_id:-}" = "" ]
then
    echo "$0: --backup-id is a required option" 1>&2
    usage 1
fi

function gen_size_estimate
{
    case "$starting_directory" in
        "")
            backshift \
                --save-directory "$save_directory" \
                --list-backup-sizes \
                --backup-id "$backup_id"
            ;;
        *)
            backshift \
                --save-directory "$save_directory" \
                --list-backup-sizes \
                --backup-id "$backup_id" \
                --starting-directory "$starting_directory"
            ;;
        esac 2>&1 | \
        count -c | \
        total
        # awk "BEGIN { total=0 }  { total += "'$3'" + $nominal_metadatasize_per_file } END { print total }"
}

case "$starting_directory" in
    "")
        backshift \
            --save-directory "$save_directory" \
            --tar-format "$tar_format" \
            --produce-tar \
            --backup-id "$backup_id"
        ;;
    *)
        backshift \
            --save-directory "$save_directory" \
            --produce-tar \
            --tar-format "$tar_format" \
            --backup-id "$backup_id" \
            --starting-directory "$starting_directory"
        ;;
esac | \
        gprog --size-estimate "$(gen_size_estimate)" --title "gprog-backshift-extract $backup_id $starting_directory"