#!/bin/bash

# This script intended to be called by tar as
# an argument of --checkpoint-action.

# Size in bytes
if [ $# -ne 2 ] ; then
	echo 'usage: tar_progress TAR_SIZE OVERRDIRE'
	exit 1
fi
TAR_SIZE=$1

if [ $2 = "yes" ] ; then
	OVERRIDE='\r'
else
	OVERRIDE='\n' # PART OF THE API
fi

if [ ! -n "$TAR_BLOCKING_FACTOR" ] ; then
	echo 'no TAR_BLOCKING_FACTOR, do not execute directly'
	exit 1
fi

if [ ! -n "$TAR_CHECKPOINT" ] ; then
	echo 'no TAR_CHECKPOINT, do not execute directly'
	exit 1
fi

let "DONE = $TAR_BLOCKING_FACTOR * 512 * $TAR_CHECKPOINT"

let "PERCENT = $DONE * 100 / $TAR_SIZE"

PREFIX="a-r-i: rootfs extracting progress:" # PART OF THE API

echo -ne "$PREFIX $PERCENT%${OVERRIDE}"
