#!/usr/bin/perl
BEGIN {
    use CGI::Carp qw(carpout fatalsToBrowser);  #
          open(LOG, ">>mycgi-log") or
              die("Unable to open mycgi-log: $!\n");
    carpout(LOG);
}

use strict;
use lib '.';
use rootSystem;
use CGI qw(:standard :html);
use Data::Dumper;
use Text::Wrap;
use spherical;
use sphericalExamples;

my %actions=(
	     'help' => \&helpPage,
	     'View' => \&display,
	     'Download' => \&display,
	     );
my $modelsDir="/home/atlas/www/weyl/integral"; #for character tables
my $modelsDirUrl="/weyl/integral";

my $submit=param('Submit');
my $action=$actions{$submit}||\&main;
warn CGI::Dump();
&$action;

sub main{
    my $message=shift;
    top();
    print center(h3("Root Systems"));
    print "<font color=red>$message</font>",p if ($message);
#    print CGI::Dump();
    my @types=qw(A B C D E F F* G G*);
    print 
    "This page contains information about root systems, including
choices of coordinates.",p," It is useful in conjuction with the ",
	a({-href=>'spherical.cgi'},'Spherical Unitary Explorer'),  p,
	start_form,		    
    strong('Type:'),popup_menu(-name=>'type',
		     -values=>\@types
		     ),'   ',
		     strong('Rank:'),
	  textfield(-name=>'rank',-size=>3),
		     p,strong('Labelling of the Dynkin Diagram:'),
		     popup_menu(-name=>'coordinates',
				-values=>['Bourbaki','Gap','Other']),
		     strong('Other:'),
		     textfield(-name=>'otherCoordinates',-size=>20),p,
		     submit(-name=>'Submit' ,-label=>'View'),
		     submit(-name=>'Submit' ,-label=>'Download'),
	  end_form,
p,
"Types F* and G* are the roots sytems of type F and G, with coordinates dual to the standard (Bourbaki) coordinates.",p,
"There are two standard labellings of the Dynkin Diagram: Bourbaki and Gap. 
These differ in types B,C,D and G. In the Bourbaki labelling, in types B,C, and D, the double bond or fork is
near the node labelled n. The Gap labelling is the opposite of this. (In G<sub>2</sub> the short root is labelled 1 in the Bourbaki labelling, 
and 2 in Gap.) The Gap labelling is useful because the root system spanned by roots 1,2,...,k is of the same type.
<P>For example in type B<sub>3</sub>:
<TABLE cellpadding=5>
<TR><td><strong>Bourbaki:</strong></td><td><strong>Gap:</strong></td></tr>
<TR><td>1--2=>=3</td><td>3--2=>=1</td></tr></table>",
p,
'It is also possible to choose your own labelling of the Dynkin Diagram. The matrices will be modified accordingly.
Enter a permutation of 1,2,...,n where n is the rank. These give the numbering of the nodes with respect to the Bourbaki numbering.
For example in E<sub>8</sub> the Bourbaki numbering is
<pre>   
        2
        |
	|
1---3---4---5---6---7---8
</pre>
so if you enter 1 8 2 3 4 5 6 7 for <strong>Other</strong> you will get
<pre>
        8
	|
	|
1---2---3---4---5---6---7
</pre>';
    print p,a({-href=>'javascript:void(self.close())'},'Close');

    exit;
}

sub top{
    print header;
    print start_html(-BGCOLOR=>'white',-title=>"Spherical Explorer");
}

sub display{
    my $downloadOnly = 1 if (param('Submit') eq 'Download');

#    print CGI::Dump();
    my $type=param('type');
    my $rank=param('rank');

    unless ($rank =~ /^[0-9]*$/){
	&main("The rank must be a positive integer");
    }
    unless ($rank){
	&main("You must enter a rank");
    }
    if (($type eq 'E') and ( ($rank <6) or ($rank > 8))){
	&main("Very funny. For type E the rank must be 6,7, or 8.");
    }
    if (($type eq 'F') and($rank != 4)){
	&main("Very funny. For type F the rank must be 4.");
    }
    if (($type eq 'G') and($rank != 2)){
	&main("Very funny. For type G the rank must be 2.");
    }

    top() unless ($downloadOnly);
    print header(-type=>"application/octet-stream",-attachment=>"RootSystem$type$rank") if ($downloadOnly);

    my $coordinates=param('coordinates');
    my @coordinates;
    if ($coordinates eq 'Other'){
	$coordinates=param('otherCoordinates');
	$coordinates =~ s/[\s,]+/,/g;
	$coordinates =~ s/[^0-9,]*//g;
	@coordinates=split ',', $coordinates;
	unless (scalar(@coordinates) == $rank){
	    warn "rank$rank coo:", join ',', @coordinates, "scara:", scalar(@coordinates);
	    main("The number of coordinates does not match the rank");
	}
	$coordinates=\@coordinates;
    }elsif ($coordinates eq 'Bourbaki'){
	$coordinates='';
    }

    print h3("Root Data for type $type<sub>$rank</sub>") unless ($downloadOnly);
    print 'Back to ', a({-href=>'rootSystem.cgi'},'Root Systems'),p,'<pre>' unless ($downloadOnly);
    showAll($type,$rank,$coordinates);
    print '</pre>',p;
    unless ($downloadOnly){
	if (-e "$modelsDir/$type$rank/characterTable"){
	    print a({-href=>"$modelsDirUrl/$type$rank/characterTable"},"Character Table"), ' of Weyl group',p;
	}
    }
    print a({-href=>'javascript:void(self.close())'},'Close') unless ($downloadOnly);


}
