#!/usr/local/bin/perl
# Use the output from wwwstat to generate a set of graphs
# Copyright (C) 1994 1995
# Qiegang Long
#
# Department of Computer Science  or  Open Software Foundation  
# University of Massachusetts         11 Cambridge Center
# Amherst, MA01003                    Cambridge, MA02142 
# QLONG@cs.umass.edu                  QLONG@osf.org
# 
# 
# This program is provided in the hope it is useful, but with absolutely 
# no warranty. You can distribute it under the terms of the GNU General 
# Public License as published by the Free Software Foundation;
# either version 2, or (at your option) any later version.
#
# The latest version of gwstat can be found
#          <ftp://dis.cs.umass.edu/pub/gwstat.tar.gz>
#
# The documentation can be found at
#          <http://dis.cs.umass.edu/stats/gwstat.html
#
# To run this program, you must have
#  a) wwwstat1.0 (http://www.ics.uci.edu/WebSoft/wwwstat/), 
#  b) xmgr v3.xx (ftp://ftp.teleport.com/pub/users/pturner/acegr/)
#  c) ImageMagick (ftp://ftp.x.org/contrib/applications/ImageMagick)
#  e) ghostscript (ftp://prep.ai.mit.edu/pub/gnu/ghostscript-2.6.1.tar.gz) or 
#     ghostscript 3.xx (ftp://ftp.cs.wisc.edu/pub/ghost/aladdin)
#  
# 
# This program uses the output from wwwstat to generate a set of  graphs.
# Check <http://dis.cs.umass.edu/> for an example.
# The program should be executed AFTER you execute wwwstat. Please 
# refer to the documenation come with wwwstat to see how you should run 
# wwwstat. For gwstat, you must use command line options to decide what
# graphs you'd like  to generate. Use "gwstat -h" to get the details. 
# You might  want to set some variables below.
#
# Please note the "Daily Transmission Statistics" generated by wwwstat
# is sorted from the past to the present (strange!). 
# Find  "sub datecompare" in  wwwstat, 
# and change "$date1 - $date2;" to "$date2 - $date1;" will get 
# the correct order gwstat needs.
# 
#
# $Log: gwstat,v $
# Revision 2.0  1995/10/04  20:11:40  qlong
# Support alpha release of gr which is X-less and Motif-less.
# Added "-I" and "-T" options.
# Done 't use this version if you have Motif. 1.13 is more stable.
#
# Revision 1.13  1995/07/02  04:01:53  qlong
# Add by-year graph.
#
# Revision 1.12  1995/01/03  20:13:55  qlong
# Change subtitle. Remove reference to "gwstat" from the graphs.
#
# Revision 1.11  1994/11/25  00:03:50  qlong
# Fix a bug to pick up the top entries for "by country" and "by
# archive".
#
# Add a new "-n" option to sort the value, instead of key for the about
# two graphs.
#
# Revision 1.10  1994/11/11  00:30:22  qlong
# Add by-archive graph.
#
# Revision 1.9  1994/10/10  07:11:21  qlong
# Fix a bug in locate_starting_date which did not work correctly in some
# cases. This is related to all the graphs which are related to the
# daily data.
#
# Revision 1.8  1994/10/06  01:26:47  qlong
# Fix a bug in prepare_data_by_day, so the "by-day" graph works well
# with "-t" option.
#
# Revision 1.7  1994/09/30  19:32:04  qlong
# Change "by-day" graph so it illustrates the daily traffic of the
# given month really by day.
#
# Add "recent" graph which shows the last seven days traffic.
#
# Minor change to "by-week" so it draws the bar from the past to the
# present.
#
# Fix the axis label for "by-hour".
#
# Revision 1.6  1994/09/24  22:10:07  qlong
# Add new by-month graph.
# Add command line option "-t". A date can be specified so that
# gwstat can process the data on and before that particular date,
# instead of "today" which is the default. This option affects by-day,
# by-week and by-month graphs.
# Minor fix: prevent 'convert' from putting too many colors in the
# chips.
#
# Revision 1.5  1994/08/18  20:05:21  qlong
# Allow to specify the colors.
# Slightly increase the xaxis label size for "by-week" graph.
# (gs3.00 has trouble with tiny font?)
#
# Revision 1.4  1994/08/06  20:31:50  qlong
# Fix a nasty bug to deal with uncontinuous data. Use real date as a
# guide to extract the data from the output of wwwstat.
# Minor fix: when preparing data, it will definitely stop at $end_mark.
#
# Revision 1.3  1994/08/02  20:12:20  qlong
# Fix a bug in by_country graph.
# Improve by_week graph.
# Fix the tick positions  for every graphs. 
#
# Revision 1.2  1994/07/28  19:22:05  qlong
# xmgr3.00 limits at most 40 user defined tick labels. To overcome this
# limitation, we only display top 40 countries when generating
# "by-country" graphs.
#
# Revision 1.1  1994/07/13  04:21:41  qlong
# Initial revision
#
#
# --------------------------------------------------------------------

$gwstat_version = '$Id: gwstat,v 2.0 1995/10/04 20:11:40 qlong Exp qlong $';

# --------------------------------------------------------------------
# Safe to use full path name to specify where to find getopts.pl
# The user who runs this script may not have set $PERLLIB correctly

$getoptfile = '/usr/local/lib/perl/getopts.pl';
do "$getoptfile" || die "Cannot find $getoptfile:$!\n";
 

# --------------------------------------------------------------------
# configurations
# Some of these variables can be reset by command-line option
# They are here to serve as the default values
# --------------------------------------------------------------------

# If you don't set $hostname, we will use the real `hostname`.
# You might want to specify this if `hostname` does not return 
# what you want (like www.x.y.z which is an alias of foo.x.y.z)

$hostname = '';

# where is grbatch (from xmgr or gr package)
#

$generator = "/users/qlong/bin/HP-UX/grbatch";

# where is convert (from ImageMagick, which requires ghostscript package.)
# this is optional 

$convertor = "/usr/local/bin/convert";

# tell me where I can write tmp files

$tmp = "/tmp";   

# where do you want the generated graphic files go?

$destination = "/nfs/cen/u0/dis/ftp/httpd/docs/stats";


# the names for the graphic files.
# Will append ".gif" or "-chip.gif" for large and small icon files.
# E.g. "x.gif" or "x-chip.gif" for "x".

$daily = "";
$monthly = "";
$yearly = "";
$weekly = "";
$hourly = "";
$recent = "";
$by_country = "";
$by_archive = "";
$end_mark='</PRE>';

# where can I find the output of wwwstat?

$stat_file = "/nfs/cen/u0/dis/ftp/httpd/docs/stats/wwwstats.html";

# the default size of the chip image

$csize = "100x100";

# the default number of color allocated for the chip image
# this value should be no less than 6. Be careful when you 
# increase its value, as it may slow down your Mosaic, and used up too many
# colors

$ccolor = 10; 

# --------------------------------------------------------------------
# You should NOT change these seperators which are defined in wwwstat
#

