#!/bin/bash

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

function usage
{
    retval="$1"
	case "$retval" in
		0)
			;;
		*)
			exec 1>&2
			;;
	esac
	echo "Usage: $0"
	echo "--directories         A list of directories to du and tar - must be the last option"
	echo "--help                This stuff"
	echo
	echo "Tar up a local directory hierarchy and pipe it through gprog, using du to get an estimate of how much data will need"
	echo "to be copied."
    exit "$retval"
}

while [ "$#" -ge 1 ]
do
    if [ "$1" = --directories ]
    then
        shift
        break
    elif [ "$1" = --help ]
    then
        usage 0
    else
        echo "$0: Illegal option: $1" 1>&2
        usage 1
    fi
    shift
done

if type -path gtar > /dev/null 2>&1
then
	tar=gtar
else
	tar=tar
fi

estimate=$(for i in "$@"
do
    du -skx "$i"
done | \
    count -c | \
    python3 -c '
import sys
total = 0
for line in sys.stdin:
    total += int(line.split()[0]) * 1024
print(total)')

echo "Estimate: $estimate bytes" 1>&2

"$tar" --create --sparse --one-file-system "$@" | gprog --size-estimate "$estimate" --title "gprog-du-tar $*"