#!/usr/bin/expect -f
#
# Set up scope device files, DAQ user, and copy over 
# DAQ skeleton to board.  Board must be set up 
# already to allow ssh access by root.
#
# Usage is: ./setup_daq <serial #> <station id>
#
#--------------------------------------

set GATEWAY "192.168.61.161"
set SUBNET  "192.168.61"

set ROOTPWD "Watson%7%"
set DAQPWD  "Watson7%7"

set SKELDIR "../skel"

set L3CONF  "/home/daq/l3_waiter.conf"
set LSINIT  "/home/daq/LS_init"

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

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

if { ($sNum == "") || ($station == "") } { 
    puts "Usage: ./setup_daq <SERIAL #> <STATION ID>"
    exit
}

set ipaddr "$SUBNET.$sNum"
puts "Setting up DAQ on Auger board $sNum at $ipaddr"
 
set timeout 5
spawn ssh root@$ipaddr

expect {
    "Are you sure you want to continue connecting (yes/no)?" { 
        send "yes\r" 
        expect {
            "assword: " { 
                send "$ROOTPWD\r" 
            }
            "# " { 
            }
        }
    }
    "assword: " { 
        send "$ROOTPWD\r" 
    }
    "# " { 
    }
    timeout {puts "Couldn't log in; check password?" }
}

send "\r"
expect "# "

#
# Add device files
#
puts "Adding device files..."
send "rm -f /dev/fpga\r"
expect "# "
send "mknod -m 600 /dev/fpga c 61 0\r"
expect "# "

send "rm -f /dev/scope\r"
expect "# "
send "mknod -m 666 /dev/scope c 62 0\r"
expect "# "

#
# Add daq user
#
puts "Adding DAQ user..."

send "adduser -h /home/daq daq\r"
expect {
    "assword:" {
        send "$DAQPWD\r"
        expect "assword:"
        send "$DAQPWD\r"
        }
    "in use" {
        puts "DAQ user already exists, not updating."
    }
}
expect "# "
send "exit\r"
expect eof

#
# Set station ID in l3_waiter.conf
#

set timeout 120
spawn bash
expect "$ "

puts "Fixing station ID in L3 configuration file..."

send "sed -e 's/station_id \[0-9\]*/station_id $station/' $SKELDIR$L3CONF > $SKELDIR$L3CONF.tmp\r"
expect "$ "
send "mv $SKELDIR$L3CONF.tmp $SKELDIR$L3CONF\r"
expect "$ " 

puts "Fixing station ID in LS_init..."
send "sed -e 's/aera\[0-9\]* \[0-9\]*/aera$station $station/' $SKELDIR$LSINIT > $SKELDIR$LSINIT.tmp\r"
expect "$ "
send "mv $SKELDIR$LSINIT.tmp $SKELDIR$LSINIT\r"
expect "$ " 

#
# Rsync files from skeleton directory to board
# Copy as root:
#   etc/
#   root/
# Copy as daq:
#   home/daq
#

puts "Copying DAQ files to board..."

send "rsync -rlp -e ssh $SKELDIR/etc root@$ipaddr:/\r"
expect {
#    "$ " { send "\r" }
    "assword:" {send "$ROOTPWD\r"}
}
expect "$ "

send "rsync -rlp -e ssh $SKELDIR/usr root@$ipaddr:/\r"
expect {
#    "$ " { send "\r" }
    "assword:" {send "$ROOTPWD\r"}
}
expect "$ "

send "rsync -rlp -e ssh $SKELDIR/root root@$ipaddr:/\r"
expect {
#    "$ " { send "\r" }
    "assword:" {send "$ROOTPWD\r"}
}
expect "$ "

send "rsync -rlp -e ssh $SKELDIR/home daq@$ipaddr:/\r"
expect {
#    "$ " { send "\r" }
    "assword:" {send "$DAQPWD\r"}
}
expect "$ "

# Now reboot
puts "Rebooting..."

send "su - \r"
expect "assword:"
send "$ROOTPWD\r"
expect "# "
send "reboot\r"

set timeout 300
expect "login: "

puts "Done."
exit
