#!/usr/bin/ksh  
# License GNU General Public License version 3.0 (GPLv3)
# (c) Copyright 2015. Nigel Griffiths

export topaschart_version=7 # Nigel had it as 5 but indicated it is 7

if [[ -e /usr/sbin/aixpert ]]
then
# echo running on  AIX
export ECHO="echo "
export SORTARG=" +2 "
else
#echo running on  Linux 
export ECHO="echo -e "
export SORTARG=" -k2 "
fi


html_start() 
{
$ECHO '<html>'
$ECHO '\t<head>'
$ECHO '\t\t<title>TopasChart</title>'
$ECHO '\t\t<script type="text/javascript" src="https://www.google.com/jsapi"></script>'
$ECHO '\t\t<script type="text/javascript">'
$ECHO '\t\tgoogle.load("visualization", "1.1", {packages:["corechart"]});'
$ECHO 
$ECHO '\t\tgoogle.setOnLoadCallback(setupCharts);'
$ECHO '\t\t'
$ECHO '\t\tfunction setupCharts() {'
$ECHO '\t\t'
$ECHO '\t\tvar chart = null;'
$ECHO 
}

chart_start()
{
$ECHO '\t\t\tvar data_'$1 '= google.visualization.arrayToDataTable(['
}

chart_end()
{
$ECHO '\t\t]);'
$ECHO
$ECHO '\t\tvar options_'$1' = {'
$ECHO '\t\t\tchartArea: {left: "5%", width: "85%", top: "10%", height: "80%"},'
$ECHO '\t\t\ttitle: "'$2'",'
$ECHO '\t\t\tfocusTarget: "category",'
$ECHO '\t\t\thAxis: {'
$ECHO '\t\t\t\tgridlines: {'
$ECHO '\t\t\t\t\tcolor: "lightgrey",'
$ECHO '\t\t\t\t\tcount: 30'
$ECHO '\t\t\t\t}'
$ECHO '\t\t\t},'
$ECHO '\t\t\tvAxis: {'
$ECHO '\t\t\t\tgridlines: {'
$ECHO '\t\t\t\t\tcolor: "lightgrey",'
$ECHO '\t\t\t\t\tcount: 11'
$ECHO '\t\t\t\t}'
$ECHO '\t\t\t},'
$ECHO '\t\t\tisStacked: ' $3
$ECHO '\t\t};'
$ECHO
$ECHO '\t\tdocument.getElementById("draw_'$1'").addEventListener("'click'", function() {'
$ECHO '\t\tif (chart && chart.clearChart) chart.clearChart();'
$ECHO 
$ECHO '\t\tchart = new google.visualization.AreaChart(document.getElementById("chart_master"));'
$ECHO '\t\tchart.draw( data_'$1', options_'$1');'
$ECHO '\t\t});'
$ECHO
}

chart_end_column()
{
$ECHO '\t\t]);'
$ECHO
$ECHO '\t\tvar options_'$1' = {'
$ECHO '\t\t\tchartArea: {left: "5%", width: "85%", top: "10%", height: "80%"},'
$ECHO '\t\t\ttitle: "'$2'",'
$ECHO '\t\t\tfocusTarget: "category",'
$ECHO '\t\t\tvAxis: {'
$ECHO '\t\t\t\tgridlines: {'
$ECHO '\t\t\t\t\tcolor: "lightgrey",'
$ECHO '\t\t\t\t\tcount: 11'
$ECHO '\t\t\t\t}'
$ECHO '\t\t\t},'
$ECHO '\t\t\tisStacked: ' $3
$ECHO '\t\t};'
$ECHO
$ECHO '\t\tdocument.getElementById("draw_'$1'").addEventListener("'click'", function() {'
$ECHO '\t\tif (chart && chart.clearChart) chart.clearChart();'
$ECHO 
$ECHO '\t\tchart = new google.visualization.ColumnChart(document.getElementById("chart_master"));'
$ECHO '\t\tchart.draw( data_'$1', options_'$1');'
$ECHO '\t\t});'
$ECHO
}

