#!/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=""
# ten minutes by default
timeout="600"

# 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"
    (
        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 "--timeout             Specify the reblock timeout, in seconds. Defaults to 10 minutes"
        echo "--help                This stuff"
    ) 1>&2
    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" = --timeout ]
    then
        timeout="$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 \
                --backup-id "$backup_id"
            ;;
        *)
            backshift \
                --save-directory "$save_directory" \
                --list-backup \
                --backup-id "$backup_id" \
                --starting-directory "$starting_directory"
            ;;
        esac | \
        count -c | \
        awk "BEGIN { total=0 }  { total += "'$3'" + $nominal_metadatasize_per_file } END { print total }"
}

case "$starting_directory" in
    "")
        backshift \
            --save-directory "$save_directory" \
            --produce-tar \
            --backup-id "$backup_id"
        ;;
    *)
        backshift \
            --save-directory "$save_directory" \
            --produce-tar \
            --backup-id "$backup_id" \
            --starting-directory "$starting_directory"
        ;;
esac | \
        reblock -e "$(gen_size_estimate)" -t "$timeout"