$daily_sep = '<H2><A NAME="Daily">Daily Transmission Statistics</A></H2>';
$hourly_sep = '<H2><A NAME="Hourly">Hourly Transmission Statistics</A></H2>';
$domain_sep = '<H2><A NAME="Domain">Total Transfers by Client Domain</A></H2>';
$archive_sep = '<H2><A NAME="Archive">Total Transfers from each Archive Section</A></H2>';

# --------------------------------------------------------------------
# No lines below needs be changed.

($myname) = getpwuid($<);

@legends;               # store the legends;
@country;               # store the mapping of country
@archive;               # store the mapping of archive
@weeklabels;            # store the week
$rtime = time;          # the  time
$ltime = $rtime;

$transparency;          # controls the fig
$interlaced;

@month_names = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

%name_months = ('Jan', 1, 'Feb', 2, 'Mar',3, 'Apr',4, 'May',5, 'Jun',6, 
'Jul',7,'Aug', 8, 'Sep',9, 'Oct',10, 'Nov',11, 'Dec', 12);

@labels;                # used to pass different labels

%colors =
    ('white', 0, 'black', 1, 'red', 2, 'green', 3, 'blue', 4,
     'yellow', 5, 'RosyBrown', 6, 'gray', 7, 'DarkOrchid', 8,
     'cyan', 9, 'magenta', 10, 'orange', 11, 'DarkViolet', 12,
     'brown', 13, 'turquoise', 14, 'LimeGreen', 15);

# default color 
$bg = 'black';         #frame background
$fill = 'red';         #bar fill color
$fill2 = 'blue';         #another bar fill color
$outline = 'yellow';   #bar outline color

#  some global variables

$bgc;
$fillc;
$fillc2;
$outlinec;
$sort_by_number;

$day_in_second = 24 * 60 * 60;   # seconds per day

$debug;            #debug on?

# --------------------------------------------------------------------
# usage

sub usage {
  die <<"EndUsage";
$gwstat_version
usage: gwstat [-h] [-v] [-d destination] [-i input] [-s chip_size]
	      [-D daily_name]  [-H hourly_name] [-M monthly_name]
	      [-W weekly_name] [-C name] [-R name] [-A name]
	      [-b background] [-f bar filling color]
	      [-o bar outline color] [-t date] [-n] [-I] [-T]
Generate a set of graphic files based on the results from wwwstat. 
Options:
  -h  Help.
  -i  The input html file generated by wwwstat. 
      Default: $stat_file
  -d  Where to put the graphic files?
      Default: $destination
  -n  Used along with "-A" or "-C", if present, the output will be sorted
      by the access number, otherwise be sorted by country or by archive.
  -s  The size of chip image.
      Default: $csize
  -I  the graphs are interlaced.
  -T  the graphs have transparent background.
  -A  the filename for by-archive graphs.
  -D  the filename for daily graphs.
      Example: Given the name "daily", generate "daily.gif" and
               "daily-chip.gif". 
  -H  the filename for hourly graphs.
  -W  the filename for weekly graphs.
  -M  the filename for monthly graphs.
  -Y  the filename for yearly graphs.
  -C  the filename for by-country graphs.
  -R  the filename for recent seven days graphs.

  -t  process the data on and before the date for daily, weekly and monthly
      graphs. Example: "07/03/94".
  -b  the frame  background color.
      Default: $bg
  -f  the bar filling color(s), like "red" or "red blue". 
      Default: $fill
  -o  the bar outline color
      Default: $outline
  -v  Do not remove the tmp files (for debug purpose).
Known colors are: white, black, red, green, blue, yellow, RosyBrown, gray, 
DarkOrchid, cyan, magenta, orange, DarkViolet, brown, turquoise and LimeGreen.
Example:
   % gwstat -D "daily" -W "weekly"
   will generate four graphics for daily and weekly reports.
EndUsage
}
# --------------------------------------------------------------------
# process the command line options

sub process_command_line {

  do Getopts('TIhvnd:A:D:H:R:M:Y:W:C:i:s:b:f:o:t:');


  if (($opt_d) && ($opt_d != 1)) { $destination = $opt_d;}
  if (($opt_i) && ($opt_i != 1)) { $stat_file = $opt_i; }
  if (($opt_D) && ($opt_D != 1)) { $daily = $opt_D;}
  if (($opt_R) && ($opt_R != 1)) { $recent = $opt_R;}
  if (($opt_H) && ($opt_H != 1)) { $hourly = $opt_H; }
  if (($opt_M) && ($opt_M != 1)) { $monthly = $opt_M; }  
  if (($opt_Y) && ($opt_Y != 1)) { $yearly = $opt_Y; }
  if (($opt_W) && ($opt_W != 1)) { $weekly = $opt_W; }
  if (($opt_A) && ($opt_A != 1)) { $by_archive = $opt_A; }
  if (($opt_C) && ($opt_C != 1)) { $by_country = $opt_C; }
  if (($opt_s) && ($opt_s != 1)) { $csize = $opt_s; }
  if (($opt_b) && ($opt_b != 1)) { $bg = $opt_b; }
  if (($opt_f) && ($opt_f != 1)) { ($fill, $f2) = split(/\s+/, $opt_f);
				   $fill2 = $f2 if ($f2); }
  if (($opt_o) && ($opt_o != 1)) { $outline = $opt_o; }
  if ($opt_n) { $sort_by_number = 1; }
  if ($opt_t) { $ltime = &adjust_time($opt_t); }
  
  if ($opt_v) { $debug = 1; }
  if ($opt_T) { $transparency = '-gift'; }
  if ($opt_I) { $interlaced = '-gifi'; }
  if ($@ || ($opt_h)) { &usage; }


  1;
}


# --------------------------------------------------------------------

sub adjust_time {
    local ($date) = shift(@_);
    
    if ($date =~ m|(\d\d)/(\d\d)/(\d\d)|) {
	return $rtime - (&jday((localtime($rtime))[4] + 1,
				 (localtime($rtime))[3],
				 (localtime($rtime))[5] + 1900)	 -
			   &jday($1, $2, $3 + 1900)) * $day_in_second;}
    else {
	die "\n Date format should be mm/dd/yy.\n"; }
    
}

			    
# --------------------------------------------------------------------
# how many days in this month?

sub days_of_month {
    local ($ti) = shift(@_);
    local ($month, $day, $tt);

    $month = (localtime($ti))[4];

    $day = (localtime($ti))[3];
    $tt = 0;

    while ($month == (localtime($ti))[4]) {

	$ti = $ti + $day_in_second; 
	$tt++; }

    return $day + $tt - 1;

}
	
# --------------------------------------------------------------------

sub open_stat_file {
  open(sfh, $stat_file) 
    || die "\nCannot open wwwstat output $stat_file: $!\n";
}

# --------------------------------------------------------------------
# position file pointer at  the beginning of the sought data
# $separator specify which data we are looking for

sub locate_stat_data {
  local ($seperator) = shift(@_);

  seek(sfh, 0, 0) || die "$!\n";
  while (<sfh>) {
    if (eof(sfh)) {return undef;}
    chop;
    if ($_ eq $seperator) { 
      <sfh>; <sfh>; <sfh>;  
      return 1;}
  }
}


