#!/usr/bin/perl -w

# traffic-togif
#
# Script to generate a GIF image from a traffic-vis report
#
# Copyright 1998-1999 Damien Miller <dmiller@ilogic.com.au>
#
# This software is licensed under the terms of the GNU General 
# Public License (GPL). Please see the file COPYING for details.
# 
# $Id: traffic-togif,v 1.3 1999/03/27 00:12:50 dmiller Exp $

use strict;
use Getopt::Long;

my $x_size = 750;
my $y_size = 750;

my $traffic_tops = "/usr/sbin/traffic-tops";
my $gs = "/usr/bin/ghostscript";
my $pnmcrop = "/usr/bin/pnmcrop";
my $pnmscale = "/usr/bin/pnmscale";
my $ppmtogif = "/usr/bin/ppmtogif";

[ -x $traffic_tops ] || die "Cannot find traffic-tops.\n";
[ -x $gs ] || die "Cannot find ghostscript.\n";
[ -x $pnmcrop ] || die "Cannot find pnmcrop.\n";
[ -x $pnmscale ] || die "Cannot find pnmscale.\n";
[ -x $ppmtogif ] || die "Cannot find ppmtogif.\n";

my $tmpdir = "/tmp/traffic-togif.$>.$$";

my $input;
my $output;
my $help;
my $version;

my $commandline;
my $image_process;
my %options;

# Set up options 
$options{"help"}		= \$help;
$options{"version"}	= \$version;
$options{"V"}			= \$version;
$options{"input"}		= \$input;
$options{"output"}	= \$output;
$options{"x-size"}	= \$x_size;
$options{"y-size"}	= \$y_size;

GetOptions(\%options, "input=s", "output=s", "x-size=i", "y-size=i",
							 "help", "version", "V");

if ($help)
{
	Usage();
	exit(0);
}

if ($version)
{
	Version();
	exit(0);
}

# Create temporary directory
mkdir($tmpdir, 0700) or die "Couldn't make temporary directory.\n";

# Start building image generation commandline
$commandline = "$traffic_tops --suppress-title";

if (defined $input)
{
	$commandline .= " --input $input";
}

$commandline .= "| $gs -q -sDEVICE=ppmraw -g1785x2526 -r216  -sOutputFile=\'$tmpdir/out.ppm\'";

# Run commandline to create pnm file in temporary directory
system "$commandline >/dev/null 2>/dev/null";

# Build image manipulation commandline
$image_process = "$pnmcrop $tmpdir/out.ppm 2>/dev/null|$pnmscale -xysize $x_size $y_size 2>/dev/null|$ppmtogif 2>/dev/null";

if (defined $output)
{
	$image_process .= ">$output";
}

# Run command line to create gif file
system "$image_process";

# Clean up and remobe temp directory
unlink("$tmpdir/out.ppm") or die "Couldn\'t remove temporary file $tmpdir/out.ppm.\n";
rmdir($tmpdir) or die "Couldn\'t remove temporary directory $tmpdir.\n";

exit(0);

sub Usage()
{
	print STDERR 
"Usage: traffic-togif --output [file] [OPTIONS]

This script accepts a network traffic summary (as produced by 
traffic-collector(8) on standard input (or the file specifed by the 
--input option) and writes a GIF image to the file specified by the
--output option.

Options:
-o, --output file    Write output to specified file (mandatory).
-i, --input  file    Read input from specied file rather than stdin.
-x, --x-size pixels  Specify width of output image in pixels.
-y, --y-size pixels  Specify height of output image in pixels.
-V, --version        Print program version.
-h, --help           Print this help text.

";
}

sub Version()
{
	print STDERR "traffic-togif 0.30\n";
}
