#! /usr/bin/perl -w
#
# This module was written by Karl Feaux on May 25, 1997.
# The purpose of this module is to build a SITE file to be
# used by the LAPDOGS program.

print "\n\n\n\n*********COMPUTER SPECIFICS********* \n\n";

print "RFILE EXECUTABLE PATH\n";
print "This variable tells LAPDOGS where the rfile executables are located\n";
print "This can be a relative path from the executable (dwnld_trimble) directory.\n";
print "This can also be an absolute path.\n";
print "Enter path name [bin/]:  ";
chomp ($rfilepath = <>);

if(!($rfilepath)) { $rfilepath = "bin/"; }

while (!(-d $rfilepath) || !(-r $rfilepath && -w $rfilepath)) {
if (!(-d $rfilepath)) {
 print "Directory does not exist. Try Again:  ";
 chomp ($rfilepath = <>);
} elsif (!(-r $rfilepath && -w $rfilepath)) {
 print "File must be readable and writeable. Try Again:  ";
 chomp ($rfilepath = <>);
}
}

if ($rfilepath !~m/\/$/) {$rfilepath = $rfilepath . '/';}
print "rfilepath set to $rfilepath\n\n";
  
print "DATA PATH\n";
print "This can be a relative path from the executable (dwnld_trimble) directory.\n";
print "This can also be an absolute path.\n";
print "Enter data download directory pathname  [Data/]:  "; 
chomp ($dpath = <>);

if(!($dpath)){ $dpath = "Data/"; }

while (!(-d $dpath) || !(-r $dpath && -w $dpath)) {
if (!(-d $dpath)) {
 print "Directory does not exist. Try Again:  ";
 chomp ($dpath = <>);
} elsif (!(-r $dpath && -w $dpath)) {
 print "File must be readable and writeable. Try Again:  ";
 chomp ($dpath = <>);
}
}

if ($dpath !~m/\/$/) {$dpath = $dpath . '/';}
print "dpath set to $dpath\n\n";

print "MODEM PATH\n";
print "This variable tells LAPDOGS where the modem configuration files \nfor rfile are located\n";
print "This can be a relative path from the executable (dwnld_trimble) directory.\n";
print "This can also be an absolute path.\n";
print "Enter modem path name [Modem/]:  ";
chomp ($mpath = <>);

if(!($mpath)){ $mpath = "Modem/";}

while (!(-d $mpath) || !(-r $mpath && -w $mpath)) {
if (!(-d $mpath)) {
 print "Directory does not exist. Try Again:  ";
 chomp ($mpath = <>);
} elsif (!(-r $dpath && -w $mpath)) {
 print "File must be readable and writeable. Try Again:  ";
 chomp ($mpath = <>);
}
}

if ($mpath !~m/\/$/) {$mpath = $mpath . '/';}
print "mpath set to $mpath\n\n";

print "**************SITE SPECIFICS************* \n\n";
print "Enter 4 character station ID - Alphanumerics Only:  "; 
chomp ($id = <>);

$size = length($id);
while ($id !~m/[a-zA-Z0-9]{$size}/ || !($size)) {
print "Invalid Station ID. Try again:  ";
chomp ($id = <>);
$size = length($id);
}


print  "\nStation  id set to $id \n\n";

# Prompts the user to enter the long site name and checks
# that it is less than 256 characters long.


print "Enter site name. Name must be between 0-255 characters long:  "; 
chomp ($name = <>);

while (length($name) > 255) {
print "Invalid Site Name. Try again:  ";
chomp ($name = <>);
}
print "\nSite name set to $name \n\n";

# Prompts the user to enter the antenna serial number and checks
# that the input is a numeric charater.
#

print "Enter antenna serial number [UNKNOWN]:  "; 
chomp ($ant = <>);

if(!($ant)){ $ant = "UNKNOWN";}

$size = length($ant);
while ($ant !~m/[a-zA-Z_0-9]{$size}/) {
print "Invalid Serial Number($ant). Try again [UNKNOWN]:  ";
chomp ($ant = <>);
if(!($ant)){ $ant = "UNKNOWN";}
$size = length($ant);
}