# --------------------------------------------------------------------
# position file pointer at  the beginning of the desired date

sub locate_starting_date {
  local ($time) = shift(@_);
  local ($length, @items, $jday);
  
  $jday = &jday((localtime($time))[4] + 1,
		(localtime($time))[3],
		(localtime($time))[5] + 1900);


  $_ = <sfh>;
  if ((m|$end_mark|) || (eof(sfh))) {return undef;}
  $_ =~ /^[.0-9\s]+\s[0-9]+\s\|\s+([a-zA-Z]+)\s+([0-9]+)\s+([0-9]+)/;

  $length = length;

  while ($jday < &jday($name_months{$1}, $2, $3)) {
      $_ = <sfh>;
      if ((m|$end_mark|) || (eof(sfh))) {return undef;}
      $_ =~ /^[.0-9\s]+\s[0-9]+\s\|\s+([a-zA-Z]+)\s+([0-9]+)\s+([0-9]+)/;
      $length = length; }

  seek(sfh, $length * (-1), 1);

  1;
}
  
  

  
# --------------------------------------------------------------------

sub date_of_time {
    local ($time) = shift(@_);

    return sprintf("%s %2d %4d", 
		   $month_names[(localtime($time))[4]],
		   (localtime($time))[3],
		   (localtime($time))[5] + 1900);

}
    

# --------------------------------------------------------------------

sub jday 
{
	local($m,$d,$y) = @_;
	local($ya,$c);

	$y = (localtime(time))[5] + 1900  if ($y eq '');

	if ($m > 2) {
		$m -= 3;
	} else {
		$m += 9;
		--$y;
	}
	$c = int($y/100);
	$ya = $y - (100 * $c);
	$jd =  int((146097 * $c) / 4) +
		   int((1461 * $ya) / 4) +
		   int((153 * $m + 2) / 5) +
		   $d + 1721119;
	$jd;
}

# --------------------------------------------------------------------
# extract the last 7 days data, put them into @requests_by_day;

sub prepare_data_of_last_week {
  local ($day, $sday, $time);
  local (@tt);

  $time = $ltime;

  return undef unless (&locate_stat_data($daily_sep));
  return undef unless (&locate_starting_date($time));
  
  $day = 6;
  open(wh, "> " . &temp_filename($recent .  '.dat')) || die "$!\n";

  @labels = ();
  
  while ($day >= 0) {
    if (eof(sfh)) {last;}
    $_ = <sfh>;
    if (m|$end_mark|) {last;}

    $sday = &date_of_time($time);
		    
    while (! /^[.0-9\s]+\s([0-9]+)\s\|\s+$sday.*/) {
	@tt=split(/\s+/, $sday);
	print wh "$day 0\n";
	$labels[$day] = "$tt[1]";
	$day--;

	if ($day < 0) {close(wh); return 1;}

	$time = $time - $day_in_second;
        $sday = &date_of_time($time);
    }

    if (/^[.0-9\s]+\s([0-9]+)\s\|\s+($sday).*/) {
	@tt=split(/\s+/, $2);
	print wh "$day $1\n";
	$labels[$day] = "$tt[0] $tt[1]";
    }
    
    $time = $time - $day_in_second;
    $day--;
}

 while ($day >= 0) {
     $sday = &date_of_time($time);
     @tt=split(/\s+/, $sday);
     print wh "$day 0\n";
     $labels[$day] = "$tt[0] $tt[1]";
     $time = $time - $day_in_second;
     $day--; }

  close(wh);
  1;
}
  

# --------------------------------------------------------------------
# prepare data for the last four weeks

sub prepare_data_by_week {
  local ($time, $day, $sday, $week, $sum, $weekl, $start);
  local ($read);

  $time = $ltime;
  return undef unless (&locate_stat_data($daily_sep));
  return undef unless (&locate_starting_date($time));  

  open(wh, ">" . &temp_filename($weekly . '.dat')) || die "$!\n";

  $read = 1;
  for ($week = 3; $week >= 0; $week--) {
    $sum = 0; 
    $weekl = '';
    for ($day = 0; $day < 7; $day++) {
      if (eof(sfh)) {
	  $weekl = $start . "-" . $weekl;
	  $weeklabels[$week] = $weekl;
	  print wh "$week $sum\n";
	  last;}

      $_ = <sfh> if ($read); 

      if (m|$end_mark|) {
	  $weekl = $start . "-" . $weekl;
	  $weeklabels[$week] = $weekl;
	  print wh "$week $sum\n"; 
	  last;}

      $sday = sprintf("%s %2d", 
		      $month_names[(localtime($time))[4]],
		      (localtime($time))[3]);

      if ($day == 0) {$weekl = $sday;}
      if ($day == 6) {$weekl = $sday . "-" . $weekl;}
      $start = $sday;

      if (/^[.0-9\s]+\s([0-9]+)\s\|\s+$sday.*/) {
	  $read = 1;

	  $sum = $sum + $1;}
      else {$read = 0; }

      $time = $time - $day_in_second;
  }

    $weeklabels[$week] = $weekl;
    print wh "$week $sum\n";
  }

  close(wh);
  1;
}

  
# --------------------------------------------------------------------
# prepare data by month

sub prepare_data_by_month {
    local ($time) = shift(@_);
    local ($month, $year, $monthn, $sum, $tt); 
    local (@months);

    $time = $ltime unless ($time);

    ($month, $year) = (localtime($time))[4,5];
    $year = $year + 1900;
    return undef unless (&locate_stat_data($daily_sep));
    return undef unless (&locate_starting_date($time));

    open(wh, ">" . &temp_filename($monthly . '.dat')) || die "$!\n";
    
    $tt = 11;
    while ($tt > $month) {
	$months[$tt] = 0;
	print wh "$tt 0\n";
	$tt--; }

    $_ = <sfh>;
    while ($month >= 0)  {
	
	$monthn = $month_names[$month];  
	$sum = 0;

	if (eof(sfh)) {last;}
	if (m|$end_mark|) {last;}
	
	while (/^[.0-9\s]+\s([0-9]+)\s\|\s+$monthn\s+\d+\s$year/) {
	    $sum = $sum + $1; 
	    $_ = <sfh>;
	    if ((m|$end_mark|) || (eof(sfh))) {last;} }

        $months[$month] = $sum;
	printf wh "$month $sum\n";
	
	$month--; }

    while ($month >= 0) {
	$months[$month] = 0;
	print wh "$month 0\n";
	$month--; }

    close(wh);
    @months;
}

# --------------------------------------------------------------------
# prepare data by year

sub prepare_data_by_year {
    local ($time, $tt, $year, @this_year, @last_year); 
    
    $time = $ltime;
    $year = (localtime($time))[5] - 1;
    @this_year = &prepare_data_by_month;
    @last_year = &prepare_data_by_month (&adjust_time("12/31/$year"));

    open(wh, ">" . &temp_filename($yearly . '.dat')) || die "$!\n";

    if ($#this_year == 0) {
	for ($tt=0; $tt<=11; $tt++) {
	    $this_year[$tt] = 0; }}
    
    if ($#last_year == 0) {
	for ($tt=0; $tt<=11; $tt++) {
	    $last_year[$tt] = 0; }}

    for ($tt=0; $tt<=11; $tt++) {
	printf wh "$tt $this_year[$tt] $last_year[$tt]\n"; }
    

    close(wh);
    system ("rm " . &temp_filename($monthly . '.dat'));
    1;
}


