#!/usr/bin/expect --
# *********************************************************************
# This is an Expect script.
#
#       This script is stored in /twc/scripts on each STAR XL and invoked during
#       rebooting through S99RebootInfo in /etc/rc2.d directory.
#
#       This script will dial the modem and communicate to the  host 
# that the star has rebooted. 
#
#       STAR is identified by the hard disk serial number which is unique supplemented
# by the hostname and approximate time of rebooting.
#
#       If you wish to test the script without actually rebooting then please use
# /usr/sgitcl/bin/expect -f dial.sh 
#
#
# *********************************************************************

set Repeat 0
set Count 0
set scriptDir "/twc/scripts"
set CurrentTime [timestamp -format "%c %Z"] 
set IP [exec $scriptDir/getIP.sh]
set SerNum [exec sysinfo -s]
set HexNum [exec echo "ob=16; $SerNum" | bc]
set OSName [exec uname -sr]
set CPUType [exec uname -m]
set SCSI0 [ exec $scriptDir/getserial.sh sc0d1l0] 
set SCSI1 [ exec $scriptDir/getserial.sh sc0d2l0]
set hdman [exec $scriptDir/gethdman.sh]
set SBEserial [exec $scriptDir/docom.sh /usr/psc/bin/vcom_stat PUTS]
regsub -all \x14 $SBEserial "" SBEserial
set CPUMem [exec $scriptDir/getmem.sh]
set flashprom [exec $scriptDir/docom.sh hinv FLASH]
set crimestr [exec $scriptDir/docom.sh systune crime_rev]
scan $crimestr "(0x%d)" crimerev
set StarID [exec $scriptDir/doexport.sh c_star_id]
set StarDomain [exec $scriptDir/doexport.sh c_domain_id]
set StarModel [exec $scriptDir/doexport.sh c_star_type]
set PatchLevel [exec $scriptDir/doPatch.sh]

set propname "0800$HexNum.dat"
puts "Creating file $propname"
set file [open "$scriptDir/$propname" w]
puts $file "curtime=$CurrentTime"
puts $file "serial=0800$HexNum"
puts $file "ram=$CPUMem"
puts $file "ip_deploy=$IP"
puts $file "hdd_man=$hdman"
puts $file "disk0_id=$SCSI0"
puts $file "disk1_id=$SCSI1"
puts $file "star_id=$StarID"
puts $file "domain_id=$StarDomain"
puts $file "model_no=$StarModel"

puts $file "hw_cputype=$CPUType"
puts $file "sw_osversion=$OSName"
puts $file "hw_flashprom=$flashprom"
puts $file "hw_sbeversion=$SBEserial"
set crime10 [expr $crimerev / 10]
set crimedig [expr $crimerev % 10]
puts $file "hw_crime_rev=$crime10.$crimedig"

puts $file "sw_twcpatchlevel=\"${PatchLevel}\""
close $file

set FTPHost [exec $scriptDir/doexport.sh c_svr_host]
set ftp "/usr/bsd/ftp"
set wxlUser [exec $scriptDir/doexport.sh c_svr_user]
set wxlPwd  [exec $scriptDir/doexport.sh c_svr_pwd]

if {$FTPHost != ""} {
    set Repeat 1
}

set timeout 5

while {$Repeat == 1} { 

 set Count [expr $Count + 1]
 if {$Count > 3} {
        puts "Exiting after 3 attempts"
	set file [open "$scriptDir/$propname" a]
	puts $file "ftpstatus=\"FAILED to ftp to $FTPHost $wxlUser $wxlPwd\""
	close $file
	exit 1
	}

puts "ftp $FTPHost using account $wxlUser, password $wxlPwd"
puts "Try # $Count"

spawn ftp $FTPHost

expect timeout {send "quit\r"; set Repeat 1; continue;} "Name"
send "$wxlUser\r"
expect "Password:"
send "$wxlPwd\r\n"
expect "230*" {set Repeat 0}
send "bin\r"
expect "200*"
send "put $scriptDir/$propname unregistered/$propname\r"
expect "local*"
send "quit\r"
expect "221*"
       
}

if {$Count > 0} {
set file [open "$scriptDir/$propname" a]
puts $file "ftpstatus=\"SUCCESSFULLY sent to $FTPHost\""
close $file
}

exit 0


