#!/usr/bin/perl -T

# This is a CGI program that aggregates and displays network traffic
# information (usually as a graph).

# Copyright 2009 Corey Hickey


# This file is part of Captrap.
#
# Captrap is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Captrap is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Captrap.  If not, see <http://www.gnu.org/licenses/>.


use strict;
use warnings FATAL => 'all';

# for development using a different Captrap module
use lib "lib";
use Captrap qw(:cgi :config :db);
use Captrap::Graph qw(:graph :param);

# -----------------------------------------------------------------------------
# CGI handling
# -----------------------------------------------------------------------------

# print errors found by params_ok();
sub cgi_print_errors {
  my $common = shift; # hash ref
  my $err = shift; # array ref
  my $cgi = $common->{cgi};
  my $config = $common->{config};
  my $me = $common->{config}->{grapher};
  print $cgi->header;
  print $cgi->start_html('-title' => "$me errors");
  print $cgi->h1("Errors");
  print $cgi->p("Sorry--your parameters parsed ok, but they cannot be used:");
  foreach my $item (@$err) {
    print $cgi->p($item);
  }
  print $cgi->end_html();
}


# use provided cgi parameters to call the graphing subroutine
sub cgi_mk_graph {
  my $common = shift; # hash ref
  my $params = shift; # hash ref
  my $cgi = $common->{cgi};
  # do some additional parameter validation
  my $err = params_ok($common, $params);
  if (@$err) {
    # problems!
    cgi_print_errors($common, $err);
    return;
  }
  print $cgi->header(pick_mime($params->{output}));
  print mk_graph($common, $params);
}


# choose the MIME type based on the specified output
sub pick_mime {
  my $output = shift;
  return "text/html"     if ! defined($output); # usage page
  return "image/png"     if $output eq "png";
  return "image/svg+xml" if $output eq "svg";
  return "text/plain"    if $output eq "gnuplot";
  return "text/plain"    if $output eq "text";
  return "text/csv"      if $output eq "csv";
  return "text/csv"      if $output eq "csvraw";
  die "unknown MIME type for output $output\n";
}


# figure out what to do with supplied CGI parameters
sub do_cgi {
  my $common = shift;
  my $cgi = $common->{cgi};
  my $param_info = mk_param_info($common->{config});
  my $err = $cgi->cgi_error();
  if (defined($err)) {
    $err = "CGI error: $err";
    return cgi_bad_params($cgi, $param_info, $err);
  }
  return cgi_handle_params($common, $param_info, \&cgi_mk_graph);
}


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


my $config = parse_config();
my $common = {
  cgi    => mk_cgi(),
  config => $config,
  dbh    => mk_dbh($config),
};
do_cgi($common);
$common->{dbh}->disconnect();
exit(0);
