#!/usr/bin/perl 

# read a rdb table and make a basic cgi interface to display the table

$dir_pub 	= "/proj/champwww/ChaMPlane/data/xray";
$rdb	 	= "$dir_pub/xfield.rdb";
$http_pxp 	= "http://hea-www.harvard.edu/ChaMPlane/data/xray";
$guide	 	= "http://hea-www.harvard.edu/ChaMPlane/data/scripts/guide.html";

$dir_bin   	= "/proj/axaf/bin";
$transpose 	= "$dir_bin/rdbtranspose --icol=ObsID --ocol=ObsID";
$column	   	= "$dir_bin/column";
$sorttbl   	= "$dir_bin/sorttbl";
$rdbeval   	= "$dir_bin/rdbeval";
$ptbl		= "$dir_bin/ptbl -l1000 -p0";
$tbl2lst	= "$dir_bin/tbl2lst -l1000";
$rdb2html  	= "$dir_bin/rdb2html --nocellalign"
			." --cellspacing=0 --cellpadding=2 --border=0 "
	 		." --tblcolor=#c0c0c0 "
			." --nomargin";

$dosort   	= "no";

#click <a href="xpipe_status_internal.html">here</a> for more information <p>

&parse_form;

@fields=keys %Form;
@scols=();

for($i=1;$i<=$#fields;$i++) {
        #$Form{$field} = &clean_html($Form{$field});
	$field = "c$i";
#	print "$i \n";
	if ($Form{$field}) {
		if ($Form{$field} ne "None") {
			$column .= " \"$Form{$field}\"";
			push(@scols, $Form{$field});
		}
	}
}

if ($Form{"rest"}) {
	$column .= " -Z";
}

for($i=1;$i<=$#fields;$i++) {
        #$Form{$field} = &clean_html($Form{$field});
	$field = "r$i";
	if ($Form{$field}) { $sorttbl .= " -r" ;}
	$field = "s$i";
	if ($Form{$field}) {
		$sorttbl .= " \"$Form{$field}\"" 
			unless $Form{$field} eq "None" ; 
		$dosort = "yes";
	}
}
#print "$sorttbl\n";
$target="";
$target_="";
if ($Form{"target"}) {
	$target=$Form{"target"};
	$target_="&target=contents";
};
if ($target eq "") {$target="type";};
if ($target_ eq "") {$target_="&target=contents";};


# 2 space lines are necessary
print <<EOFINPUT;
Content-type: text/html


<HTML>
<HEAD>
</HEAD>
<BODY>
<b>Browse by ObsID</b><br>
Select obsid below.<br>
Click <a href="rdb2webin.pl?rdb=$rdb&ncolumn=9&cgi=http://hea-www.harvard.edu/ChaMP-bin/x-ray4pub/list.pl">here</a> 
for more information on X-ray fields (gl, gb, sorting, etc) and <a
href="$guide" target="list">here</a> for browsing guide.
Click <a href="http://hea-www.harvard.edu/ChaMPlane/data_archive.html" target="_parent">here</a>
to go back to the main page of data archive.

EOFINPUT

$cmd = "$column < $rdb ";
$cmd .= "| $sorttbl " if $dosort eq "yes";
$cmd .=	"| $rdbeval '\$obsid = sprintf(\"link%s\",\$obsid)' ";
#$cmd .= "| $transpose " if $Form{"transpose"} eq "yes";
if ($Form{"format"} eq "html") {
	$cmd .=	"| $rdb2html ";
} elsif ( $Form{"format"} eq "txt") {
	$cmd .=	"| $ptbl " ;
} elsif ( $Form{"format"} eq "list") {
	$cmd .=	"| $tbl2lst " ;
	
}

$output = `$cmd`;
$output =~ s/link(.\d+)/<a target="$target" href="http:\/\/hea-www.harvard.edu\/ChaMP-bin\/x-ray4pub\/obsid.pl?obsid=$1$target_">$1<\/a>/g;

$output = "<PRE>$output </PRE>" 
	unless $Form{"format"} eq "html";

print $output;

print <<EOFINPUT;
</BODY>
</HTML>
EOFINPUT

sub parse_form {

   	# Get the input
	if ($ENV{'REQUEST_METHOD'} eq 'GET') {
	        # Split the name-value pairs
	        @pairs = split(/&/, $ENV{'QUERY_STRING'});
#		print "here\n";
	} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        	@pairs = split(/&/, $buffer);
    	}
   	foreach $pair (@pairs) {
#		print "$pair\n";
      		($name, $value) = split(/=/, $pair);

      		$value =~ tr/+/ /;
      		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      		$Form{$name} = $value;
   	}
#	print "@pairs \n";
}

sub get_column_name{			# check current line, $_
	while(<RDB>){
		next if /^\s*$/ ;	# skip blank lines
    		next if( /^\s*\#/ );	# comment line
    		$lln++ ;  		# logical line nr
		last if( $lln > 1 );
#		while( s/\s*(\S+)// ) {
		while( s/\t*([^\t]+)// ) {
			 $x = $1 ;
    			push( @column, $x ) ;
			#print $x . "\n";
    		}
	}

}

