#!/opt/perl5/bin/perl
#
# Test routine for the installation of the catalogue servers USNO-A2.0 and
# GSC-1.1 and the DSS-1 server in the Paranal configuration.
#
# Andreas Wicenec [ESO]: 19-Mar-2001
#
#
# First check existence of files and directories
#

# Setup
#
$HOST = `uname -n`;
$HOST = substr($HOST,0,length($HOST)-1);

$OS = `uname -s`;
$OS = substr($HOST,0,length($HOST)-1);

$OS_V = `uname -r`;
$OS_V = substr($OS_V,0,length($OS_V)-1);

print "\n\nGENERAL: **************************************************\n";
# General settings
#
print "Checking existence of directories and links...\n";
if (!-d "/catsrv"){
    die "/catsrv directory does not exist!!\n";}
else {
    print "/catsrv OK\n";
}
if (!-d "/home/web/archive"){
    die "/home/web/archive directory does not exist!!\n";}
else {
    print "/home/web/archive OK\n";
}
if (!-d "/home/web/archive/docs"){
    die "/home/web/archive/docs directory does not exist!!\n";}
else {
    print "/home/web/archive/docs OK\n";
}
if (!-d "/home/web/archive/docs/skycat"){
    die "/home/web/archive/docs/skycat directory does not exist!!\n";}
else {
    print "/home/web/archive/docs/skycat OK\n";
}
if (!-d "/home/web/archive/docs/skycat/servers"){
    die "/home/web/archive/docs/skycat/servers directory does not exist!!\n";}
else {
    print "/home/web/archive/docs/skycat/servers OK\n";
}
if (!-d "/home/web/archive/docs/images"){
    die "/home/web/archive/docs/images directory does not exist!!\n";}
else {
    print "/home/web/archive/docs/images OK\n";
}
if (!-d "/home/web/archive/docs/gsc"){
    die "/home/web/archive/docs/gsc directory does not exist!!\n";}
else {
    print "/home/web/archive/docs/gsc OK\n";
}
if (!-d "/home/web/archive/docs/dss"){
    die "/home/web/archive/docs/dss directory does not exist!!\n";}
else {
    print "/home/web/archive/docs/dss OK\n";
}
if (!-l "/home/web/archive/docs/dssdsk"){
    print "/home/web/archive/docs/dssdsk link does not exist!!\n";
    die "Should point to /catsrv/dss\n";
}
else {
    print "/home/web/archive/docs/dssdsk OK\n";
}


print "\n\nGSC: **************************************************\n";
# check GSC directories and files:
print "Checking GSC files and directories...\n";
@gsc = glob('/catsrv/catalogs/GSC/[NS]*');
#
if (!-R "/home/web/archive/docs/skycat/servers/gsc"){
    die "/home/web/archive/docs/skycat/servers/gsc is not executable!!\n";}
else {
    print "GSC perl script OK\n";
}
if (!-R "/catsrv/catalogs/cat-server/bin/gsc"){
    die "/catsrv/catalogs/cat-server/bin/gsc is not executable!!\n";}
else {
    print "GSC server OK\n";
}
if ($#gsc != 23){
    print "Some directories of the GSC catalogue installation are missing\n";}
else {
    print "GSC catalogue directories OK\n";
}
$gscf = `ls -1d /catsrv/catalogs/GSC/[NS]*/* | wc -l`;
if ($gscf != 9537){
    print "Some files of the GSC catalogue installation are missing\n";}
else {
    print "GSC catalogue files OK\n";
}
print "\nTrying to execute the GSC server script...\n";
$ENV{'QUERY_STRING'} = 'center=10:00:00+10:00:00&radius=0,20&mag=0,16&format=3&sort=mag&nout=200';
$ENV{'REQUEST_METHOD'} = 'GET';
@gsc = `cd /home/web/archive/docs/skycat/servers;./gsc`;
$title = @gsc[4];
if (substr($title,24,3) != 'GSC'){
    die "Something went wrong!!\n"}
else {
    print "GSC server returned:\n$title\n";
}

print "\n\nUSNO: **************************************************\n";
# check USNO directories and files:
print "Checking USNO-A2.0 files and directories...\n";
@usno = glob('/catsrv/catalogs/USNO-A2.0/*');
#
if (!-R "/home/web/archive/docs/skycat/servers/usnoa"){
    die "/home/web/archive/docs/skycat/servers/usnoa is not executable!!\n";}
