#!/bin/sh
# Show gauge while install-bulk is working
# It measures the used space based on df (disk free)
#
# Call this:
# dfgauge MESSAGE TOTAL_SIZE DEVICE [ DEVICE ...]
#
# Author: Eko M. Budi for Vector Linux
# License : GNU GPL
#
# 20060318: Eko M. Budi
# - works on multiple harddisk now

NAME=$1
shift
tot=$1
shift
DEVICES="$*"

mbused() {
  MB=0
  for DEV in $DEVICES ; do
    line=`df | grep $DEV` 
    if [ "$line" ]; then
      mb1=`echo $line | cut -f 3 -d ' '`
      MB=`expr $MB '+' $mb1`
    fi
  done;
  echo $MB
}

mbtot=`expr $tot / 1024`
start=`mbused`
pct=1

(
while [ ! "$pct" = "100" ]; do
    current=`mbused`
    progress=`expr $current '-' $start`
    mbprog=`expr $progress '/' 1024`
    if [ $mbprog -gt $mbtot ]; then
       mbprog=`expr $mbtot - 1`
       pct=99
    else
       pct=`expr 100 '*' $mbprog`
       pct=`expr $pct '/' $mbtot`
    fi
    
    echo "XXX"
    echo $pct
    echo "\nInstalling $NAME ($mbprog of $mbtot MB)"
    echo "XXX"
    AA=$(cat /tmp/working)
    #AA=`ps | grep install-bulk | grep -v grep`
    if [ "$AA" = "working" ]; then
        sleep 4
	echo "$AA" >> /tmp/setup.log 
	echo "Installing $NAME = $progress KB" >> /tmp/setup.log 
    else
	pct=100
	echo "XXX"
	echo $pct
	echo "\nInstallation of $NAME has been completed!"
	echo "XXX"
	echo "Installed $NAME = $progress KB" >> /tmp/setup.log 
	sleep 1
	exit 0
    fi
done
) | dialog --backtitle "Vector Linux Install" --title " Please Wait...." --gauge "" 8 66 0

