#!/usr/bin/expect -f
#
# Fix up network settings for an Auger v2.0 board connected
# to the serial port.  Some serial program (like "minicom") must already be
# set up to connect.
#
# A text file with a MAC address entry for the given board serial number
# must exist in the script directory (see MAC_TABLE) below.
#
# Usage is: ./fix_network <serial #> <station id>
#
#--------------------------------------

set MAC_TABLE "mac_table.txt"

set GATEWAY "192.168.61.161"
set SUBNET  "192.168.61"

set SERIAL "minicom"
set ROOTPWD "Watson%7%"

#--------------------------------------

log_user 0
exp_internal 0

set sNum [lindex $argv 0]
set station [lindex $argv 1]

if { $sNum == "" } { 
    puts "Usage: ./fix_network <SERIAL> <STATION ID>"
    exit
}

puts "Fixing network settings for Auger board $sNum on serial port"

#
# Get the MAC address from the table file
#
set mac ""
spawn bash
set timeout 1
expect "\\$ "
send "grep ^$sNum $MAC_TABLE | awk '{print \x24\x32}'\r"
expect -re "..:..:..:..:..:.." {
    set mac $expect_out(0,string)
}
send "exit\r"
expect eof

if { $mac != "" } {
    puts "Found MAC address $mac in table file"
} else {
    puts "Couldn't find MAC address in table file $MAC_TABLE!"
    exit
}
 
spawn $SERIAL
send "\r"
set timeout 3
expect "login: " { send "root\r" } \
       "# " { send "\r" } \
       timeout { puts "Not at login prompt!  Try resetting?" }

expect "# " { send "\r" } \
    "Password: " { send "$ROOTPWD\r"} \
timeout { puts "Couldn't log in; perhaps password has changed?" }

#
# Fix hostname
#
puts "Fixing hostname..."
send "echo \"aera$station\" > /etc/hostname\r"
expect "# "

#
# Fix /etc/network/interfaces file to start eth0 with correct IP address
#
puts "Fixing /etc/network/interfaces..."
send "echo \"\x23 Interfaces autogenerated by fix_network script\" > /etc/network/interfaces\r"
expect "# "
send "echo \"auto lo\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"iface lo inet loopback\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"auto eth0\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"iface eth0 inet static\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"  netmask 255.255.255.0\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"  gateway $GATEWAY\" >> /etc/network/interfaces\r"
expect "# "
send "echo \"  address $SUBNET.$sNum\" >> /etc/network/interfaces\r"
expect "# "

# 
# Fix network startup script
#
puts "Fixing /etc/init.d/S40network..." 
send "sed -e '/ifup -a/ i\ifconfig eth0 hw ether $mac' /etc/init.d/S40network > /tmp/S40network.new\r"
expect "# "
send "mv /tmp/S40network.new /etc/init.d/S40network\r"
expect "# "
send "chmod 755 /etc/init.d/S40network\r"
expect "# "

#
# Fix root password
#
puts "Updating root password..."
send "passwd\r"
expect "assword:"
send "$ROOTPWD\r"
expect "assword:"
send "$ROOTPWD\r"
expect "# "

send "\r"
expect "# "

puts "Done (rebooting)."
send "reboot\r"

set timeout 300
expect "login: "

puts "Done."

exit