print "\nAntenna serial number set to $ant \n\n";

print "Enter Computer Name or Host Name(if Networked):  ";
chomp ($compname = <>);
if (!$compname) {
   $compname ="unknown";
} else {
    $size = length($compname);
    while ($compname !~m/[a-zA-Z\.0-9]{$size}/) {
      print "Invalid computer name. Leave blank for unknown. Try again:  ";
      chomp ($compname = <>);
      $size = length($compname);
      if (!$compname) {$compname ="unknown";} 
    }
}

print "\nComputer name set to $compname.\n\n";

print "Enter Monument ID [UNKNOWN]:  ";
chomp ($monid = <>);

if (!$monid) {
   $monid ="UNKNOWN";
} else {
    $size = length($monid);
    while ($monid !~m/[a-zA-Z0-9]{$size}/) {
      print "Invalid monument ID. Leave blank for unknown. Try again:  ";
      chomp ($monid = <>);
      $size = length($monid);
      if (!$monid) {$monid ="UNKNOWN";} 
    }
}

print "\nMonument ID set to $monid.\n\n";

# Prompts the user to enter the file name for the UNIX port and 
# checks that the input is a valid file name. 

print "Enter port filename (Ex. /dev/cua/c): ";
chomp ($port = <>);
while  (!(-e $port || -w $port)) {
  print "Invalid file name. Please try again: ";
  chomp ($port = <>);
}

print "\nPort name checked and set to $port.\n\n";

# Prompts the user to enter the baud rate, control settting, and
# type of connection.

@speeds = (1200,4800,9600,19200,38400);
print "PORT BAUD RATE - Select (0-4):\n\n";
print "0 - 1200\n1 - 4800\n2 - 9600\n3 - 19200\n4 - 38400\n\n";
chomp ($speed = <>);

while ($speed !~ /[0-4]/ || length($speed) != 1) {
print "Invalid selection. Please select again (0-4):  ";
chomp ($speed = <>);
}
print "\nPort baud rate set to $speeds[$speed] \n\n";
#
@controls = ('8N1','8N2','8E1','8O1','7E1','7O1');
print "PORT CONTROL - Select (0-5):\n";
print "Control information for port to match modem or reciever.\n";
print "0 - 8N1\n1 - 8N2\n2 - 8E1\n3 - 8O1\n4 - 7E1\n5 - 7O1\n\n";
chomp ($control = <>);

while ($control !~ /[0-5]/ || length($control) != 1) {
print "Invalid selection. Please select again (0-5):  ";
chomp ($control = <>);
}
print "\nPort control set to $controls[$control] \n\n";
#
@cons = ('DIRECT','MODEM','FREEWAVE');
print "PORT CONECTION  - Select (0,1,2):\n\n";
print "0 - DIRECT\n1 - MODEM\n2 - FREEWAVE(Mode 6 only, otherwise DIRECT)\n";
chomp ($con = <>);

while ($con !~ /[0-2]/ || length($con) != 1) {
print "Invalid selection. Please select again (0,1,2):  ";
chomp ($con = <>);
}
print "\nPort Connection set to $cons[$con] \n\n";

#
# This part prompts the user for the modem specifics and performs
# various tests to check the existence of certain program files.

if ($con == 1 ) {
  print "Enter modem type: ";
  chomp ($modtype = <>);
  $modtype = lc($modtype);
  
  print "\nModem type is set to $modtype \n\n";

} else { $modtype = "UNKNOWN"; }

if ($con == 1 || $con == 2 ) {
  
  print "Enter modem dialstring (Ex. ATD0): ";
  chomp ($dial = <>);
  $dial = uc($dial);
 
  while ( $dial !~ /^ATD[TP0-9,]/) {
  print "Invalid Dialstring. Please try again: \n";
  chomp ($dial = <>);
  $dial = uc($dial);
 }

print "\nDial string set to $dial.\n\n";

print "Enter modem scale factor (1-9) [1]:  "; 
chomp ($mscale = <>);

if(!$mscale) { $mscale = 1; }

if (length($mscale) != 1 || $mscale !~m/[1-9]/) {
  print "Invalid scale factor... Setting to default value (3).\n\n";
  $mscale = 3;
}

} else { $dial = "UNKNOWN"; $mscale = "UKNOWN"; }