chart_end_top()
{
$ECHO '\t\t]);'
$ECHO
$ECHO '\t\tvar options_TOPSUM = {'
$ECHO '\t\t\tchartArea: {left: "5%", width: "85%", top: "10%", height: "80%"},'
$ECHO '\t\t\ttitle: "Top 20 processes by CPU correlation between CPU-seconds(Total), Character-I/O(Total), Memory-Size(Max) for each Command Name",'
$ECHO '\t\t\thAxis: {title: "CPU seconds in Total"},'
$ECHO '\t\t\tvAxis: {title: "Character I/O in Total"},'
$ECHO '\t\t\tsizeAxis: {maxSize: 200},'
$ECHO '\t\t\tbubble: {textStyle: {fontSize: 15}}'
$ECHO '\t\t};'
$ECHO
$ECHO '\t\tdocument.getElementById("draw_TOPSUM").addEventListener("'click'", function() {'
$ECHO '\t\tif (chart && chart.clearChart) chart.clearChart();'
$ECHO 
$ECHO '\t\tchart = new google.visualization.BubbleChart(document.getElementById("chart_master"));'
$ECHO '\t\tchart.draw( data_TOPSUM, options_TOPSUM);'
$ECHO '\t\t});'
$ECHO
}

chart_add_unstacked()
{
$ECHO '\t\tvar options_'$1'u = {'
$ECHO '\t\t\tchartArea: {left: "5%", width: "85%", top: "10%", height: "80%"},'
$ECHO '\t\t\ttitle: "'$2'",'
$ECHO '\t\t\tfocusTarget: "category",'
$ECHO '\t\t\thAxis: {'
$ECHO '\t\t\t\tgridlines: {'
$ECHO '\t\t\t\t\tcolor: "lightgrey",'
$ECHO '\t\t\t\t\tcount: 30'
$ECHO '\t\t\t\t}'
$ECHO '\t\t\t},'
$ECHO '\t\t\tvAxis: {'
$ECHO '\t\t\t\tgridlines: {'
$ECHO '\t\t\t\t\tcolor: "lightgrey",'
$ECHO '\t\t\t\t\tcount: 11'
$ECHO '\t\t\t\t}'
$ECHO '\t\t\t},'
$ECHO '\t\t\tisStacked: 0'
$ECHO '\t\t};'
$ECHO
$ECHO '\t\tdocument.getElementById("draw_'$1'u").addEventListener("'click'", function() {'
$ECHO '\t\tif (chart && chart.clearChart) chart.clearChart();'
$ECHO 
$ECHO '\t\tchart = new google.visualization.AreaChart(document.getElementById("chart_master"));'
$ECHO '\t\tchart.draw( data_'$1', options_'$1'u);'
$ECHO '\t\t});'
$ECHO
}

html_mid() 
{
$ECHO '\t\t}'
$ECHO '\t\t</script>'
$ECHO '\t</head>'
$ECHO '\t <body bgcolor="#EEEEFF">'
$ECHO '\ttopas data file: <b>'$ORIGINAL'</b>'
$ECHO '\t<br>'
#$ECHO '\t <hr>'
}

chart_button() 
{
$ECHO '\t<button id="draw_'$1'" style="color:'$2';">'$1'</button>'
}

chart_draw()
{
$ECHO ""
$ECHO '\t<div id="chart_master" style="width:100%; height:75%;">'
$ECHO '\t<h2 style="color:blue">Click on a Graph button above, to display that graph</h2>'
$ECHO '\t</div>'
$ECHO ""
}

html_end() 
{
$ECHO '\t</body>'
$ECHO '</html>'
}