else {
    print "USNO-A2.0 perl script OK\n";
}
if (!-l "/home/web/archive/docs/skycat/servers/usnoa_res"){
    die "/home/web/archive/docs/skycat/servers/usnoa_res does not exist!!\n";}
else {
    print "usnoa_res link OK\n";
}
if (!-l "/home/web/archive/docs/skycat/servers/usnoa_post_res"){
    die "/home/web/archive/docs/skycat/servers/usnoa_post_res does not exist!!\n";}
else {
    print "usnoa_post_res link OK\n";
}
if (!-R "/catsrv/catalogs/cat-server/bin/usnoa-server"){
    die "/catsrv/catalogs/cat-server/bin/usnoa-server is not executable!!\n";}
else {
    print "USNO-A2.0 server OK\n";
}
if ($#usno != 24){
    print "Some files of the USNO-A2.0 catalogue installation are missing\n";}
else {
    print "USNO-A2.0 catalogue files OK\n";
}
$ENV{'QUERY_STRING'} = 'center=10:00:00+10:00:00&radius=0,20&mag=0,16&format=3&sort=mag&nout=200';
$ENV{'REQUEST_METHOD'} = 'GET';
@usno = `cd /home/web/archive/docs/skycat/servers;./usnoa`;
$title = @usno[4];
if (substr($title,24,9) != 'USNO-A2.0'){
    die "Something went wrong!!\n"}
else {
    print "USNO-A2.0 server returned: \n$title\n";
}

print "\n\nDSS: **************************************************\n";
# check DSS directories and files
# checks only existence of directories, else it would take too long!
#
print "Checking DSS files and directories...\n";
@dss = glob('/catsrv/dss/*');
#
if (!-R "/home/web/archive/docs/dss/dss"){
    die "/home/web/archive/docs/dss/dss is not executable!!\n";}
else {
    print "DSS perl script OK\n";
}
if (!-R "/catsrv/dss/bin.hp/dss"){
    die "/catsrv/dss/bin.hp/dss is not executable!!\n";}
else {
    print "DSS server OK\n";
}
if ($#dss != 105){
    print "Some files of the DSS catalogue installation are missing\n";}
else {
    print "DSS catalogue files OK\n";
}
$ENV{'QUERY_STRING'} = 'center=10:00:00+10:00:00&radius=0,20&mag=0,16&format=3&sort=mag&nout=200';
$ENV{'REQUEST_METHOD'} = 'GET';
@dss = `cd /home/web/archive/docs/dss;./dss`;
$title = @dss[4];
if (substr($title,24,9) != 'DSS'){
    die "Something went wrong!!\n"}
else {
    print "DSS server returned: \n$title\n";
}
#
#
# Now perform the full test of web-server plus catalog servers using telnet
#
print "\n\nTelnet test: **************************************************\n";
print "GSC test\n";
open tn,"|telnet wu0dhs 8080 >/tmp/Tmp$$";
printf tn "GET /skycat/servers/gsc?ra=10:00:00&dec=+10:00:00&radmax=20&mag=0,16&format=3&sort=mag&nout=200\n";
close tn;
$tn = `cat /tmp/Tmp$$ | wc -l`;
if ($tn != 132){
    print `cat /tmp/Tmp$$`;
    die "GSC http request did not return the correct number of lines!\n";}
else {
    print "GSC http request OK\n";}
unlink "/tmp/Tmp$$";
#
#
print "USNOA test\n";
open tn,"|telnet wu0dhs 8080 >/tmp/Tmp$$";
printf tn "GET /skycat/servers/usnoa?ra=10:00:00&dec=+10:00:00&radmax=20&mag=0,16&format=3&sort=mag&nout=200\n";
close tn;
$tn = `cat /tmp/Tmp$$ | wc -l`;
if ($tn != 255){
    print `cat /tmp/Tmp$$`;
    die "USNO http request did not return the correct number of lines!\n";}
else {
    print "USNO http request OK\n";}
unlink "/tmp/Tmp$$";

#
#
# It seems that everything went fine
#
print "\n\nFinalize: **************************************************\n";
print "Installations seem to be OK. Try to run queries through web browser\n";
print "using the following URLs:\n";
print "http://$HOST:8080/skycat/servers/gsc\n";
print "http://$HOST:8080/skycat/servers/usnoa\n";
print "http://$HOST:8080/dss/dss\n";
print "**************************************************\n";

