#!/opt/SPECTRUM/bin/perl -I/opt/SPECTRUM/lib/perl
###############################################################################
#
#  CA Incorporated
#  273 Corporate Drive
#  Portsmouth, NH 03801
#  Copyright (c) 2010 CA, Inc.
#  All rights reserved.
#
#  IN NO EVENT SHALL CA INCORPORATED BE LIABLE FOR
#  ANY INCIDENTAL, INDIRECT, SPECIAL, OR CONSEQUENTIAL DAMAGES
#  WHATSOEVER (INCLUDING BUT NOT LIMITED TO LOST PROFITS) ARISING OUT
#  OF OR RELATED TO THIS SOFTWARE, EVEN IF CA INCORPORATED HAS BEEN
#  ADVISED OF, KNOWN, OR SHOULD HAVE KNOWN, THE POSSIBILITY OF SUCH
#  DAMAGES.
#
###############################################################################

###############################################################################
#
#  File: /sablime/sablime5_2/sdb/Ssprd/prod/s.startSS.pl.tpl
#
#  Version: 1.1.1.7 - 11/13/06 04:11:00
#
###############################################################################

use Getopt::Long;

$result = GetOptions("secondary|ss", "h|help");

die "Usage $0 [--secondary]\n" if ( $result == 0 || defined( $opt_h ) );

# obtain SPECROOT from /opt/SPECTRUM/spectrum80.env
$spec_env = "/opt/SPECTRUM/spectrum80.env";

open(INFILE, "<$spec_env") || die "Unable to open $spec_env for reading\n";
while (defined($inline = <INFILE>)) {
    if( $inline =~ /^SPECROOT/ )
    {
        $SPECROOT=$inline;
        chomp( $SPECROOT );
        $SPECROOT =~ s/^[^=]+=\s*//;
        break;
    }
}
close(INFILE);

if( $< == 0 )
{
  $filename = "$SPECROOT/SS/ssMtypeAttrs.db";
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks)
    = stat($filename);
  if( $< != $uid )
  {
    die "$0 should not be run as root\n";
  }
}

$launchinstdbapp = "$SPECROOT/bin/launchinstdbapp";
$cmdC = "$SPECROOT/bin/cmdC";

$VNMOUT = "$SPECROOT/SS/VNM.OUT";

# first verify that SS is not currently running
$ss_pid = isRunning( "SS" );
if( $ss_pid == 0 )
{
    $vnmdb = "$SPECROOT/SS/.VNMDB.LOCK";
    if( -e "$vnmdb" )
    {
        print "Removing SpectroSERVER lock file\n";
        unlink( $vnmdb ) || die "Error removing SpectroSERVER lock file\n";
    }
    !system("$launchinstdbapp localhost SS n $VNMOUT") || die "Error starting SS\n";

    $lines_read = 0;

    $last_line = "";

    $done = 0;
    $| = 1;
    while( !$done )
    {
        sleep( 1 );
        open(INFILE, "<$VNMOUT") || die "Unable to open $VNMOUT for reading\n";
        @inlines = <INFILE>;

        # VNM.OUT contains lines with carriage returns but no newline
        # so if the line changes, reprint it
        if( $lines_read != 0 && $last_line ne $inlines[$lines_read-1] )
        {
          print $inlines[$lines_read-1];
          $last_line = $inlines[$lines_read-1];
        }

        for( ; $lines_read <= $#inlines; $lines_read++ )
        {
            $inline = $inlines[$lines_read];
            print $inline;
            if( $inline =~ /is now ready on port/ )
            {
                $done = 1;
            }
            elsif( $inline =~ /Landscape not initialized/ )
            {
                $done = 1;
            }
            $last_line = $inlines[$lines_read];
        }
        close(INFILE);
    }
    $ss_pid = isRunning( "SS" );
}
else
{
    print "SpectroSERVER is already running with PID $ss_pid";
}

$DDMOUT = "$SPECROOT/SS/DDM/ARCHMGR.OUT";

# if the SS is running and we aren't a secondary, start the Archive Manager
if( $ss_pid && !defined($opt_secondary) )
{
    $armgr_pid = isRunning( "ARCHMGR" );
    if( $armgr_pid == 0 )
    {
        $ddmdb = "$SPECROOT/SS/DDM/.DDMDB.LOCK";
        if( -e "$ddmdb" )
        {
            print "Removing Archive Manager lock file\n";
            unlink( $ddmdb ) || die "Error removing Archive Manager lock file\n";
        }

        print "Starting Archive Manager\n";
        !system("$launchinstdbapp localhost ARCHMGR n $DDMOUT") || die "Error starting Archive Manager\n";
    }
    else
    {
        print "Archive Manager is already runing with PID $armgr_pid";
    }
}

sub isRunning()
{
    my( $TicketName ) = @_;
    my $is_running = 0;

    open(CMDC,"$cmdC localhost 8 $TicketName |") || die "Error running cmdC\n";
    while (defined($inline = <CMDC>)) {
        if( $inline =~ /^ticket process_pid/ )
        {
            $inline =~ s/^[^=]+=\s*//;
            if( $inline != 0 )
            {
                $is_running = $inline;
                break;
            }
        }
    }
    close(CMDC);

    return $is_running;
}