print "\nModem scale factor set to $mscale.\n\n";


print "***********SESSION SPECIFICS***********\n\n";

print "\n\nEnter Number of times to attempt to get data [3]:  ";
chomp ($retry = <>);
if(!($retry)){$retry = 3; print STDOUT "\nDefault = 3\n"; }
$size = length($retry);

if ($retry <= 0 || $retry !~m/[0-9]{$size}/) {
  print "Invalid value. Setting to default value (3):  ";
  $retry = 3;
} 

print "\n\nEnter the number of times that the program will try to download\n";
print "a file after connecting to the receiver [3]:  ";
chomp ($fretry = <>);
if(!($fretry)){ $fretry = 3; print "\nDefault = 3\n"; }
$size = length($fretry);

if ($fretry <= 0 || $fretry !~m/[0-9]{$size}/) {
    print "Invalid value. Setting to default value (3). \n\n";
    $fretry = 3;
} 

print "\nEnter the number of times for the program to attempt a purge [3]:  ";
chomp ($pretry = <>);
if(!($pretry)) { $pretry = 3; print "\nDefault = 3\n";}
$size = length($pretry);

if ($pretry <= 0 || $pretry !~m/[0-9]{$size}/) {
   print "Invalid value. Setting to default value (3). \n\n";
   $pretry = 3;
}


@noyes = (NO,YES);

if( $con ==2 ) {
	$fdl = 0;
} else {

print "\nWould you like the fast download flag set? - Enter (0 or 1): \n";
print "This does not work with Freewave Modems(Enter 0 for freewave)!!!!\n";
print "0 - NO\n1 - YES\n";
chomp ($fdl =  <>);
while ($fdl !~ /[0-1]/ || length($fdl) != 1) {
   print "Invalid Input.  Please enter 0 or 1: \n";
   chomp ($fdl = <>);
}
}
 
print "\nSession Fastdownload Flag set to $noyes[$fdl].\n\n";

# Writes user supplied inputs to file called ($id).site

open(SITE,">Sites/" . $id . ".site");
$date = `date`;

print SITE "#This is a SITE file, an input file for the LAPDOG program. \n";
print SITE "#This file was created on $date \n\n";       
print SITE "# Computer Specifics \n"; 
print SITE "env.rfilepath : $rfilepath \n";
print SITE "env.downloadpath : $dpath \n";
print SITE "env.modempath : $mpath \n\n";
print SITE "# Site specifics \n";
print SITE "site.id : $id \n";
print SITE "site.name : $name \n";
print SITE "site.antenna_serial : $ant \n";
print SITE "site.computer_id : $compname \n";
print SITE "site.monument_id : $monid \n\n";
print SITE "# Connection specifics \n";
print SITE "port.port : $port \n";
print SITE "# Valid choices are (1200,4800,9600,19200,38400) \n";
print SITE "port.speed : $speeds[$speed] \n";
print SITE "# Valid choices are (8N1,8N2,8E1,8O1,7E1,7O1) \n";
print SITE "port.control : $controls[$control] \n";
print SITE "# Valid choices are (DIRECT,MODEM) \n";
print SITE "port.connection : $cons[$con] \n\n";
if ($con) {
   print SITE "# Modem Specifics \n";
   print SITE "modem.type : $modtype \n";
   print SITE "modem.dialstring : $dial \n";
   print SITE "modem.scale_factor : $mscale \n\n";
}
print SITE "# Session Specifics \n";
print SITE "session.fastdownload : $noyes[$fdl] \n"; 
print SITE "session.retry : $retry \n";
print SITE "session.f_retry : $fretry \n";
print SITE "session.p_retry : $pretry";
close (SITE);

print STDOUT "\n\nMake site complete\n";
