#!/usr/local/bin/perl -w
# Query coordinates, texture, VRML and interior
use DBI;

use CGI;


$database = '***';
$hostname = '***';
$user = '***';
$password = '***';


$query = new CGI;
print $query->header;
$TITLE="Query Example";

$path_info = $query->path_info;

# If no path information is provided, then we create 
# a side-by-side frame set
if (!$path_info) {
    &print_frameset;
    exit 0;
}


#Start HTML page
&print_html_header;
&print_query if $path_info=~/query/;
&print_response if $path_info=~/response/;



&print_end;
exit;

#----------------------------------------
# Subroutines
#-----------------------------------------

sub print_html_header {
    print $query->start_html($TITLE);
}

sub print_end {
       print $query->end_html;
}

sub print_frameset {
    $script_name = $query->script_name;
    print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset rows="50,50">
<frame src="$script_name/query" name="query">
<frame src="$script_name/response" name="response">
</frameset>
EOF
    ;
    exit 0;
}


sub print_query {
    $script_name = $query->script_name;
    print "<H1>Query</H1>\n";
    print $query->startform(-action=>"$script_name/response",-TARGET=>"response");

    print "<P>What would like to see? ",
    $query->popup_menu(-name=>'keyword',
		       -values=>['coordinates','texture','VRML world', 'interior']),
    "<P>";
    print $query->submit;
    print $query->endform;
 print qq{<P><A href="http://barley.itc.nl/examples.htm" TARGET= "vrml" >back</A>};

}

sub print_response {
    print "<H1>Result</H1>\n";
    unless ($query->param) {
	print "<b>No query submitted yet.</b>";
	return;
    }
       print "<P>Your preferences are: <EM>",$query->param('keyword'),"</EM> <BR>";

if (($query->param('keyword')) eq 'coordinates') {

#Query database--1------------------------------------------------

$dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password);

$sth = $dbh->prepare("select fid,nid,xc,yc,zc from comobg,surfg,face,node where cidg=1 and type='s' and oid=sidg and fids=fid and nidf=nid");
$sth->execute;

$num=0;

print "<BR>";
while ( @field = $sth->fetchrow) { 
$all[$num]=[@field];
  
  #print "$num:   $field[3] $field[4] <BR> ";

  $num++;
}

print "<BR>";
$sth->finish;

#---printing coordinates------------------------
$k=0;
while ($k<$num) {
  $j=0;
  while ($j<5) { 
    print "$all[$k]->[$j++]  ";
   
  }
  $k++;
  print "<BR>";
}

#--------END-------------------------------------

$dbh->disconnect;

#Query database END--1-------------------------------------------
}

elsif (($query->param('keyword')) eq 'texture'){

#Query database-2-------------------------------------------------

$dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password);

$sth = $dbh->prepare("select tname from surfa,texta where surfa.sida=18 and tids=tid");
$sth->execute;

$i=0;

print "<BR>";
while ($file_name = $sth->fetchrow) { 

  print qq {<P><hr><A HREF="http://barley.itc.nl/VRML/$file_name" TARGET="responce">See the image</A>};  
  
  $i++;
}

print "<BR>";
$sth->finish;

print "$all[1] <BR>";
#

$dbh->disconnect;

#Query database END-2--------------------------------------------

}
elsif (($query->param('keyword')) eq 'VRML world') {

print qq {<P><hr><A HREF="http://barley.itc.nl/cgi-bin/tvrml.cgi" TARGET="responce">Get the file</A>};  

}

else {

print qq{<embed src="http://barley.itc.nl/panorama.pan"
align="baseline" border="0" width="180" height="140" pan="15"
type="application/x-pan">};

}
; 

}


