#!/usr/bin/perl -T

# This is a CGI program for displaying the Captrap main page.

# 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::Main qw(:mainpage :param :misc);


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

# make the main CGI page (when no parameters are given)
sub cgi_main {
  my $common = shift;
  my $params = shift;
  my $cgi = $common->{cgi};
  print $cgi->header;
  print mk_mainpage($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($common->{config});
  # check cgi_error before doing anything
  my $err = $cgi->cgi_error();
  if (defined($err)) {
    $err = "CGI error: $err";
    return cgi_bad_params($cgi, $param_info, $err);
  }
  if ($cgi->param()) {
    return cgi_handle_params($common, $param_info, \&cgi_redirect_check);
  }
  cgi_main($common, undef);
}


# either redirect or make main page
sub cgi_redirect_check {
  my $common = shift;
  my $params = shift;
  foreach my $submit (qw(select_views select_graph)) {
    if (defined($params->{$submit})) {
      return cgi_redirect($common, $params);
    }
  }
  return cgi_main($common, $params);
}

# redirect the browser to the requested page
sub cgi_redirect {
  my $common = shift;
  my $params = shift;
  my $cgi = $common->{cgi};
  my $viewer  = $common->{config}->{viewer};
  my $grapher = $common->{config}->{grapher};
  # return cgi_bad_params($cgi, mk_param_info(), "blah");
  # this should be set to 'submit', if defined at all
  if (defined($params->{select_views})) {
    if (! @{$params->{view}}) {
      return cgi_bad_params($cgi, mk_param_info(), "No views selected.");
    }
    my $views = join(',', @{$params->{view}});
    my $redir = "$viewer?views=$views" . add_now($params->{now});
    $redir .= "&item=$params->{item}"         if defined($params->{item});
    $redir .= "&patterns=$params->{patterns}" if defined($params->{patterns});
    print $cgi->redirect($redir);
    return;
  }
  if (defined($params->{select_graph})) {
    # we need to translate a few parameters first
    if (defined($params->{start})) {
      $params->{start} =~ s/ /T/;
    }
    if (defined($params->{end})) {
      $params->{end} =~ s/ /T/;
    }
    if (defined($params->{states})) {
      $params->{states} = join(',', @{$params->{states}});
    }
    my $new = new_graph_string($common->{config}, $params);
    print $cgi->redirect("$grapher?$new");
    return;
  }
  # if this happens, look at cgi_redirect_check()
  die "No 'submit' parameter set.";
}

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


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