chart()
{
# note: '\'' is VERY special and means to get around the impossible ' in a awk string
#	'   = end the awk program string 
#	\'  = escaped so its just a character
#	'   = start the awk program string again
#	strings are concatenated to the end and start disappear but this allows a ' char to be added
# as strings are concatenated by the shell before awk gets it, this just adds a single quote = '
	chart_start $1
	case $1 in

	PHYSICAL_CPU) 
	awk -F , '
	/^LPAR,L/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $3 "'\'','\''" $4 "'\'','\''" $7 "'\'']" }
	/^LPAR,T/ { print ",['\''" $2 "'\''," $3 ","  $4 "," $7 "]" }
	' <$INPUT
	;;

	CPU_UTIL) 
	awk -F , '
	/^CPU_ALL,C/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $3 "'\'','\''"  $4 "'\'','\''"  $5 "'\'','\''" $6 "'\'']" }
	/^CPU_ALL,T/ { print ",['\''" $2 "'\''," $3 "," $4 "," $5 "," $6 "]" }
	' <$INPUT
	;;

	POOLIDLE) 
	awk -F , '
	/^LPAR,L/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $6 "'\'','\''" $9 "'\'']" }
	/^LPAR,T/ { print ",['\''" $2 "'\''," $6 "," $9 "]" }
	' <$INPUT
	;;

	REALMEM) 
	awk -F , '
	/^MEMREAL,R/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $3 "'\'','\''" $4 "'\'']" }
	/^MEMREAL,T/ { print ",['\''" $2 "'\''," $3 "," $4 "]" }
	' <$INPUT
	;;

	REALMEMUSE) 
	awk -F , '
	/^MEMREAL,R/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $5 "'\'','\''" $6 "'\'','\''" $7 "'\'','\''" $8 "'\'']" }
	/^MEMREAL,T/ { print ",['\''" $2 "'\''," $5 "," $6 "," $7 "," $8 "]" }
	' <$INPUT
	;;

	MEM_LINUX) 
	awk -F , '
	/^MEM,M/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $3 "'\'','\''" $7 "'\'','\''" $12 "'\'','\''" $13 "'\'','\''" $15 "'\'','\''" $17"'\'']" }
	/^MEM,T/ { print ",['\''" $2 "'\''," $3 "," $7 "," $12 "," $13 "," $15 "," $17 "]" }
	' <$INPUT
	;;

	VIRTMEM) 
	awk -F , '
	/^MEM,M/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $6 "'\'','\''" $8 "'\'']" }
	/^MEM,T/ { print ",['\''" $2 "'\''," $6 "," $8 "]" }
	' <$INPUT
	;;

	SWAP_LINUX) 
	awk -F , '
	/^MEM,M/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $6 "'\'','\''" $10 "'\'']" }
	/^MEM,T/ { print ",['\''" $2 "'\''," $6 "," $10 "]" }
	' <$INPUT
	;;

	FSCACHE) 
	awk -F , '
	/^MEMUSE,M/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $3 "'\'','\''"  $4 "'\'','\''"  $5 "'\'']" }
	/^MEMUSE,T/ { print ",['\''" $2 "'\''," $3 "," $4 "," $5 "]" }
	' <$INPUT
	;;
	#;;

	RUNQ) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $3 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $3 "]" }
	' <$INPUT
	;;

	PSWITCH) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $5 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $5 "]" }
	' <$INPUT
	;;

	SYSCALL) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $6 "'\'','\''" $7 "'\'','\''" $8 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $6 "," $7 "," $8 "]" }
	' <$INPUT
	;;

	READWRITE) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $7 "'\'','\''" $8 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $7 "," $8 "]" }
	' <$INPUT
	;;

	FORKEXEC) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $9 "'\'','\''" $10 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $9 "," $10 "]" }
	' <$INPUT
	;;

	FILEIO) 
	awk -F , '
	/^FILE,F/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $6 "'\'','\''" $7 "'\'']" }
	/^FILE,T/ { print ",['\''" $2 "'\''," $6 "," $7 "]" }
	' <$INPUT
	;;

	PAGESPACE) 
	awk -F , '
	/^MEMVIRT,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $4 "'\'','\''"  $5 "'\'']" }
	/^MEMVIRT,T/ { print ",['\''" $2 "'\''," $4 "," $5 "]" }
	' <$INPUT
	;;

	PAGING) 
	awk -F , '
	/^MEMVIRT,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $6 "'\'','\''"  $7 "'\'','\''"  $8 "'\'','\''" $8 "'\'']" }
	/^MEMVIRT,T/ { print ",['\''" $2 "'\''," $5 "," $6 "," $7 "," $8 "]" }
	' <$INPUT
	;;

	SWAPIN) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $4 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $4 "]" }
	' <$INPUT
	;;

	IPC) 
	awk -F , '
	/^PROC,P/ { print "[{type: '\''datetime'\'', label: '\''Datetime'\'' },'\''" $11 "'\'','\''" $12 "'\'']" }
	/^PROC,T/ { print ",['\''" $2 "'\''," $11 "," $12 "]" }
	' <$INPUT
	;;

	*) $ECHO Oh handling code for chart $1
		;;
	esac

	chart_end $1 "$2" $3
}