# --------------------------------------------------------------------
# prepare data by day


sub prepare_data_by_day {
    local ($time, $month, $monthn, @days, $tt, $end); 


    $time = $ltime;

    $end = &days_of_month($time);
    $month = (localtime($time))[4];
    $monthn = $month_names[$month];  

    return undef unless (&locate_stat_data($daily_sep));
    return undef unless (&locate_starting_date($time));

    open(wh, ">" . &temp_filename($daily . '.dat')) || die "$!\n";
    
    for ($tt = 1; $tt <=$end; $tt++) { $days[$tt] = 0; }
	
    $_ = <sfh>;

    while (/^[.0-9\s]+\s([0-9]+)\s\|\s+$monthn\s+([0-9]+).+/) {
	$days[$2] = $1;
	$_ = <sfh>;
	if ((m|$end_mark|) || (eof(sfh))) {last;} }

    for ($tt = 1; $tt <=$end; $tt++) { print wh "$tt $days[$tt]\n"; }

    close(wh);
    1;
}

# --------------------------------------------------------------------
# prepare hourly data 

sub prepare_data_by_hour {
  local ($hour, $shour);

  return undef unless (&locate_stat_data($hourly_sep));
  open(wh, ">" .&temp_filename($hourly . '.dat')) || die "$!\n";

  $hour = 0;
  while ($hour <24) {

    if (eof(sfh)) {last;}
    $_ = <sfh>;
    if (m|$end_mark|) {last;}

    $shour = sprintf("%02d", $hour);

    while (! /^[.0-9\s]+\s([0-9]+)\s\|\s+$shour/) {
	print wh "$hour 0\n"; 
	$hour++;
	if ($hour >=24) {close(wh); return 1; }
	$shour = sprintf("%02d", $hour);}

    if (/^[.0-9\s]+\s([0-9]+)\s\|\s+$shour/) {
	print wh "$hour $1\n"; }
    $hour++;

  }
 
  close(wh);
  1;
  
}

# --------------------------------------------------------------------
# prepare country data 

