#!/usr/local/bin/perl
##############################################################################
# List key Mascot parameters for Daemon                                      #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2001 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/get_params.pl                                     $ #
#     $Author:: Johnc                                                      $ #
#       $Date:: 28/01/05 9:54                                              $ #
#   $Revision:: 18                                                         $ #
# $NoKeywords::                                                            $ #
##############################################################################
# use strict;                                                                #
##############################################################################
  
  use CGI;

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

# set current directory to script location for FastTrack on NT
# chdir also handles a Windows drive letter change
  if ($ENV{'WINDIR'} && $0 =~ /(.*)[\\\/]/) {
    chdir($1);
  }
  
# if a single parameter is supplied, pass to ms-status.exe and return output
  if ($thisScript->param("Show")) {
    my $args = "Show=" . $thisScript->param("Show");
    if ($thisScript->param("sessionID")) {
      $args = $args . " sessionID=" . $thisScript->param("sessionID")
    }
    unless (open(SOCK, "../x-cgi/ms-status.exe " . $args . " |")) {
      print "problem=Could not open socket to ms-status.exe\n";
      exit 1;
    }
    my @temp = <SOCK>;
    close(SOCK);
    if ($temp[0] =~ /^Content-type:/i) {
      shift @temp;
      shift @temp;
    }
    print @temp;
    exit 0;
  }

  my (
    @dbName, 
    @dbType,
    @options,
    );
    
# retrieve information from mascot.dat
  open(TEMPFILE, "../config/mascot.dat") || die "cannot open mascot.dat";
  while (<TEMPFILE>) {
    if (/^Databases/i) {
      while (<TEMPFILE>) {
        last if (/^end/i);
        unless (/^#/){
          push @dbName, (split)[0];
          push @dbType, (split)[2];
        }
      }
    }
    if (/^Options/i) {
      while (<TEMPFILE>) {
        last if (/^end/i);
        if ( /^ReportNumberChoices\s+/i
        	|| /^IgnoreIonsScoreBelow\s+/i
        	|| /^MinPepLenInPepSummary\s+/i
        	|| /^Mudpit\s+/i
        	|| /^RequireBoldRed\s+/i
        	|| /^ShowSubSets\s+/i
        	|| /^SigThreshold\s+/i
        	|| /^SortUnassigned\s+/i ) {
          push @options, $_;
        }
      }
    }
  }
  close(TEMPFILE) || die "cannot close mascot.dat";
  
# options for DB
  print "\n\n[DB]\n";
  foreach (@dbName) {
    print "$_\n";
  }

# options for MODS
  print "\n[MODS]\n";
  open(TEMPFILE, "../config/mod_file") || die "cannot open mod_file";
  my @mods=<TEMPFILE>;
  close(TEMPFILE) || die "cannot close mod_file";
  my $i=0;    
  while ($mods[$i]){
    if ($mods[$i] =~ /^Title:(.+?)\n/i){
      my $saveMod = $1;
      $i++;
      unless ($mods[$i] =~ /^Hidden/i){
        print "$saveMod\n";
      }
    }
    $i++;
  }

# options for CLE
  print "\n[CLE]\n";
  open(TEMPFILE, "../config/enzymes") || die "cannot open enzymes file";
  my @enzymes=<TEMPFILE>;
  close(TEMPFILE) || die "cannot close enzymes file";
  $i=0;    
  while ($enzymes[$i]){
    if ($enzymes[$i] =~ /^Title:(.+?)\n/i){
      print "$1\n";
    }
    $i++;
  }

# options for TAXONOMY
  print "\n[TAXONOMY]\n";
  if (open(TEMPFILE, "../config/taxonomy")) {
    my @taxonomy=<TEMPFILE>;
    close(TEMPFILE) || die "cannot close taxonomy file";
    $i=0;    
    while ($taxonomy[$i]){
      if ($taxonomy[$i] =~ /^Title:(.+?)\n/i){
        my $saveTax = $1;
        $i++;
        unless ($taxonomy[$i] =~ /^Hidden/i){
          print "$saveTax\n";
        }
      }
      $i++;
    }
  } else {
    print "All entries";
  }

# selected options from Options
  print "\n\n[OPTIONS]\n";
  print @options;

# options for INSTRUMENT
  print "\n[INSTRUMENT]\n";
  open(TEMPFILE, "../config/fragmentation_rules") || die "cannot open fragmentation_rules file";
  my @instrument=<TEMPFILE>;
  close(TEMPFILE) || die "cannot close fragmentation_rules file";
  $i=0;    
  while ($instrument[$i]){
    if ($instrument[$i] =~ /^Title:(.+?)\n/i){
      print "$1\n";
    }
    $i++;
  }

# Enhanced DB list, including type (AA or NA)
  print "\n[DB_type]\n";
  for (my $i = 0; $i < scalar(@dbName); $i++) {
    print "$dbName[$i] $dbType[$i]\n";
  }

# hidden MODS
  print "\n[HIDDEN_MODS]\n";
  $i=0;    
  while ($mods[$i]){
    if ($mods[$i] =~ /^Title:(.+?)\n/i){
      my $saveMod = $1;
      $i++;
      if ($mods[$i] =~ /^Hidden/i){
        print "$saveMod\n";
      }
    }
    $i++;
  }
  
  print "\n\n";

  exit 0;
