#!/usr/local/bin/perl
##############################################################################
# Wrapper for ms-searchcontrol.exe                                           #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2003 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
# This program may be used and modified within the licensee's organisation   #
# provided that this copyright notice remains intact. Distribution of this   #
# program or parts thereof outside the licensee's organisation without the   #
# prior written consent of Matrix Science Limited is expressly forbidden.    #
##############################################################################
#    $Archive:: /www/cgi/client.pl                                         $ #
#     $Author:: Davidc                                                     $ #
#       $Date:: 31/01/05 12:46                                             $ #
#   $Revision:: 2                                                          $ #
# $NoKeywords::                                                            $ #
##############################################################################
# use strict;                                                                #
##############################################################################
#
# All actions take an optional sessionID=s argument for Mascot security
#
# To obtain a task ID for search submission requires one argument:
#  create_task_id
#
# A search status query requires two arguments:
#  status task_id=n
#
# To kill a search job requires two arguments:
#  kill_job task_id=n
#
# To obtain a result file name requires two arguments:
#  result_file_name task_id=n
#
# To obtain summary results for a completed search requires two arguments:
#  results task_id=n
#
# To obtain xml (Distiller) results for a completed search requires three arguments:
#  xmlresults task_id=n reporttop=[FILE|AUTO|num_hits]
#
# To obtain the version number of ms-searchcontrol.exe requires one argument:
#  version
#
# To set the priority of a search requires three arguments:
#  nice_job task_id=n nice_value=n
# The valid range for nice_value is -20 to 20
#

  $| = 1; # set autoflush on STDOUT

# set current directory to script location for FastTrack on NT
# chdir also handles a Windows drive letter change
  if ($ENV{'WINDIR'} && $0 =~ /(.*)[\\\/]/) {
    chdir($1);
  }

  use CGI;
  my $thisScript = new CGI;
  print $thisScript->header('text/plain');

  my($taskID, $sessionID, $nice_value);
  if (defined($thisScript->param('task_id'))) {
    $taskID = $thisScript->param('task_id');
  }
  if ($thisScript->param('sessionID')) {
    $sessionID = "--sessionID " . $thisScript->param('sessionID');
  }
  if (defined($thisScript->param('nice_value'))) {
    $nice_value = $thisScript->param('nice_value');
  }
  if (defined($thisScript->param('keywords'))) {
    $thisScript->param(-name => $thisScript->param('keywords'),
                       -value => 1);
  }
      
  if (defined($thisScript->param('create_task_id'))) {

  # supply the correct action string for the search
    if (-e "../config/lsf.dat") {
      print "actionstring=nph-lsf_helper.pl\n";
    } else {
      print "actionstring=nph-mascot.exe\n";
    }
  
  # Get a task ID
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --create_task_id $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    print @temp;

  } elsif (defined($thisScript->param('status'))) {
    
    if ($taskID < 1) {
      print "problem=Invalid task ID\n";
      exit 1;
    }

  # Call ms-searchcontrol.exe to get status info
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --status --taskID $taskID $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    print @temp;

  } elsif (defined($thisScript->param('kill_job'))) {
    
    if ($taskID < 1) {
      print "problem=Invalid task ID\n";
      exit 1;
    }

  # Call ms-searchcontrol.exe to kill a job
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --kill_job --taskID $taskID $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    print @temp;

  } elsif (defined($thisScript->param('result_file_name'))) {
    
    if ($taskID < 1) {
      print "problem=Invalid task ID\n";
      exit 1;
    }

  # Call ms-searchcontrol.exe to get result file name
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --result_file_name --taskID $taskID $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    print @temp;

  } elsif (defined($thisScript->param('results')) || defined($thisScript->param('xmlresults'))) {

    if ($taskID < 1) {
      print "problem=Invalid task ID - $taskID\n";
      exit 1;
    }

    # Same code for results and xmlresults except that the latter requires
    # reporttop and different command to be passed to ms-searchcontrol
    my ($cmnd, $reporttop);
    if (defined($thisScript->param('xmlresults'))) {
      $cmnd = "--xmlresults";
      if (!defined($thisScript->param('reporttop'))) {
        print "problem=Invalid reporttop\n";
        exit 1;
      } else {
        $reporttop = "--reporttop " . $thisScript->param('reporttop');
      }
    } else {
      $cmnd = "--results";
    }
    
  # Call ms-searchcontrol.exe to output search results to STDOUT
    if (system("../bin/ms-searchcontrol.exe $cmnd --taskID $taskID $sessionID $reporttop")) {
      print "problem=Error return from ms-searchcontrol.exe\n";
      exit 1;
    }
    
  # Remove the .flg file so that results don't get emailed.
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --result_file_name --taskID $taskID $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    my $fileIn = "";
    foreach (@temp) {
      if (/^filename=(.+)/i) {
        $fileIn = $1;
        last;
      }
    }
    if ($fileIn) {
      unlink $fileIn . '.flg';
    }

  # trigger export of results to the late worksbase
    if ($fileIn && (-e "ms-wxbs-results.exe")) {
    # for debug, try $comString = "net send localhost \"datfile=$fileIn\"";
      my $comString = "ms-wxbs-results.exe datfile=$fileIn";
    # assume that argument requires file delimiters to be backslashes
      if ($ENV{'WINDIR'}){
        $comString =~ s#/#\\#g;
      }
      system($comString);
    }

  } elsif (defined($thisScript->param('version'))) {

  # Get version information for ms-searchcontrol.exe
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --version $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    print @temp;

  } elsif (defined($thisScript->param('nice_job'))) {
    
    if ($taskID < 1) {
      print "problem=Invalid task ID\n";
      exit 1;
    }

  # Call ms-searchcontrol.exe to set search priority
    unless (open(SOCK, "../bin/ms-searchcontrol.exe --nice_job --taskID $taskID --nice $nice_value $sessionID |")) {
      print "problem=Could not open socket to ms-searchcontrol.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    print @temp;

  } else {

    print "problem=Invalid keyword argument supplied to client.pl\n";
    exit 1;

  }
  
  exit 0;

