#!/usr/bin/perl -T

# This is a CGI program for providing Captrap's textual information and for
# including graph images.

# 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(:db :cgi :config);
use Captrap::View qw(:view :param);


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

# make the main CGI page (when no parameters are given)
# print a CGI header and views
sub cgi_mk_views {
  my $common = shift; # hash ref
  my $params = shift; # hash ref
  my $cgi = $common->{cgi};
  print $cgi->header;
  print mk_views($common, $params);
}


# 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();
  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_views);
}

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


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