#! /usr/bin/perl

require "modules/question.pl";

my $testgood = `modules/check_dir.pl | tail -1`;
if ( $testgood != 1 ) {
    printf("Strange directory.\n");
    printf("Please run this script only in Stand1 or Stand2 directory.\n");
    exit;
}


my $pwd = `pwd`;
my $stand;
if ($pwd =~ /Stand([12])$/) {
    $stand = $1;
} else {
    print "Unable to identify test stand\n";
    exit;
}

$ANSWER = question( "You are now in Test Stand $stand, correct? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease check and try again.\n\n");
    exit(2);
}

printf("Creating directory.\n");

$DATE = `/bin/date +%Y%m%d-%H%M`;
printf("Date: $DATE");

printf("Please Enter your initial   (two alphabets like KO) : "); $INITIAL    = `head -1`;
printf("Enter DCS Serial number     (four digits like 0123) : "); $DCS_SERIAL = `head -1`;

chop $DATE;
chop $INITIAL;

# Update wingDB to point to chamber on test stand
system "gate2wing";


#system "wing_settestroc $DCS_SERIAL";

$dcsserial = $DCS_SERIAL;
chop $dcsserial;
if ($stand == 1) {
    system "wing_dcsalias rm 2:0:0";
    system "wing_dcsalias rm dcs$dcsserial";
    system "wing_dcsalias add dcs$dcsserial 2:0:0";
} elsif ($stand == 2) {
    system "wing_dcsalias rm 2:0:1";
    system "wing_dcsalias rm dcs$dcsserial";
    system "wing_dcsalias add dcs$dcsserial 2:0:1";
} else {
    die "Unknown test stand\n";
}

# Restart InterCom Layer to read updated config
system "ssh root\@trddcs02 /etc/rc.d/init.d/intercomlayer restart";

# invoke DB
@lines = `wing_rocinfo $DCS_SERIAL`;
foreach( @lines ) {
    chop;
    if( /ROC=(\S+)$/ ){	$ROC_NAME = $1; }
    if( /ROC_SERIAL=(\S+)$/ ){	$ROC_SERIAL = $1; }
    if( /ROC_LAYER=(\S+)$/ ) {	$ROC_LAYER  = $1; }
    if( /ROC_TYPE=(\S+)$/ )  {	$ROC_TYPE   = $1; }
    if( /DCS_SERIAL=(\S+)$/ ){	$DCS_SERIAL = $1; }

    if( /ROB_POS0=(\S+)$/ ) {	$ROB_POS0 = $1; }
    if( /ROB_POS1=(\S+)$/ ) {	$ROB_POS1 = $1; }
    if( /ROB_POS2=(\S+)$/ ) {	$ROB_POS2 = $1; }
    if( /ROB_POS3=(\S+)$/ ) {	$ROB_POS3 = $1; }
    if( /ROB_POS4=(\S+)$/ ) {	$ROB_POS4 = $1; }
    if( /ROB_POS5=(\S+)$/ ) {	$ROB_POS5 = $1; }
    if( /ROB_POS6=(\S+)$/ ) {	$ROB_POS6 = $1; }
    if( /ROB_POS7=(\S+)$/ ) {	$ROB_POS7 = $1; }

}    

# confirmation
printf("\n");
printf("The chamber you are going to test is:\n\n");
printf("   L%sC%s - #%d\n",  $ROC_LAYER, $ROC_TYPE, $ROC_SERIAL);
printf("   with dcs%s\n",  $DCS_SERIAL);

$ANSWER = question( "\nIs everything ok? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease check DB entries and try again.\n\n");
    exit(2);
}


printf("\n");
printf("The ROBs on the chamber should be :\n\n");

printf("  Pos 7   %4d  --  %4d   Pos 6\n",  $ROB_POS7, $ROB_POS6);
printf("  Pos 5   %4d  --  %4d   Pos 4\n",  $ROB_POS5, $ROB_POS4);
printf("  Pos 3   %4d  --  %4d   Pos 2\n",  $ROB_POS3, $ROB_POS2);
printf("  Pos 1   %4d  --  %4d   Pos 0\n",  $ROB_POS1, $ROB_POS0);

$ANSWER = question( "\nAre these boards mounted in the right positions? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease update the gateDB and try again.\n\n");
    exit(2);
}

$ANSWER = question( "\nGND of HV box connected to nearest ROB GND? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease repair and try again.\n\n");
    exit(2);
}

$ANSWER = question( "\nIs the drift HV return wire connected in the correct place ? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease repair and try again.\n\n");
    exit(2);
}

$ANSWER = question( "\nIs DCS GND connected to the ROC frame? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease connect and try again.\n\n");
    exit(2);
}

$ANSWER = question( "\nAre all patched capacitors (big one) glued? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease tell expert.\n\n");
    exit(2);
}

$ANSWER = question( "\nCable boards are ok? (y or n)", "y", "n" );
if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
    printf("\nPlease repair.\n\n");
    exit(2);
}

$ANSWER = question( "\nWith cooling pipes? (y or n)", "y", "n" );
if( $ANSWER eq 'y' || $ANSWER eq 'Y' ) { $WITH_PIPES = "-wcp"; } else { $WITH_PIPES = ""; }

if( $WITH_PIPES eq "-wcp" ) {
    $ANSWER = question( "\nWith long HV cables? (y or n)", "y", "n" );
    if( $ANSWER ne 'y' && $ANSWER ne 'Y' ) {
	printf("\nPlease connect and try again.\n\n");
	exit(2);
    }
}

$DIR_NAME="L".$ROC_LAYER."C".$ROC_TYPE."-".$ROC_SERIAL."-dcs".$DCS_SERIAL."-".$DATE.$WITH_PIPES;

if( $DIR_NAME =~ /L\dC\d-\d\d\d-dcs\d\d\d\d-\d\d\d\d\d\d\d\d-\d\d\d\d/ && 
    $ROC_SERIAL >= 0 && $ROC_SERIAL <= 999  &&
    $ROC_LAYER  >= 0 && $ROC_LAYER  <= 5    &&
    $ROC_TYPE   >= 0 && $ROC_TYPE   <= 1    &&
    $DCS_SERIAL >= 0 && $DCS_SERIAL <= 9999
) {
    printf("Directory: <%s> created\n", $DIR_NAME );
    system("mkdir $DIR_NAME");

    # Create _current symbolic link
    system( "rm _current" );
    system( "ln -s $DIR_NAME _current" );

    open( FH, ">$DIR_NAME/_setup" ) || die "Could not open _setup file";
    printf( FH "# you can source me in shell script.\n");
    printf( FH "DIR_NAME=$DIR_NAME\n");
    printf( FH "DATE=$DATE\n");
    printf( FH "ROC_SERIAL=$ROC_SERIAL\n");
    printf( FH "ROC_LAYER=$ROC_LAYER\n");
    printf( FH "ROC_TYPE=$ROC_TYPE\n");
    printf( FH "DCS_SERIAL=$DCS_SERIAL\n");
    printf( FH "WITH_PIPES=$WITH_PIPES\n");
    printf( FH "INITIAL=$INITIAL\n");
    close(FH);

    # Create symbolic linke for current project.
    system( "ln -fs $DIR_NAME/_setup ./_setup");

    # Write on history file
    open( FH, ">>_history" ) || die "Could not open _history file";
    printf( FH "$DATE: $DIR_NAME created by $INITIAL\n");
    close(FH);

} else {
    printf("Error in some of your input.\n");
}

system prepare_trd_fee.pl

# EOF
