#!/bin/bash
#
# progbar (raider progression bar function)
# This file is part of Raider

ctlc_close_bar() { # close progress bar after ctrl-c
	"rm" -f $BAR_SEMAF >/dev/null 2>&1	# close progress bar
	>$TRAP_TRIGGERED >/dev/null 2>&1
	EndBar=1
	echo -e "\nStopped with Ctrl+C break"
	if [ -e /tmp/printk-$$ ]; then
		cat /tmp/printk-$$ > /proc/sys/kernel/printk
	fi
	exit 1
}

print_bar() {		# print the bar 

	local Count=$1
	local Max=$2
	local Remain=$3
	local Elapsed=$4
	local Speed=$5
	local LenBar=50
	if [ $# -eq 6 ]; then
		LenBar=$6
	fi
	if [ ${Count} -gt ${Max} ]; then
		Count=${Max}
	fi
	local Percent=$(((100*Count)/Max))
	local BarPercent=$(((LenBar*Percent)/100))

	if [ ${Percent} -eq 100 ]; then
		Remain=0
	fi
	
	local BarProgress=$(for i in $(seq ${BarPercent}); do echo -n '='; done)
	BarProgress="${BarProgress:0:${#BarProgress}}>"
	local Nxt=$((LenBar-BarPercent))
	local BarPad=$(for i in $(seq ${Nxt}); do echo -n '.'; done)

	local TElapsed=$(date -u -d @$Elapsed +%Hh%Mm%Ss)
	TElapsed=$(echo ${TElapsed} | sed -e "s/^00h//" -e "s/^00m//")

	local TRemain=$(date -u -d @$Remain +%Hh%Mm%Ss)
	TRemain=$(echo ${TRemain} | sed -e "s/^00h//" -e "s/^00m//")

	local larg=$(tput cols)
	if [ ${TimeElapsed} -gt ${TimeDiff} ] && [ ${EndBar} -eq 0 ] && [ ${Remain} -gt 0 ]; then		
		local mess0="[ ${Percent}% ][${BarProgress}${BarPad}] (${Speed}K/s) [ Remain: ${TRemain} Spent: ${TElapsed} ]"
		local mess=" :: ${mess0}"
		local mess1="${TAB_}${mess0}"
		echo -en "\r${mess1}\033[K"
		if [ ${#mess} -gt ${larg} ]; then
			echo -ne "\r\033[1A"
		fi
	else
		echo -en "\r${TAB_}[ ${Percent}% ][${BarProgress}${BarPad}][ Time: ${TElapsed} ]\033[K"
	fi
}


progress_bar() {		# function to print a progress bar
	BOOTP_EXCEPTION=0
	if [ $# -eq 3 ]; then
		# boot partition exception
		BOOTP_EXCEPTION=1
	fi
	touch ${BAR_SEMAF}
	local LenBar=25
	local from_mp=$1
	local to_mp=$2
	if [ $BOOTP_EXCEPTION -eq 1 ]; then
		local BytesLoadMax=$(space_used_mountpoint ${to_mp})
	else
		local BytesLoadMax=$(space_used_mountpoint ${from_mp})
	fi
	
	local BytesLoadKonstant=$(space_used_mountpoint ${to_mp})
	local BytesLoadNow=0
	local TimeStart=$(date +%s)
	local TimeDiff=10	# 20 seconds difference
	local TimeRemain=0
	local TimeElapsed=0
	local Speed=1
	local EndBar=0
	while [ -e ${BAR_SEMAF} ] && sleep 1
	do
		TimeNow=$(date +%s)
		TimeElapsed=$((TimeNow-TimeStart))
		if [ $BOOTP_EXCEPTION -eq 1 ]; then
			BytesLoadMax=$(space_used_mountpoint ${to_mp})
		else
			BytesLoadMax=$(space_used_mountpoint ${from_mp})
		fi
		BytesLoadNow=$(( $(space_used_mountpoint ${to_mp})-BytesLoadKonstant ))
		if [ ${TimeElapsed} -eq 0 ]; then
			Speed=1
		else
			Speed=$(( BytesLoadNow/TimeElapsed ))
		fi
		
		
		if [ ${Speed} -le 0 ]; then		# avoid errors if Speed<=0 (don't calculate TimeRemain)
			Speed=0
		else
			TimeRemain=$(( (BytesLoadMax-BytesLoadNow)/Speed ))
		fi
		if ! [ -f ${TRAP_TRIGGERED} ]; then
			print_bar ${BytesLoadNow} ${BytesLoadMax} ${TimeRemain} ${TimeElapsed} ${Speed} ${LenBar}
		fi
	done
	if ! [ -f ${TRAP_TRIGGERED} ]; then
		EndBar=1
		print_bar ${BytesLoadMax} ${BytesLoadMax} 0 ${TimeElapsed} ${Speed} ${LenBar}
	else
		"rm" -f ${TRAP_TRIGGERED} >/dev/null 2>&1
	fi
	echo
	exit
}

space_used_mountpoint() {	# parse space used by the mountpoint
	local mp=$1
	echo $(df -k -P | grep ^/dev | awk -v mp="$mp" '$6 == mp {print $3}')
}


