#!/bin/sh
# The script for displaying status of installation
# Copyright (c) 2008 Canon IT Solutions Inc.
#

# check shell and set PATH environment variable
#
PATH=/bin:/usr/bin
export PATH

# check a script path
#
case $0 in
bin/statdpy)
	;;
*)
	echo "Start this script from the 'release' directory using: 'bin/statdpy'." 1>&2
	exit 1
	;;
esac

_LN=$1
_FN=$2

# check parameter
#
if [ "X${_LN}" = "X" -o "X${_FN}" = "X" ]; then
	echo "Usage: $0 <Number> <Filename>" 1>&2
	exit 1
fi
if [ `expr ${_LN}` -lt 1 ]; then
	echo "Usage: $0 <Number> <Filename>" 1>&2
	exit 1
fi

# ignore HUP CTRL-C and QUIT interrupts
#
trap "" 1 2 3

if [ "X${AWK}" = "X" ]; then
	which nawk >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		AWK="nawk"
	else
		AWK="awk"
	fi
fi

${AWK} '
function xy_cursor(X, Y) {
	str = "tput cup " Y " " X
	system(str)
}

function xy_clear(X, Y) {
	xy_cursor(X, Y)
	str = "tput el"
	system(str)
}

function dpy_asterisk(curx, lastx, y) {
	while ( lastx < curx ) {
		lastx++
		printf "*"
	}
}

BEGIN {
	XX = 12
	YY = 14
	ST = "0% -------------------------------------------------- 100%"
	ET = "0% ************************************************** 100%"
	xy_clear(XX - 3, YY)
	xy_cursor(XX - 3, YY)
	print ST
	xy_cursor(XX, YY)
	lastx = XX
}
{
	x = int(NR * 50 / lines) + 11
	if (lastx < x) {
		dpy_asterisk(x, lastx, YY)
		lastx = x
	}
}
END {
	xy_cursor(XX - 3, YY)
	print ET
}
' "lines=${_LN}" < ${_FN}
