#!/bin/bash

# fix the trailing slash annoyance
all=`for i in "$@"; do echo ${i%/};done`
all=("$all")

IFS=$'\n' #sets Bash's internal field separator to newline only

cp -R ${all} &

destination="${!#}" # the last argument


#delete last argument, destination
argv=;for i; do eval argv$#=\$i;argv="\"\$argv$#\" $argv";shift;done;eval set x "$argv";shift;shift

totalsize=`du -c -k "$@" | tail -1 | awk '{print $1}'`
onepercent=`echo "$totalsize" '/' '100' | bc`

cd "$destination"
copied=`basename "$@"`
COPIED=("$copied")

echo -ne '\033(0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\033(B'

howmuch=1
while [ "$howmuch" -lt "50" ]
	do
		sleep 1
		copied=`du -c -k ${COPIED} 2> /dev/null | tail -1 | awk '{print $1}'`
		percentage=`echo "$copied" '/' "$onepercent" | bc`
		howmuch=`echo "$percentage" '/' '2' | bc`
		if [ "$howmuch" -lt '1' ]
			then
				howmuch=1
		fi
	 	progress=`echo "                                                  " | cut -c 1-${howmuch}`
		echo -ne '\r'
		echo -ne "\033[7m$progress\033[0m"
		echo -ne '\r'
		echo -ne "\033[52C${percentage}%"
	done
echo