#!/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);

my $OutFile = "SPG.txt";
open(OUT, ">$OutFile") or die "$!: Can not write to [$OutFile].\n";
for(my $i = 1 ; $i <= 230 ; $i++) {
	my ($iSPG, $SPGName, $uaxis, $pInf) = &ReadPositionInfo($i);
	
	print OUT "$i: $SPGName (unique axis: $uaxis)\n";
	for(my $i = 1 ; $i < @$pInf ; $i++) {
		print OUT "  $i: $pInf->[$i]{WyckoffPosition}: $pInf->[$i]{Representative}\n";
	}
}
close(OUT);
exit;

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/[\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);
	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