hint()
{
	$ECHO 
	$ECHO  Hint: $0 topas_file html_file
	$ECHO 
	$ECHO  "   topas_file = topas captured data filename (ends with .topas) converted to nmon format"
	$ECHO  "                Convert with: topasout -a file.topas"
	$ECHO  "                The output is placed into file.csv"
	$ECHO  "   html_file  = output filename on your website directory like /webpages/docs/file.html"
	$ECHO  
	$ECHO  "\tExample: $0 mytopasfile.csv /webpages/docs/mycharts.html"
	$ECHO  
	exit
}

#------------------------------------ Don't change anything above here

generate()
{
html_start

# Straight forward graphs where we have to pick the data out of the line with fixed numbers of columns
chart PHYSICAL_CPU "VM Physical CPU Use of Shared CPU" 0
chart POOLIDLE "Whole machine Physical CPU Pool Use (If all PoolIdle=0 it means perf stats are not switch on at VM level)" 0
chart CPU_UTIL "CPU Utilisation Percentages" 1

chart REALMEM    "Real Memory (RAM)" 0
chart REALMEMUSE "Real Memory Use Percentages" 0
chart RUNQ    "Run Queue - processes ready to run" 0
chart PSWITCH "Process Switch - between processes" 0
chart SYSCALL "System calls from application to the kernel" 0
chart READWRITE "Read and Write System calls - for disk & network I/O" 0
chart FORKEXEC "Fork and Exec System Calls - creating processes" 0
chart FILEIO "File I/O through System Call read() & write() [disk I/O, process pipes & network sockets] in bytes" 0
chart PAGESPACE "Paging Space percentages" 1
chart PAGING "Paging from Pagespace (pgspgin & pgspgout) & Paging from Filesystem (pagein & pageout)" 0
chart SWAPIN    "Process Swap-In per second" 0

# CPU_USE
chart_start CPU_USE
$ECHO "['CPU','User-Time-Total','System-Util-Total']" 
grep "^CPU[0-9]*,T"  $INPUT | awk -F, '
{
	count[$1] += 1;
        usr[$1]   += $3;
        sys[$1]   += $4;
}
END {
        for (i in usr) {
                printf "%s %.1f %.1f\n", i, usr[i]/count[i], sys[i]/count[i]
        }
}' | sed -e 's/CPU//' | sort -n | awk '{
                printf ",['\''CPU%s'\'',%.1f,%.1f]\n", $1, $2, $3
}'
chart_end_column CPU_USE "Logical CPU Core Use (Power SMT or x86 Hyperthreads) Average(User%+System%)" 1


# These lines have different numbers of stats depending on the machine config
# Assuming we want all the stats on a line then fortunately we just need to format them

# For the wacko topas network stats we have to separate the KB/s from the frames/s (every one else on the planet calls this a packets/s)
grep ^LAN, $INPUT  | sed 's/,,/,/g' >$NET
# Now the column maths
integer columns=$(grep LAN,LAN $NET | awk -F, '{ print NF }')
integer nets=$(($(($columns-2))/5))
integer KBcols=$(($nets*2+2))
integer Frame1cols=$(($nets*2+2+1))
integer Frame2cols=$(($nets*4+2))

#echo columns $columns
#echo nets $nets
#echo KBcols $KBcols
#echo Frame1 $Frame1cols
#echo Frame2 $Frame2cols
#echo cut -d, -f 1-$KBcols
#echo cut -d, -f 1-2,$Frame1cols-$Frame2cols

cat $NET |  cut -d , -f 1-$KBcols                   >$NETKB 
cat $NET |  cut -d , -f 1-2,$Frame1cols-$Frame2cols >$NETFR

chart_start NET 
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^LAN,L $NETKB | cut -f 3- -d, | sed -e "s/,$//"  -e "s/,/\',\'/g" -e "s/\$/\\']/" 
grep ^LAN,T $NETKB | \
awk -F"," '{printf(",['\''%s'\''",$2); for(i=3; i<=NF; i++){if( i<=(((NF-2)/2+2)) ){printf(",%.1f",$i)} else {printf(",-%.1f",$i)}};printf("]\n")}'
chart_end NET "Network Throughput in KB per second - out=sent & in=received(shown negatively)" 0

chart_start NETPACKET 
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^LAN,L $NETFR | cut -f 3- -d, | sed -e "s/,$//"  -e "s/,/\',\'/g" -e "s/\$/\\']/" 
grep ^LAN,T $NETFR | sed -e "s/LAN,/#\[\'/" -e "s/,/\',/"    -e "s/\$/\\]/" -e  "s/#/,/"
chart_end NETPACKET "Network Packet count per second" 0