sub prepare_data_by_country {
  local ($i) = 0;
  local (%c_v);
  local (@vc);
  local ($c, $v);

  return undef unless (&locate_stat_data($domain_sep));
  open(wh, ">" .&temp_filename($by_country . '.dat')) || die "$!\n";

  while (1) {
    if (eof(sfh)) {last;}
    $_ = <sfh>;
    if (m|$end_mark|) {last;}
    if (/^[.0-9\s]+\s([\d]+) \| \S+\s+(.+)/) {
	$c_v{$2} = $1;
    }}
  
  # construct "value country" pair
  while (($c,$v) = each %c_v) {
      $vc[$i++] = sprintf("%040d %s", $v, $c);
  }				
 
  # sort "value country" pair
  @vc = sort { $b <=> $a } @vc;

  $i = 0; %c_v = ();
  
  while (($i <= 39) && ($i <= $#vc)) {
      $vc[$i] =~ m|0*([1-9].+)|; 
      ($v, $c) = split(/\s+/, $1, 2);
      $c_v{$c} = $v;		
      
      if ($sort_by_number) {
	  $country[$i] = $c;	
	  print wh "$c_v{$c} $i\n"; }
      $i++; }

  #sort by "country"

  $i = 0; 

  if ($sort_by_number != 1) {
      for $c (sort(keys %c_v)) {
	  $country[$i] = $c;	
	  print wh "$c_v{$c} $i\n";
	  $i++; } }
  
  close(wh);
  1;
  
}


# --------------------------------------------------------------------

# prepare archive data 

sub prepare_data_by_archive {
  local ($i) = 0;
  local (%a_v);
  local (@va);
  local ($c, $v);

  return undef unless (&locate_stat_data($archive_sep));
  open(wh, ">" .&temp_filename($by_archive . '.dat')) || die "$!\n";

  while (1) {
    if (eof(sfh)) {last;}
    $_ = <sfh>;
    if (m|$end_mark|) {last;}
    if (m#^[.0-9\s]+\s([\d]+) \|\s+(.+)#) {
	$a_v{$2} = $1;
    }}
  
  # construct "value archive" pair
  while (($a,$v) = each %a_v) {
      $va[$i++] = sprintf("%040d %s", $v, $a);
  }				
 
  # sort "value archive" pair
  @va = sort { $b <=> $a } @va;

  $i = 0; %a_v = ();
  
  while (($i <= 39) && ($i <= $#va)) {
      $va[$i] =~ m|0*([1-9].+)|; 
      ($v, $a) = split(/\s+/, $1, 2);
      $a_v{$a} = $v; 

      if ($sort_by_number) {
	  $archive[$i] = $a;	
	  print wh "$a_v{$a} $i\n"; }

      $i++; }

  #sort by "archive"
  $i = 0; 

  if ($sort_by_number != 1) {
      for $a (sort(keys %a_v)) {
	  $archive[$i] = $a;	
	  print wh "$a_v{$a} $i\n";
	  $i++; } }
  
  close(wh);
  1;
  
}

# --------------------------------------------------------------------


sub do_it {
  local ($date) = `date`;
  local ($subtitle, $month, $monthn, $tt, $end);
  &process_command_line;  

  # setup colors

  if (defined($colors{$bg})) {
      $bgc = $colors{$bg};}
  else {
      die "Invalid color $bg.\n"; }

  if (defined($colors{$fill})) {
      $fillc = $colors{$fill}; }
  else {
      die "Invalid color $fill.\n"; }

  if (defined($colors{$fill2})) {
      $fillc2 = $colors{$fill2}; }
  else {
      die "Invalid color $fill2.\n"; }



  if (defined($colors{$outline})) {
      $outlinec = $colors{$outline};}
  else {
      die "Invalid color $outline.\n"; } 


  chop($date);

  if (!$hostname) {
      $hostname = `hostname`; chop($hostname); }

  $subtitle = "$hostname $date";

  &open_stat_file;	
  
  # can I write in $tmp directory?
  die "$tmp is not a directory:$!\n" unless -d $tmp;
  die "Cannot write in directory $tmp:$!\n" unless -w $tmp;

  
  if ($hourly) {

    for ($tt=0; $tt<=23; $tt++) { $labels[$tt] = $tt; }

    &prepare_parameter_with_defined_label 
	('Hourly Traffic Summary', $subtitle, 'bar',
	 'decimal', 'decimal',
	 -1, 24, 0, 10,
	 'Time', 'Requests');

    &prepare_data_by_hour;
    &generate_gifs($hourly, 'y');} 

  if ($recent) {

    &prepare_data_of_last_week;
    &prepare_parameter_with_defined_label 
	('Recent Seven-Day Traffic Summary', $subtitle, 'bar',
	 'decimal', 'decimal',
	 -1, 7, 0, 10,
	 ' ', 'Requests');
    &generate_gifs($recent, 'y');} 


  if ($daily) {

    $month = (localtime($ltime))[4];
    $monthn = $month_names[$month];  
    $end = &days_of_month($ltime);
    
    @labels = ();
    for ($tt=1; $tt<=$end; $tt=$tt+2) { $labels[$tt] = $tt; }
    
    &prepare_parameter_with_defined_label 
	("Daily Traffic Summary of $monthn",
	 $subtitle, 'bar',
	 'decimal', 'decimal',
	 0, $end+1, 0, 10,
	 '', 'Requests');
    &prepare_data_by_day;  
    &generate_gifs($daily, 'y');}

  if ($monthly) {
    &prepare_data_by_month;  
    @labels = @month_names;
    &prepare_parameter_with_defined_label
	('Monthly Traffic Summary', $subtitle, 'bar',
	 'decimal', 'decimal',
	 -1, 12, 0, 10, 
	 '', 
	 'Requests');
    &generate_gifs($monthly, 'y');}


  if ($yearly) {
      &prepare_data_by_year;  
      $legends[0] = (localtime($ltime))[5] + 1900;
      $legends[1] = $legends[0] -1; 
      @labels = @month_names;
      &prepare_parameter_with_defined_label
	  ('Two Year Traffic Summary', $subtitle, 'bar',
	 'decimal', 'decimal',
	 -1, 12, 0, 10, 
	 '', 
	 'Requests');
      &generate_gifs($yearly, 'y', '-nxy');
      undef @legends; }



  if ($weekly) {
    &prepare_data_by_week;
    @labels = @weeklabels;
    &prepare_parameter_with_defined_label
	('Weekly Traffic Summary', $subtitle, 'bar',
	 'decimal', 'decimal',
	 -1, 4, 0, 10, 
	 '', 
	 'Requests');
    &generate_gifs($weekly, 'y'); }

  if ($by_country) {
    &prepare_data_by_country;
    @labels = @country;		
    &prepare_parameter_with_defined_y_label
	('Traffic Summary by Country/Domain', $subtitle);
    &generate_gifs($by_country, 'x');}

  if ($by_archive) {
      &prepare_data_by_archive;	
      @labels = @archive;
      &prepare_parameter_with_defined_y_label
	  ('Traffic Summary by Archive', $subtitle);
      &generate_gifs($by_archive, 'x');}

   close(sfh);
  exec("rm " .  &temp_filename('bar.par')) unless ($debug);

}

&do_it;

# --------------------------------------------------------------------
# generate gif files, put them to where you want
# the input if the name of the input data from, $autoscale specify
# how to scale the input date ('x', 'y', 'xy')
# type is the input data type like "-xy " or "-nxy"

sub generate_gifs {
  local ($name,$autoscale,$type) = @_;
  local ($command);

  local ($par, $dat, $eps, $gif, $chip, $chip_w, $chip_h);

  ($chip_w, $chip_h)=split(/x/, $csize);


  $par = &temp_filename('bar.par');
  $dat = &temp_filename($name . '.dat');
  $eps = &temp_filename($name . '.eps');
  $gif = &temp_filename($name . '.gif');
  $chip = &temp_filename($name . '-chip.gif');


  $command = "$generator -autoscale $autoscale  " .
    "-gif $interlaced $transparency -printfile $gif " .
      "-noask -p $par $type $dat";

  system("$command");
#  system("$convertor $eps $gif");
#  system("$convertor -geometry $csize! -colors $ccolor $eps $chip");
  $command = "$generator -autoscale $autoscale -gif -gifp " .
             " $interlaced $transparency " .
             "0,$chip_w,0,$chip_h,0.1,2,2,2,2 " .
             "-printfile $chip " .
             "-noask -p $par $type $dat";
  system("$command");
  system("mv $gif $destination/$name" . '.gif');
  system("mv $chip $destination/$name" . '-chip.gif');
  system("rm  $dat") unless ($debug); 
  1;
}
  

sub temp_filename {
  return "$tmp/$myname" . "_" . shift(@_);
}
 

# --------------------------------------------------------------------
# prepare parmeter file with user defined labels
# --------------------------------------------------------------------

sub prepare_parameter_with_defined_label {
    local($title, $subtitle, $type, $x_tick_format, $y_tick_format,
	  $xmin, $xmax, $ymin, $ymax,
	  $x_label, $y_label) = @_;
    local ($par,$i, $ll);

  $ll = $#labels + 1;
  $par = &temp_filename('bar.par');
  open(fh, ">$par");

  print fh <<end_of_part1;
# ACE/gr parameter file
#
#
page 5
page inout 5
link page off
default linestyle 1
default linewidth 1
default color 1
default char size 1.000000
default font 4
default font source 0
default symbol size 1.000000
with g0
g0 on
g0 label off
g0 hidden false
g0 type $type
g0 autoscale type AUTO
g0 fixedpoint off
g0 fixedpoint type 0
g0 fixedpoint xy 0.000000, 0.000000
g0 fixedpoint format general general
g0 fixedpoint prec 6, 6
    world xmin $xmin
    world xmax $xmax
    world ymin $ymin
    world ymax $ymax
    view xmin 0.200000
    view xmax 0.850000
    view ymin 0.150000
    view ymax 0.850000
    title "$title"
    title font 4
    title size 1.500000
    title color 1
    title linewidth 1
    subtitle "$subtitle"
    subtitle font 4
    subtitle size 1.000000
    subtitle color 1
    subtitle linewidth 1
    s0 type xy
    s0 symbol 0
    s0 symbol size 1.000000
    s0 symbol fill 0
    s0 symbol color 1
    s0 symbol linewidth 1
    s0 symbol linestyle 1
    s0 symbol center false
    s0 symbol char 0
    s0 skip 0
    s0 linestyle 1
    s0 linewidth 1
    s0 color $outlinec
    s0 fill 1
    s0 fill with color
    s0 fill color $fillc
    s0 fill pattern 0
    s0 errorbar type BOTH
    s0 errorbar length 1.000000
    s0 errorbar linewidth 1
    s0 errorbar linestyle 1
    s0 errorbar riser on
    s0 errorbar riser linewidth 1
    s0 errorbar riser linestyle 1
    s0 xyz 0.000000, 0.000000
    s0 comment "qlong_w.dat"
    s1 type xy
    s1 symbol 0
    s1 symbol size 1.000000
    s1 symbol fill 0
    s1 symbol color 1
    s1 symbol linewidth 1
    s1 symbol linestyle 1
    s1 symbol center false
    s1 symbol char 0
    s1 skip 0
    s1 linestyle 1
    s1 linewidth 1
    s1 color $outlinec
    s1 fill 1
    s1 fill with color
    s1 fill color $fillc2
    s1 fill pattern 3
    s1 errorbar type BOTH
    s1 errorbar length 1.000000
    s1 errorbar linewidth 1
    s1 errorbar linestyle 1
    s1 errorbar riser on
    s1 errorbar riser linewidth 1
    s1 errorbar riser linestyle 1
    s1 xyz 0.000000, 0.000000
    s1 comment "qlong_w.dat"
    xaxis  tick on
    xaxis  tick major 1
    xaxis  tick minor 0.5
    xaxis  tick offsetx 0.000000
    xaxis  tick offsety 0.000000
    xaxis  label "$x_label"
    xaxis  label layout para
    xaxis  label place auto
    xaxis  label char size 1.000000
    xaxis  label font 4
    xaxis  label color 1
    xaxis  label linewidth 1
    xaxis  ticklabel on
    xaxis  ticklabel type spec
    xaxis  ticklabel prec 1
    xaxis  ticklabel format $x_tick_format
    xaxis  ticklabel append ""
    xaxis  ticklabel prepend ""
    xaxis  ticklabel layout horizontal
    xaxis  ticklabel skip 0
    xaxis  ticklabel stagger 0
    xaxis  ticklabel op bottom
    xaxis  ticklabel sign normal
    xaxis  ticklabel start type spec
    xaxis  ticklabel start 0.000000
    xaxis  ticklabel stop type auto
    xaxis  ticklabel stop 0.000000
    xaxis  ticklabel char size 0.77000
    xaxis  ticklabel font 4
    xaxis  ticklabel color 1
    xaxis  ticklabel linewidth 1
    xaxis  tick major on
    xaxis  tick minor on
    xaxis  tick default 6
    xaxis  tick in
    xaxis  tick major color 1
    xaxis  tick major linewidth 1
    xaxis  tick major linestyle 1
    xaxis  tick minor color 1
    xaxis  tick minor linewidth 1
    xaxis  tick minor linestyle 1
    xaxis  tick log off
    xaxis  tick size 1.000000
    xaxis  tick minor size 0.500000
    xaxis  bar off
    xaxis  bar color 1
    xaxis  bar linestyle 1
    xaxis  bar linewidth 1
    xaxis  tick major grid off
    xaxis  tick minor grid off
    xaxis  tick op both
    xaxis  tick type auto
    xaxis  tick spec $ll
end_of_part1
    for ($i = 0; $i <= $#labels; $i++) {
	print fh "    xaxis  tick $i, $i\n";
	print fh "    xaxis  ticklabel $i, \"" .  $labels[$i] . "\"\n";}
print fh <<"end_of_part2";
    yaxis  tick on
    yaxis  tick major 1000
    yaxis  tick minor 500
    yaxis  tick offsetx 0.000000
    yaxis  tick offsety 0.000000
    yaxis  label "$y_label"
    yaxis  label layout para
    yaxis  label place auto
    yaxis  label char size 1.000000
    yaxis  label font 4
    yaxis  label color 1
    yaxis  label linewidth 1
    yaxis  ticklabel on
    yaxis  ticklabel type auto
    yaxis  ticklabel prec 1
    yaxis  ticklabel format $y_tick_format
    yaxis  ticklabel append ""
    yaxis  ticklabel prepend ""
    yaxis  ticklabel layout horizontal
    yaxis  ticklabel skip 0
    yaxis  ticklabel stagger 0
    yaxis  ticklabel op left
    yaxis  ticklabel sign normal
    yaxis  ticklabel start type auto
    yaxis  ticklabel start 0.000000
    yaxis  ticklabel stop type auto
    yaxis  ticklabel stop 0.000000
    yaxis  ticklabel char size 1.000000
    yaxis  ticklabel font 4
    yaxis  ticklabel color 1
    yaxis  ticklabel linewidth 1
    yaxis  tick major on
    yaxis  tick minor on
    yaxis  tick default 6
    yaxis  tick in
    yaxis  tick major color 1
    yaxis  tick major linewidth 1
    yaxis  tick major linestyle 1
    yaxis  tick minor color 1
    yaxis  tick minor linewidth 1
    yaxis  tick minor linestyle 1
    yaxis  tick log off
    yaxis  tick size 1.000000
    yaxis  tick minor size 0.500000
    yaxis  bar off
    yaxis  bar color 1
    yaxis  bar linestyle 1
    yaxis  bar linewidth 1
    yaxis  tick major grid off
    yaxis  tick minor grid off
    yaxis  tick op both
    yaxis  tick type auto
    yaxis  tick spec 0
    zeroxaxis  tick on
    zeroxaxis  tick major 1
    zeroxaxis  tick minor 0.5
    zeroxaxis  tick offsetx 0.000000
    zeroxaxis  tick offsety 0.000000
    zeroxaxis  label ""
    zeroxaxis  label layout para
    zeroxaxis  label place auto
    zeroxaxis  label char size 1.000000
    zeroxaxis  label font 4
    zeroxaxis  label color 1
    zeroxaxis  label linewidth 1
    zeroxaxis  ticklabel off
    zeroxaxis  ticklabel type auto
    zeroxaxis  ticklabel prec 1
    zeroxaxis  ticklabel format decimal
    zeroxaxis  ticklabel append ""
    zeroxaxis  ticklabel prepend ""
    zeroxaxis  ticklabel layout horizontal
    zeroxaxis  ticklabel skip 0
    zeroxaxis  ticklabel stagger 0
    zeroxaxis  ticklabel op bottom
    zeroxaxis  ticklabel sign normal
    zeroxaxis  ticklabel start type auto
    zeroxaxis  ticklabel start 0.000000
    zeroxaxis  ticklabel stop type auto
    zeroxaxis  ticklabel stop 0.000000
    zeroxaxis  ticklabel char size 1.000000
    zeroxaxis  ticklabel font 4
    zeroxaxis  ticklabel color 1
    zeroxaxis  ticklabel linewidth 1
    zeroxaxis  tick major off
    zeroxaxis  tick minor on
    zeroxaxis  tick default 6
    zeroxaxis  tick in
    zeroxaxis  tick major color 1
    zeroxaxis  tick major linewidth 1
    zeroxaxis  tick major linestyle 1
    zeroxaxis  tick minor color 1
    zeroxaxis  tick minor linewidth 1
    zeroxaxis  tick minor linestyle 1
    zeroxaxis  tick log off
    zeroxaxis  tick size 1.000000
    zeroxaxis  tick minor size 0.500000
    zeroxaxis  bar off
    zeroxaxis  bar color 1
    zeroxaxis  bar linestyle 1
    zeroxaxis  bar linewidth 1
    zeroxaxis  tick major grid off
    zeroxaxis  tick minor grid off
    zeroxaxis  tick op both
    zeroxaxis  tick type auto
    zeroxaxis  tick spec 0
    zeroyaxis  tick on
    zeroyaxis  tick major 1000
    zeroyaxis  tick minor 500
    zeroyaxis  tick offsetx 0.000000
    zeroyaxis  tick offsety 0.000000
    zeroyaxis  label ""
    zeroyaxis  label layout para
    zeroyaxis  label place auto
    zeroyaxis  label char size 1.000000
    zeroyaxis  label font 4
    zeroyaxis  label color 1
    zeroyaxis  label linewidth 1
    zeroyaxis  ticklabel off
    zeroyaxis  ticklabel type auto
    zeroyaxis  ticklabel prec 1
    zeroyaxis  ticklabel format decimal
    zeroyaxis  ticklabel append ""
    zeroyaxis  ticklabel prepend ""
    zeroyaxis  ticklabel layout horizontal
    zeroyaxis  ticklabel skip 0
    zeroyaxis  ticklabel stagger 0
    zeroyaxis  ticklabel op left
    zeroyaxis  ticklabel sign normal
    zeroyaxis  ticklabel start type auto
    zeroyaxis  ticklabel start 0.000000
    zeroyaxis  ticklabel stop type auto
    zeroyaxis  ticklabel stop 0.000000
    zeroyaxis  ticklabel char size 1.000000
    zeroyaxis  ticklabel font 4
    zeroyaxis  ticklabel color 1
    zeroyaxis  ticklabel linewidth 1
    zeroyaxis  tick major off
    zeroyaxis  tick minor on
    zeroyaxis  tick default 6
    zeroyaxis  tick in
    zeroyaxis  tick major color 1
    zeroyaxis  tick major linewidth 1
    zeroyaxis  tick major linestyle 1
    zeroyaxis  tick minor color 1
    zeroyaxis  tick minor linewidth 1
    zeroyaxis  tick minor linestyle 1
    zeroyaxis  tick log off
    zeroyaxis  tick size 1.000000
    zeroyaxis  tick minor size 0.500000
    zeroyaxis  bar off
    zeroyaxis  bar color 1
    zeroyaxis  bar linestyle 1
    zeroyaxis  bar linewidth 1
    zeroyaxis  tick major grid off
    zeroyaxis  tick minor grid off
    zeroyaxis  tick op both
    zeroyaxis  tick type auto
    zeroyaxis  tick spec 0
end_of_part2
    if (defined(@legends)) {
	print fh "   legend on\n"; }
    else {
	print fh "   legend off\n"; }
print fh <<"end_of_part3";
    legend loctype view
    legend layout 0
    legend vgap 2
    legend hgap 1
    legend length 4
    legend box off
    legend box fill off
    legend box fill with color
    legend box fill color 0
    legend box fill pattern 1
    legend box color 1
    legend box linewidth 1
    legend box linestyle 1
    legend x1 0.432692
    legend y1 0.0807249
    legend font 4
    legend char size 1.000000
    legend linestyle 1
    legend linewidth 1
    legend color 1
end_of_part3
    for ($i = 0; $i <= $#legends; $i++) {
	print fh '    legend  string ' . $i . '"' . $legends[$i] . '"' . "\n"; }
print fh <<"end_of_part4";
    frame on
    frame type 0
    frame linestyle 1
    frame linewidth 1
    frame color 1
    frame fill on
    frame background color $bgc
end_of_part4
    close fh;
}
# --------------------------------------------------------------------
# prepare parmeter file for country. We can use &prepare_parameter_file,
# but I am lazy to do it.
# --------------------------------------------------------------------

sub prepare_parameter_with_defined_y_label {
  local($title, $subtitle) = @_;
  local ($par, $i, $ll);

  
  $ll = $#labels + 1;
  $par = &temp_filename('bar.par');
  open(fh, ">$par");

  print fh <<end_of_part1;
# ACE/gr parameter file
#
#
page 5
page inout 5
link page off
default linestyle 1
default linewidth 1
default color 1
default char size 1.000000
default font 4
default font source 0
default symbol size 1.000000
with g0
g0 on
g0 label off
g0 hidden false
g0 type hbar
g0 autoscale type AUTO
g0 fixedpoint off
g0 fixedpoint type 0
g0 fixedpoint xy 0.000000, 0.000000
g0 fixedpoint format general general
g0 fixedpoint prec 6, 6
    world xmin 0
    world xmax 1000
    world ymin -1
    world ymax $ll
    view xmin 0.300000
    view xmax 0.950000
    view ymin 0.150000
    view ymax 0.850000
    title "$title"
    title font 4
    title size 1.500000
    title color 1
    title linewidth 1
    subtitle "$subtitle"
    subtitle font 4
    subtitle size 1.000000
    subtitle color 1
    subtitle linewidth 1
    s0 type xy
    s0 symbol 0
    s0 symbol size 1.000000
    s0 symbol fill 0
    s0 symbol color 1
    s0 symbol linewidth 1
    s0 symbol linestyle 1
    s0 symbol center false
    s0 symbol char 0
    s0 skip 0
    s0 linestyle 1
    s0 linewidth 1
    s0 color $outlinec
    s0 fill 1
    s0 fill with color
    s0 fill color $fillc
    s0 fill pattern 0
    s0 errorbar type BOTH
    s0 errorbar length 1.000000
    s0 errorbar linewidth 1
    s0 errorbar linestyle 1
    s0 errorbar riser on
    s0 errorbar riser linewidth 1
    s0 errorbar riser linestyle 1
    s0 xyz 0.000000, 0.000000
    s0 comment "qlong_c.dat"
    xaxis  tick on
    xaxis  tick major 200
    xaxis  tick minor 100
    xaxis  tick offsetx 0.000000
    xaxis  tick offsety 0.000000
    xaxis  label "Requests"
    xaxis  label layout para
    xaxis  label place auto
    xaxis  label char size 1.000000
    xaxis  label font 4
    xaxis  label color 1
    xaxis  label linewidth 1
    xaxis  ticklabel on
    xaxis  ticklabel type auto
    xaxis  ticklabel prec 1
    xaxis  ticklabel format decimal
    xaxis  ticklabel append ""
    xaxis  ticklabel prepend ""
    xaxis  ticklabel layout horizontal
    xaxis  ticklabel skip 0
    xaxis  ticklabel stagger 0
    xaxis  ticklabel op bottom
    xaxis  ticklabel sign normal
    xaxis  ticklabel start type auto
    xaxis  ticklabel start 0.000000
    xaxis  ticklabel stop type auto
    xaxis  ticklabel stop 0.000000
    xaxis  ticklabel char size 1.000000
    xaxis  ticklabel font 4
    xaxis  ticklabel color 1
    xaxis  ticklabel linewidth 1
    xaxis  tick major on
    xaxis  tick minor on
    xaxis  tick default 6
    xaxis  tick in
    xaxis  tick major color 1
    xaxis  tick major linewidth 1
    xaxis  tick major linestyle 1
    xaxis  tick minor color 1
    xaxis  tick minor linewidth 1
    xaxis  tick minor linestyle 1
    xaxis  tick log off
    xaxis  tick size 1.000000
    xaxis  tick minor size 0.500000
    xaxis  bar off
    xaxis  bar color 1
    xaxis  bar linestyle 1
    xaxis  bar linewidth 1
    xaxis  tick major grid off
    xaxis  tick minor grid off
    xaxis  tick op both
    xaxis  tick type auto
    xaxis  tick spec 0
    yaxis  tick on
    yaxis  tick major 1
    yaxis  tick minor 1
    yaxis  tick offsetx 0.000000
    yaxis  tick offsety 0.000000
    yaxis  label ""
    yaxis  label layout para
    yaxis  label place auto
    yaxis  label char size 1.000000
    yaxis  label font 4
    yaxis  label color 1
    yaxis  label linewidth 1
    yaxis  ticklabel on
    yaxis  ticklabel type spec
    yaxis  ticklabel prec 1
    yaxis  ticklabel format decimal
    yaxis  ticklabel append ""
    yaxis  ticklabel prepend ""
    yaxis  ticklabel layout horizontal
    yaxis  ticklabel skip 0
    yaxis  ticklabel stagger 0
    yaxis  ticklabel op left
    yaxis  ticklabel sign normal
    yaxis  ticklabel start type spec
    yaxis  ticklabel start 0.000000
    yaxis  ticklabel stop type auto
    yaxis  ticklabel stop 0.000000
    yaxis  ticklabel char size 0.790000
    yaxis  ticklabel font 4
    yaxis  ticklabel color 1
    yaxis  ticklabel linewidth 1
    yaxis  tick major off
    yaxis  tick minor on
    yaxis  tick default 6
    yaxis  tick in
    yaxis  tick major color 1
    yaxis  tick major linewidth 1
    yaxis  tick major linestyle 1
    yaxis  tick minor color 1
    yaxis  tick minor linewidth 1
    yaxis  tick minor linestyle 1
    yaxis  tick log off
    yaxis  tick size 1.000000
    yaxis  tick minor size 0.500000
    yaxis  bar off
    yaxis  bar color 1
    yaxis  bar linestyle 1
    yaxis  bar linewidth 1
    yaxis  tick major grid off
    yaxis  tick minor grid off
    yaxis  tick op both
    yaxis  tick type auto
    yaxis  tick spec $ll
end_of_part1
    for ($i = 0; $i <= $#labels; $i++) {
	print fh "    yaxis  tick $i, $i\n";
	print fh "    yaxis  ticklabel $i, \"" .  $labels[$i] . "\"\n";}

print fh <<"end_of_part2";
    zeroxaxis  tick on
    zeroxaxis  tick major 200
    zeroxaxis  tick minor 100
    zeroxaxis  tick offsetx 0.000000
    zeroxaxis  tick offsety 0.000000
    zeroxaxis  label ""
    zeroxaxis  label layout para
    zeroxaxis  label place auto
    zeroxaxis  label char size 1.000000
    zeroxaxis  label font 4
    zeroxaxis  label color 1
    zeroxaxis  label linewidth 1
    zeroxaxis  ticklabel off
    zeroxaxis  ticklabel type auto
    zeroxaxis  ticklabel prec 1
    zeroxaxis  ticklabel format decimal
    zeroxaxis  ticklabel append ""
    zeroxaxis  ticklabel prepend ""
    zeroxaxis  ticklabel layout horizontal
    zeroxaxis  ticklabel skip 0
    zeroxaxis  ticklabel stagger 0
    zeroxaxis  ticklabel op bottom
    zeroxaxis  ticklabel sign normal
    zeroxaxis  ticklabel start type auto
    zeroxaxis  ticklabel start 0.000000
    zeroxaxis  ticklabel stop type auto
    zeroxaxis  ticklabel stop 0.000000
    zeroxaxis  ticklabel char size 1.000000
    zeroxaxis  ticklabel font 4
    zeroxaxis  ticklabel color 1
    zeroxaxis  ticklabel linewidth 1
    zeroxaxis  tick major off
    zeroxaxis  tick minor on
    zeroxaxis  tick default 6
    zeroxaxis  tick in
    zeroxaxis  tick major color 1
    zeroxaxis  tick major linewidth 1
    zeroxaxis  tick major linestyle 1
    zeroxaxis  tick minor color 1
    zeroxaxis  tick minor linewidth 1
    zeroxaxis  tick minor linestyle 1
    zeroxaxis  tick log off
    zeroxaxis  tick size 1.000000
    zeroxaxis  tick minor size 0.500000
    zeroxaxis  bar off
    zeroxaxis  bar color 1
    zeroxaxis  bar linestyle 1
    zeroxaxis  bar linewidth 1
    zeroxaxis  tick major grid off
    zeroxaxis  tick minor grid off
    zeroxaxis  tick op both
    zeroxaxis  tick type auto
    zeroxaxis  tick spec 0
    zeroyaxis  tick on
    zeroyaxis  tick major 10
    zeroyaxis  tick minor 5
    zeroyaxis  tick offsetx 0.000000
    zeroyaxis  tick offsety 0.000000
    zeroyaxis  label ""
    zeroyaxis  label layout para
    zeroyaxis  label place auto
    zeroyaxis  label char size 1.000000
    zeroyaxis  label font 4
    zeroyaxis  label color 1
    zeroyaxis  label linewidth 1
    zeroyaxis  ticklabel off
    zeroyaxis  ticklabel type auto
    zeroyaxis  ticklabel prec 1
    zeroyaxis  ticklabel format decimal
    zeroyaxis  ticklabel append ""
    zeroyaxis  ticklabel prepend ""
    zeroyaxis  ticklabel layout horizontal
    zeroyaxis  ticklabel skip 0
    zeroyaxis  ticklabel stagger 0
    zeroyaxis  ticklabel op left
    zeroyaxis  ticklabel sign normal
    zeroyaxis  ticklabel start type auto
    zeroyaxis  ticklabel start 0.000000
    zeroyaxis  ticklabel stop type auto
    zeroyaxis  ticklabel stop 0.000000
    zeroyaxis  ticklabel char size 1.000000
    zeroyaxis  ticklabel font 4
    zeroyaxis  ticklabel color 1
    zeroyaxis  ticklabel linewidth 1
    zeroyaxis  tick major off
    zeroyaxis  tick minor on
    zeroyaxis  tick default 6
    zeroyaxis  tick in
    zeroyaxis  tick major color 1
    zeroyaxis  tick major linewidth 1
    zeroyaxis  tick major linestyle 1
    zeroyaxis  tick minor color 1
    zeroyaxis  tick minor linewidth 1
    zeroyaxis  tick minor linestyle 1
    zeroyaxis  tick log off
    zeroyaxis  tick size 1.000000
    zeroyaxis  tick minor size 0.500000
    zeroyaxis  bar off
    zeroyaxis  bar color 1
    zeroyaxis  bar linestyle 1
    zeroyaxis  bar linewidth 1
    zeroyaxis  tick major grid off
    zeroyaxis  tick minor grid off
    zeroyaxis  tick op both
    zeroyaxis  tick type auto
    zeroyaxis  tick spec 0
    legend off
    legend loctype view
    legend layout 0
    legend vgap 2
    legend hgap 1
    legend length 4
    legend box off
    legend box fill off
    legend box fill with color
    legend box fill color 0
    legend box fill pattern 1
    legend box color 1
    legend box linewidth 1
    legend box linestyle 1
    legend x1 0.8
    legend y1 0.8
    legend font 4
    legend char size 1.000000
    legend linestyle 1
    legend linewidth 1
    legend color 1
    frame on
    frame type 0
    frame linestyle 1
    frame linewidth 1
    frame color 1
    frame fill on
    frame background color $bgc
end_of_part2
close fh;
}
