#!/bin/bash

# according to /usr/include/sysexit.h
EXIT_USAGE_ERROR=64

# print progress, echo 100% if progress file not found
# example, tc-cp progress
if [ $# -eq 1 ]; then
    if [ -f $1 ]; then
        tail -n 1 $1 | awk 'BEGIN{RS="\r";} {print $2}' | tail -n 1
    else
        echo "100%"
    fi
    exit
fi

# start copy and delete progress file
# example, tc-cp progress source destination
if [ $# -eq 3 ]; then
    rsync -h $2 $3 --progress > $1
    rm -f $1
    exit
fi

echo "tc-cp progress source destination"
exit $EXIT_USAGE_ERROR
