#!/bin/sh -

# This script will send a file to netscape with database IDs linked to SRS.
# To customize this script edit the $DATABASES and $SRS_SERVER variable in the
# perl code below and the NETSCAPE variable at the top of the script.

# To enable this in Artemis, sanger_options must be set to true in the options 
# file.

NETSCAPE=/usr/local/lib/communicator/real-netscape

if [ -f "$DIANA_ENVIRONMENT_FILE" ]
then
   . $DIANA_ENVIRONMENT_FILE
fi

if [ $# = 0 ]
then
    echo no argument given 1>&2
    exit 1
fi

if [ -f ./$1 ]
then
    # the file is in the current directory - we need the full path so netscape
    # can find the file
    new_file=`pwd`/$1.html
else
    new_file=$1.html
fi

cat <<EOF > $new_file
<HTML>
 <HEAD>
  <TITLE>
  Results for $1
 </TITLE>
 </HEAD>
 <BODY>
<PRE>
EOF

perl -pne '
BEGIN {
  # change this variable to list the databases to search for the IDs
  $DATABASES = "embl emblnew ensembl swissprot swissnew pir sptrembl remtrembl tremblnew";

  # change this to point to the wgetz script of your SRS server
  $SRS_SERVER = "www.sanger.ac.uk/srs6bin/cgi-bin/wgetz?-e+";


  $DATABASES =~ s/ /%20/g;

  $BLAST_START_LINE = "Sequences producing High-scoring Segment Pairs";
  $FASTA_START_LINE = "The best scores are";

  # the list of IDs we have seen so far
  @ids = ();

  # the list of IDs we have made anchors for so far
  @anchored_ids = ();
}

# ignore header lines
if (1..m/$BLAST_START_LINE|$FASTA_START_LINE/) {
  next;
}

if (@ids && /^\s*$/) {
  $summary_finished = 1;
}

if (/^(?:>?(?:[A-Z]+:)?)(\w+) /) {
  $id = $1;
  if ($summary_finished) {
    if ((grep {$_ eq $id} @ids) && (!grep {$_ eq $id} @anchored_ids)) {
      # not anchored yet so make it an anchor
      if (s/\b$id\b/"<a name=\"$id\">" . (hyperlink_id($id)) . "<\/a>"/e) {
        push @anchored_ids, $id;
      }
    }
  } else {
    if (!grep {$_ eq $id} @ids) {
      push @ids, $id;
    }

    s/$id/hyperlink_to_anchor($id)/ei;

    if (!s/ $id/" " . hyperlink_id($id)/ei) {
      # if the id occurs once in the line put a link at end of line
      s/$/"  SRS:" . hyperlink_id($id)/e;
    }
  }
}

sub hyperlink_to_anchor
{
  $id = shift;
  qq(<a href="#$id">$id</a>);
}

sub hyperlink_id
{
  $id = shift;

  $r = qq(<a href="http://$SRS_SERVER\[\{$DATABASES\}-ID:$id*]">$id</a>);

#   print STDERR "linking: $id to $r\n";
  $r
}
' $1 >> $new_file

cat <<EOF >> $new_file;
</PRE>
  </BODY>
</HTML>
EOF

if $NETSCAPE -remote "openURL($new_file)"
then
    exit 0
else
    echo starting new netscape 2>&1
    # netscape isn't running - so start it
    ($NETSCAPE &)&

    # now send the URL.  we do things this way so that the script doesn't exit
    # until netscape has successfully shown the URL

    sleep 1

    # don't exit the script until the file is successfully displayed or until
    # 40 seconds is up
    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    do
        if $NETSCAPE -remote "openURL($new_file)" 2> /dev/null
        then
            exit 0
        else
            sleep 2
        fi
    done

    exit 1
fi
