#! /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);
}

$ANSWER = question( "You want restore existing dir, 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("Please Enter the dir name you want to restore   (dir name like L4C1-011-dcs0415-20060817-1321) : "); $DIR_NAME = `head -1`;
printf("Enter DCS Serial number for sure   (four digits like 0123) : "); $DCS_SERIAL = `head -1`;

chop $DATE;
chop $DIR_NAME;

# Update wingDB to point to chamber on test stand
system "gate2wing";
system "ssh root\@trddcs02 /etc/rc.d/init.d/intercomlayer restart";


# Update dcs board location 
$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";
}

# 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);
}


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

printf("$$DIR_NAME is restored to _current dir!\n");

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


system prepare_trd_fee.pl

# EOF
