#!/usr/bin/perl -w

# Copyright (c) 1998,1999,2000,2003 Damien Miller.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Script to generate a GIF image from a traffic-vis report

use strict;
use Getopt::Long;

sub Version();
sub Usage();

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

my $traffic_tops = "/usr/sbin/traffic-tops";
my $gs = "/usr/bin/gs";
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";
}