chart_start DISKBUSY
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^DISKBUSY,D $INPUT | cut -f 3- -d, | sed  -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^DISKBUSY,T $INPUT | sed -e "s/DISKBUSY,/#\[\'/" -e  "s/,/\',/"   -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end DISKBUSY "Disk Busy Percentage of the time (Stacked)" 1
chart_add_unstacked DISKBUSY "Disk Busy Percentage of the time (UnStacked)"

chart_start DISKREAD
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^DISKREAD,D $INPUT | cut -f 3- -d, | sed -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^DISKREAD,T $INPUT | sed -e "s/DISKREAD,/#\[\'/" -e  "s/,/\',/"    -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end DISKREAD "Disk Read KB per second (Stacked)" 1
chart_add_unstacked DISKREAD "Disk Read KB per second (UnStacked)" 

chart_start DISKWRITE
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^DISKWRITE,D $INPUT | cut -f 3- -d, | sed -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^DISKWRITE,T $INPUT | sed -e "s/DISKWRITE,/#\[\'/" -e  "s/,/\',/"    -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end DISKWRITE "Disk Write KB per second (Stacked)" 1
chart_add_unstacked DISKWRITE "Disk Write KB per second (UnStacked)" 

chart_start DISKXFER
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^DISKXFER,D $INPUT | cut -f 3- -d, | sed -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^DISKXFER,T $INPUT | sed -e "s/DISKXFER,/#\[\'/" -e  "s/,/\',/"    -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end DISKXFER "Disk Transfers per second" 0

chart_start DISKSERV
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^DISKSERV,D $INPUT | cut -f 3- -d, | sed -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^DISKSERV,T $INPUT | sed -e "s/DISKSERV,/#\[\'/" -e  "s/,/\',/"    -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end DISKSERV "Disk Avg service time/transfer (milli-seconds)" 0

chart_start DISKWAIT
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^DISKWAIT,A $INPUT | cut -f 3- -d, | sed -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^DISKWAIT,T $INPUT | sed -e "s/DISKWAIT,/#\[\'/" -e  "s/,/\',/"    -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end DISKWAIT "Average wait queue time for read/write transfers (milli-seconds)" 0


chart_start JFS
print -n "[{type: 'datetime', label: 'Datetime' },'"
grep ^JFSFILE,J $INPUT | cut -f 3- -d, | sed -e  "s/,/\',\'/g" -e  "s/\$/\\']/"
grep ^JFSFILE,T $INPUT | sed -e "s/JFSFILE,/#\[\'/" -e  "s/,/\',/"    -e  "s/\$/\\]/" -e  "s/#/,/"
chart_end JFS "Journal File System Percent Full" 0

html_mid

chart_button PHYSICAL_CPU      black
chart_button POOLIDLE  black

chart_button CPU_UTIL      red

chart_button CPU_USE      red
chart_button RUNQ      red
chart_button PSWITCH   red
chart_button SYSCALL   red
chart_button READWRITE red
chart_button FORKEXEC  red
chart_button FILEIO    red
chart_button REALMEM   blue
chart_button REALMEMUSE   blue
chart_button PAGESPACE    blue
chart_button PAGING    blue
chart_button SWAPIN    blue
$ECHO '<br>' 
chart_button NET       purple
chart_button NETPACKET purple
chart_button DISKBUSY  brown
chart_button DISKBUSYu  brown
chart_button DISKREAD  brown
chart_button DISKREADu  brown
chart_button DISKWRITE brown
chart_button DISKWRITEu brown
chart_button DISKXFER  brown
chart_button DISKSERV  brown
chart_button DISKWAIT  brown
chart_button JFS       orange

chart_draw

# Display the bottom text table with basic onfiguration data
COUNT=`grep ZZZZ $INPUT | wc -l`
grep "^AAA," $INPUT >$INFO
$ECHO "<br><table><tr><td>"
grep AAA,host, $INFO | sed 's/AAA,host,/<li>Host:/'
grep AAA,cpus, $INFO | sed 's/AAA,cpus,/<li>CPUs:/'
$ECHO "<td>"
grep AAA,date, $INFO | sed 's/AAA,date,/<li>Date:/'
grep AAA,time, $INFO | sed 's/AAA,time,/<li>Time:/'
$ECHO "<td>"
grep AAA,interval, $INFO | sed 's/AAA,interval,/<li>Interval:/'
$ECHO "<li>Snapshots:" $COUNT
$ECHO "<td>"
grep AAA,AIX, $INFO | sed 's/AAA,AIX,/<li>AIX Version:/'
grep AAA,progname, $INFO | sed 's/:/,/' | awk -F , '{printf "<li>Agent: %s\n", $3 }'
$ECHO "<td>"

