#!/usr/bin/perl
# Script to emulate a browser for posting to a 
#   CGI program with method="POST".

# It's a good habit to always use the strict module.
use strict;

# Modules with routines for making the browser.
use LWP::UserAgent;
use HTTP::Request::Common;

# Specify the URL of the page to post to.
#my $URLtoPostTo = "http://flowto.info/cgi-bin/Dump.cgi";
#my $URLtoPostTo = "http://www.cryst.ehu.es/rep/sam.html";
#my $URLtoPostTo = "http://www.cryst.ehu.es/cgi-bin/cryst/programs/nph-sam";
my $URLtoPostTo = "http://www.cryst.ehu.es/cgi-bin/cryst/programs//nph-sam";

# If you want to specify a browser name, 
#   do so between the quotation marks. 
#   Otherwise, nothing between the quotes.
my $BrowserName = "Perl script";

# Create the browser that will post the information.
my $Browser = new LWP::UserAgent;
if($BrowserName) { $Browser->agent($BrowserName); }

#my $pInf = &ReadPositionInfor(10);

for(my $iSPG = 1 ; $iSPG <= 230 ; $iSPG++) {
	my ($iSPG2, $SPGName, $uaxis, $pInf) = &ReadPositionInfo($iSPG);
	$SPGName =~ s/\<\/?sub\>//sig;
	$SPGName =~ s/\<\/?sup\>//sig;
#	$SPGName =~ s/\-//g;
	$SPGName =~ s/\//_/g;
	$SPGName =~ s/\s//g;
	for(my $iWP = 1 ; $iWP < @$pInf ; $iWP++) {
		my $WPName = $pInf->[$iWP]{WyckoffPosition};
		my $Repr   = $pInf->[$iWP]{Representative};
		$WPName  =~ s/\s//g;
		my $OutFile = sprintf("%03d-$SPGName-%03d-$WPName.html",$iSPG, $iWP);
		if(-e $OutFile) {
			print "[$OutFile] exists. Skip.\n\n";
			next;
		}
		else {
			print "Save to [$OutFile].\n";
			&SaveIRRamanHTML($iSPG, $iWP, $OutFile);
			print "\n";
		}
	}
}

exit;

sub SaveIRRamanHTML
{
	my ($iSPG, $iWP, $OutFile) = @_;

	my ($iSPG2, $SPGName, $uaxis, $pInf) = &ReadPositionInfo($iSPG);
	
	print "$iSPG2: $SPGName (unique axis: $uaxis)\n";
	for(my $i = 1 ; $i < @$pInf ; $i++) {
		print "  $i: $pInf->[$i]{WyckoffPosition}: $pInf->[$i]{Representative}\n";
	}

	my $WPName = $pInf->[$iWP]{WyckoffPosition};
	my $Repr   = $pInf->[$iWP]{Representative};

	my %Fields = (
		"sg"    => $iSPG,
		"sam"   => "sam",
		"gua"   => "",
		"gor"   => "",
		"grha"  => "",
		$WPName => 'on',
#		$WPName => 'checked',
#		$WPName => $WPName,
#		$WPName => '1',
#		$WPName => 'check',
#		$WPName => 'ON',
		'submit' => "Continue",
	);

# Post the information to the CGI program.
	my $Page = $Browser->request(POST $URLtoPostTo,\%Fields);

	my $s;
# Print the returned page (or an error message).
#	print "Content-type: text/html\n\n";
	if ($Page->is_success) {
		$s = $Page->content;
#		print $s;
	}
	else { 
		$s = $Page->message;
#		print $Page->message; 
	}

	$OutFile =~ s/\//_/g;
	open(OUT, ">$OutFile") or die "$!: Cannot write to [$OutFile].\n";
	print OUT $s;
	close(OUT);
}

sub ReadPositionInfo
{
	my ($i) = @_;

	my $InFile = "$i.html";
	open(IN, "$InFile") or die "$!: Cannot read [$InFile].\n";
	my $s = '';
	while(<IN>) {
		last if(!defined $_);
		
		$s .= $_;
	}
	close(IN);

	$s =~ s/\<\/?i\>//sig;
#	$s =~ s/\<\/?sub\>//sig;
#	$s =~ s/\<\/?sup\>//sig;
	$s =~ s/[\r\n]//sig;
	$s =~ s/^.*?your structure//si;
	$s =~ s/\<\/body\>.*$//si;
#	print "s[$s]\n";
	
#<h4 align="center">Choose the Wyckoff Positions of the atoms in your structure for the space group <i>P</i>2<i>/m</i> (No. 10) [unique axis b]</h4><form method="post" action="/cgi-bin/cryst/programs//nph-sam">
#the space group P2/m (No. 10) [unique axis b]
#	my ($SPGName) = ($s =~ /^(.*)$/si);
#	my ($SPGName) = ($s =~ /group(.*)$/si);
	my ($SPGName) = ($s =~ /group\s+(.*?)\s+\(/s);
#	$SPGName =~ s/\-//g;
#	$SPGName =~ s/\s//g;
	my ($iSPG)    = ($s =~ /\(No\.\s*(.*?)\)/s);
	my ($uaxis)   = ($s =~ /unique\s+axis\s+(.*)\]/s);
	print "SPGName    : $SPGName\n";
	print "iSPG       : $iSPG\n";
	print "Unique axis: $uaxis\n";

	my ($s2) = ($s =~ /\<table(.*)\<\/table/i);
#	print "s2: [$s2]\n\n";

	my @Inf;
	my @trs = split(/\<tr\>/,$s2);
	for(my $i = 1 ; $i < @trs ; $i++) {
#		print "$i: [$trs[$i]]\n";
		my @tds = split(/\<td[^\>]*\>/,$trs[$i]);
		for(my $j = 2 ; $j < @tds ; $j++) {
			$tds[$j] =~ s/\<[^\>]+\>//ig;
		}
		next if(!defined $tds[3]);

#		print "$i: $tds[1]\n";
#		print "     $tds[2]\n";
#		print "     $tds[3]\n";
		$Inf[$i-1] = {
			WyckoffPosition => $tds[2],
			Representative  => $tds[3],
			};
	}

	return ($iSPG, $SPGName, $uaxis, \@Inf);
}

sub SaveWyckoffPositionHTML
{
	my ($i) = @_;
	
# Specify the information to post, the form field name on 
#   the left of the => symbol and the value on the right.
	my %Fields = (
	   "sg"  => $i,
	   "sam" => "sam",
	);
# As seen above, "@" must be escaped when quoted.

# Post the information to the CGI program.
	my $Page = $Browser->request(POST $URLtoPostTo,\%Fields);

	my $s;
# Print the returned page (or an error message).
#	print "Content-type: text/html\n\n";
	if ($Page->is_success) {
		$s = $Page->content;
#		print $s;
	}
	else { 
		$s = $Page->message;
#		print $Page->message; 
	}
	
	my $OutFile = "$i.html";
	open(OUT, ">$OutFile") or die "$!: Cannot write to [$OutFile].\n";
	print OUT $s;
	close(OUT);
}

# end of script
