eval 'exec /usr/local/bin/perl -S -w $0 ${1+"$@"}'  # -*- Mode: perl -*-
        if 0; 
#==============================================================
#
#  Abs:   Script for starting a soft ioc
#
#  Name:  siocStart                  
#         
#  Usage: siocStart <executable> <script> <iocname> <stayup>
#         where executable = executable name (unique for this ioc)
#         where script     = script name
#         where iocname    = soft ioc name
#         where stayup     = automatic restart flag (0, 1)
#
#  Auth:  24-Feb-2004, S. Allison
#--------------------------------------------------------------
#  Mods: (Latest to oldest)
#
#        01-APR-2006, S. Allison (saa):
#        Modified for linux
#        27-APR-2004, S. Allison (saa):
#        IOC now writes core dump file to NFS data area.
#        20-APR-2004, Debbie Rogind (drogind):
#        added CD_IOC case for $path
#        06-APR-2004, Debbie Rogind (drogind):
#        added /usr/local/bin/ path to perl
#==============================================================

# check command arguments
usage("Too few args") if $#ARGV < 3;

#IOC_INFO or IOC_DATA or CD_DATA_IOC is path of bootparams file.
#It is also used in determining the location of the screenlog file if
#"deflog" is set to "on" in the screenrc file.  It is also the
#directory that will contain any core file dumped by the IOC.
if ( defined $ENV{"IOC_INFO"} ) {
    $iocinfo = $ENV{"IOC_INFO"};
} elsif ( defined $ENV{"IOC_DATA"} ) {
    $iocinfo = $ENV{"IOC_DATA"};
} elsif ( defined $ENV{"CD_DATA_IOC"} ) {
    $iocinfo = $ENV{"CD_DATA_IOC"};
} else {
    $iocinfo = ".";
}

my $command  = $ARGV[0];
my $script;
my $iocname  = $ARGV[2];
my $stayup   = $ARGV[3];
my $status;
my $ioclogdir;
my $path;
my $restarts = 0;

$ioclogdir="$iocinfo/$iocname";
if (!(-d $ioclogdir)) {
    $ioclogdir = "$iocinfo";
}

if ( defined $ENV{"IOC"} ) {
    $iocdir = $ENV{"IOC"};
    $path   = "$iocdir/$iocname";
} elsif ( defined $ENV{"CD_IOC"} ) {
    $iocdir = $ENV{"CD_IOC"};
    $path   = "$iocdir/$iocname";
} else {
    $path = ".";
}
$script = "$path/$ARGV[1]";

while() {
#   print "pgrep -f \^$command\n";
    $status = system("pgrep -f \^$command");
    if ($status == 0) {
	die ("IOC $iocname already started\n");
    }
    if ($stayup) {
        printf("IOC $iocname will automatically restart.  To prevent this, \"kill %s\"\n", $$);
    }
    print "Type  ^A d  to detach from iocConsole session. \n";
    print "Type  ^A ?  for screen help. \n";
	$status = system("cd $ioclogdir;umask 000;$command $script");
    if ($status == 0) {
	$restarts = 0;
    } else {
	print "IOC $iocname stopped - status = $status\n";
	$restarts++;
    }
    if ($restarts > 100) {
	die ("IOC $iocname terminated - maximum restart count reached\n");
    } elsif($stayup) {
	print "Wait 5 seconds and restart\n";
	sleep 5;
    } else {
	die ("IOC $iocname terminated - no restart\n");
    }
}

sub usage {
	my ($txt) = @_;
	print "Error: $txt\n" if $txt;
	print "\n";
	print "Usage:\n";
	print "    siocStart  <executable> <script> <iocname> <stayup>\n"; 
	print "where\n";
	print "    <executable>  Executable name\n";
        print "                  - must be unique for this ioc\n";
        print "    <script >     Startup script name\n";
        print "    <iocname>     Name of ioc\n";
        print "    <stayup>      Automatic restart (0=no, 1=yes)\n";
	print "\n";
	exit 2;
}
