
package gsdlCGI;

use CGI;

@ISA = ('CGI');

sub new {
    my $class = shift @_;
    my ($mode) = @_;


    my $self;
    if ((defined $mode) && ($mode eq "+cmdline")) {
	my $line = <STDIN>;
	if ((defined $line) && ($line ne "")) {
	    $self = new CGI($line);
	}
	else {
	    $self = new CGI();
	}
    }
    else {
	$self = new CGI();
    }

    my $xml = (defined $self->param("xml")) ? 1 : 0;

    $self->{'xml'} = $xml;

    my @var_names = $self->param;
    my @arg_list = ();
    foreach my $n ( @var_names ) {
	my $arg = "$n=";
	my $val =  $self->param($n);
	$arg .= $val if (defined $val);
	push(@arg_list,$arg);
    }
	
    $self->{'args'} = join("&",@arg_list);

    return bless $self, $class;
}


sub clean_param
{
    my $self = shift @_;
    my ($param) = @_;

    my $val = $self->SUPER::param($param);
    $val =~ s/[\r\n]+$// if (defined $val);

    return $val;
}

sub safe_val
{
    my $self = shift @_;
    my ($val) = @_;

    # ensure only alpha-numeric plus a few other special chars remain

    $val =~ s/[^[:alnum:]@\.\/\- :]//g if (defined $val); 

    return $val;
}


sub generate_error
{
    my $self = shift @_;
    my ($mess) = @_;
    
    my $xml = $self->{'xml'};

    my $full_mess;
    my $args = $self->{'args'};

    if ($xml) {
	$full_mess =  "<Error>\n";
	$full_mess .= "  $mess\n";
	$full_mess .= "  CGI args were: $args\n";
	$full_mess .= "</Error>\n";
    }
    else {
	$full_mess = "ERROR: $mess ($args)\n";
    }

    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT $full_mess;

    die $full_mess;
}


sub generate_ok_message
{
    my $self = shift @_;
    my ($mess) = @_;
    
    my $xml = $self->{'xml'};

    my $full_mess;

    if ($xml) {
	$full_mess =  "<Accepted>\n";
	$full_mess .= "  $mess\n";
	$full_mess .= "</Accepted>\n";
    }
    else {
	$full_mess = "$mess\n";
    }

    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT $full_mess;
}



sub get_config_info {
    my $self = shift @_;
    my ($infotype) = @_;

    my $site_filename = "gsdlsite.cfg";
    open (FILEIN, "<$site_filename") 
	|| $self->generate_error("Could not open gsdlsite.cfg");

    my $config_content = "";
    while(defined (my $line = <FILEIN>)) {
	$config_content .= $line;
    }
    close(FILEIN);

        
    my ($loc) = ($config_content =~ m/^$infotype\s+(\S+)\s*\n/m);
    $loc =~ s/\"//g;

    if ((!defined $loc) || ($loc =~ m/^\s*$/)) {
	$self->generate_error("$infotype is not set in gsdlsite.cfg");
    }


    return $loc;
}



sub rm_rf
{
    my $self = shift @_;
    my ($dir) = @_;

    # Delete recursively with force flag on

    # Currently, Unix specific, need to generalise to Windows etc.
    # => hook in with util.pm ?

    my $cmd = "/bin/rm -rf $dir 2>&1"; # Unix specific

    my $output = `$cmd`;

    if ($?>0) {
	$self->generate_error("Failed to delete directory: $dir\n\n$output");
    }
}

sub unix_cmd
{
    my $self = shift @_;
    my ($cmd) = @_;

    $cmd = "( $cmd ) 2>&1"; 

    my $output = `$cmd`;

    if ($?>0) {
	$self->generate_error("Failed to run command: $cmd\n\n$output");
    }
}

sub checked_chdir
{
    my $self = shift @_;
    my ($dir) = @_;

    if (!-e $dir) {
	$self->generate_error("Directory '$dir' does not exist");
    }

    chdir $dir
	|| $self->generate_error("Unable to change to directory: $dir");
}

1;