$ECHO "<li>Author: Nigel Griffiths @mr_nmon"
$ECHO "<li>Graphed by topaschart v" $topaschart_version
$ECHO "<td>"
$ECHO "<li>Download & more info from:"
$ECHO "<li><a href=http://nmon.sourceforge.net>http://nmon.sourceforge.net</a>"
$ECHO "</table>"

html_end
}

################################### Main code starts here
# -- Check the parameters
if [[ "$1" == "" ]]
then
	$ECHO  $0 Error: No first parameter = topas data file = filename.csv
	hint
fi
if [[ "$2" == "" ]]
then
	$ECHO  $0 Error: No second parameter = outputfile = filename.html
	hint
fi
if [[ ! -r "$1" ]]
then
	$ECHO  $0 Error: Input file $1 is not readable
	hint
fi

# --- save the parameters to avoid confusion
ORIGINAL=$1
OUTPUT=$2



# --- make temporary file names
TMP=/tmp/topaschart.tmp.$$
SED=/tmp/topaschart.sed.$$
INFO=/tmp/topaschart.info.$$
TOP=/tmp/topaschart.top.$$
TOP20=/tmp/topaschart.top20.$$
TOPAWKS=/tmp/topaschart.topawks.$$
NET=/tmp/topaschart.net.$$
NETKB=/tmp/topaschart.netkb.$$
NETFR=/tmp/topaschart.netfr.$$
###$ECHO  tmp=$TMP sed=$SED

# Strip Control-M from input file as some people don't know how to use FTP!!!
# Strip out end-of-line commas and add zeros for missing data from topas output!!!
# Strip out "-0.0" and "-1.0" from Linux files - normally error warnings
INPUT=/tmp/topaschart.input.$$
sed -e 's/
//' -e 's/,,$//' -e 's/,$//' -e 's/,,/,0,/g' -e 's/-0.0/0.0/g' -e 's/-1.0/0.0/g' <$ORIGINAL >$INPUT

# --- do the bulk of the work and save in tmp file
generate >$TMP

# --- Finally fix the time stamps 
# Original Time and Date was too slow in graphing
# grep ZZZZ $INPUT| sed -e 's?ZZZZ,?s/?' -e 's?,?/?' -e 's?,? on ?' -e 's?$?/?' >$SED

# Just time i.e. date ignored for faster graphing
# Extract ZZZZ lines, transform to sed commands but split them out in to files of less that 1000 lines otherwise sed explodes
# grep ^ZZZZ,T $INPUT | sed -e 's/^M//' | sed -e 's?ZZZZ,?s/?' -e 's?,?/?' -e 's?,[0-9a-zA-Z-]*?/?' | split -l 999 - $SED 
# Note working around a topasout bug where only Jun and Jul start with lowercase letter i.e. "j"
grep ^ZZZZ,T $INPUT | sed -e 's/^M//' -e 's/-Jan-/- 0-/' -e 's/-Feb-/- 1-/' -e 's/-Mar-/- 2-/' -e 's/-Apr-/- 3-/' -e 's/-May-/- 4-/' -e 's/-[jJ]un-/- 5-/' -e 's/-[jJ]ul-/- 6-/' -e 's/-Aug-/- 7-/' -e 's/-Sep-/- 8-/' -e 's/-Oct-/- 9-/' -e 's/-Nov-/-10-/' -e 's/-Dec-/-11-/' | sed -e 's/^\(....\),\(T[0-9]*\),\(..\):\(..\):\(..\),\(..\)-\(..\)-\(....\)/s\/\2\/Date(\8, \7, \6, \3, \4, \5)\//' | split -l 999 - $SED 


for i in $SED*
do
	sed -f $i $TMP >$OUTPUT
	cp $OUTPUT $TMP  # ready for next loop
done

# --- make the output file user, group and other readable
chmod ugo+r $OUTPUT
# --- Cleanup
rm $TMP $SED* $INFO $INPUT $NET $NETKB $NETFR
