eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'  # -*- Mode: perl -*-
    if 0;
#==============================================================
#
#  Abs:   2nd  level iocConsole script for connecting to iocs
#
#  Name:  iocScreen                  
#
#  Rem:   iocConsole program provides a common mechanism for connecting 
#  to VME IOC console serial lines as well as soft IOC virtual terminal 
#  sessions. It takes advantage of gnu screen utility to provide a multiuser
#  session running telnet to manage an ioc console session.
#  - telnet session to ioc continues to run even when no users are attached
#    to the screen session
#  - ioc console output is captured and can be viewed by subsequent sessions
#  - maintains scroolback history buffer
#  - allows multiple simultaneous connections - anything typed in one
#    session appears in all of them - more than one person can be viewing
#         
#
#  Usage: iocScreen <terminal server or executable> <port> <iocname>
#         where terminal server = terminal server name assigned to ioc
#               or executable   = soft ioc executable
#         where port    = port number assigned to ioc
#         where iocname = node name or micro name
#         where stayup  = automatic restart flag (0, 1)
#
#  Facility: SLAC/ESD
#
#  Side:  
#  
#  
#  Auth:  20-NOV-2003, DRogind    (DROGIND):
#  Rev:   dd-mmm-yyyy, Whoever    (USERNAME):
#
#--------------------------------------------------------------
# Mods: (Latest to oldest)
#
#        01-APR-2006, S. Allison (saa):
#            Modified for linux
#        28-SEP-2004, Debbie Rogind (DROGIND)
#            changed default screen owner from cddev to cdioc 
#        06-APR-2004, Debbie Rogind (drogind):
#            Added /usr/local/bin/ path to perl
#        dd-mmm-yyyy, Whoever (USERNAME):
#            Whatever.
#
#==============================================================

#
# Attach to a named screen session 
#
# 

# IOC_OWNER is unix account under which screen runs
if ( defined $ENV{"IOC_OWNER"} ) {
    $screenOwner = $ENV{"IOC_OWNER"};
} else {
    $screenOwner="cdioc";
}
# If IOC_OWNER_SHELL is set (to bash or sh), then use it for all shell
# commands with the login (-l) option.  Note that the .bashrc file is
# not sourced unless this option is set.  csh and tcsh don't have this
# restriction so IOC_OWNER_SHELL is not required.
if ( defined $ENV{"IOC_OWNER_SHELL"} ) {
    $shell = $ENV{"IOC_OWNER_SHELL"};
    if (($shell == "bash") || ($shell == "sh")) {
	$shell = "$shell -l"
    }
} else {
    $shell = "";
}
# screen utility modified for root privilege
if ( defined $ENV{"SCREENBIN"} ) {
    $screendir = $ENV{"SCREENBIN"};
} else {
    $screendir = "/opt/screen/bin";
}
$screen ="$screendir/screen";

$iocScreenStart= "$shell iocScreenStart.pl";

usage("Too few args") if $#ARGV < 3;

my $iocname = $ARGV[2];

#Start iocScreenStart perl script

print "iocScreen: $iocScreenStart $ARGV[0] $ARGV[1] $iocname $ARGV[3]\n";
$status=system("$iocScreenStart $ARGV[0] $ARGV[1] $iocname $ARGV[3]");
die "command failed: $?" unless $status ==0;

#Attach to named screen session
# command for multi-user mode includes prepending $screenOwner
print "iocScreen: $screen -r -S $screenOwner/$iocname\n";
system("$screen -r -S $screenOwner/$iocname") if $status==0;

sub usage {
	my ($txt) = @_;
	print "Error: $txt\n" if $txt;
	print "\n";
	print "Usage:\n";
	print "    iocScreen  <terminal server> <port> <iocname>\n"; 
	print "where\n";
	print "    <terminal server>    Name of term. server for ioc (e.g. b5as)\n";
        print "      - if no terminal server, then this is node name\n";
        print "      - if soft ioc, then this is the full executable name\n";
	print "    <port>               Port number for ioc (e.g. 2001)\n";
        print "      - if no terminal server, then this is 'telnet' or 'rlogin'\n";
	    print "           (telnet can be postfixed with port number)\n";
        print "      - if soft ioc, then this is 'sioc'\n";
        print "      - if ssh terminal server, then port number is prefixed with 'ssh' (e.g. ssh3033)\n";
        print "    <iocname>            Name of ioc\n";
        print "    <stayup>             Automatic restart (0=no, 1=yes)\n";
	print "\n";
	exit 2;
}














