#!/bin/bash
# By: John Stile
# Purpose: Manually perform the Firmware Update that is normally produced by Another applicaiton.
#   This is to help differentiate between a problem in file transferr vs the other steps.
#   This takes 
#  1. Manual install scirpt io-firmware-update.sh 
#  2. a firmware file: firmware-v1.0.1.1.myfirmware
# sshd on remote system is modified to unpack and install any file named /tmp/firmware.tar.gz
# This scripts will:
# 1.  copy the firmware to /tmp/foo
# 2. Run the script that installs the firmware 

export interface="eth0"
export cal1_ipv6="fe80::fced:faff:fece:f202%$interface"
export fw_orig="cal-firmware-v1.0.1.1.myfirmware"
export fw_tmp="new.foo"
export script="firmware-update.sh"

#======================================
# Define functions used below
#======================================
#--------------------------------------
# USAGE: copy_remote($cal1_ipv6)
#--------------------------------------
copy_remote()
{
export remote_host="$1"
# exectue script
expect  <<EOF
    set timeout 600
    send_user "sending files to $remote_host\n"
    spawn scp -6 -c blowfish -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $script $fw_tmp root@\[$remote_host\]:/tmp/
    expect {
      "assword: " { 
            send "mypassword\n" 
            expect {
              "> " { }
              "$ " { }
              "# " { }
            }
      }
      "(yes/no)? " { 
            send "yes\n"
            expect {
              "> " { }
              "$ " { }
            }
      }
      default {
            send_user "Login failed\n"
            exit
      }
    }
    send_user "Transfer complete\n"
EOF
}
#--------------------------------------
# USAGE: run_remote_script $cal1_ipv6
#--------------------------------------
run_remote_script()
{
export remote_host="$1"
# exectue script
expect  <<EOF
    set timeout 1200

    send_user "connecting to $remote_host\n"
    spawn ssh -6 -c blowfish -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@$remote_host 

    #login handles cases:
    #   login with keys (no user/pass)
    #   user/pass
    #   login with keys (first time verification)
    expect {
      "> " { }
      "$ " { }
      "# " { }
      "assword: " { 
            send "mypassword\n" 
            expect {
              "> " { }
              "$ " { }
              "# " { }
            }
      }
      "(yes/no)? " { 
            send "yes\n"
            expect {
              "> " { }
              "$ " { }
              "# " { }
            }
      }
      default {
            send_user "Login failed\n"
            exit
      }
    }

    # List contents
    send "ls -alF /tmp/\n"

    expect {
        "> " {}
        "# " { }
        default {}
    }

    # Fix permissions on script
    send "chmod +x /tmp/firmware-update.sh\n"

    expect {
        "> " {}
        "# " { }
        default {}
    }

    # Run script 
    send "/tmp/firmware-update.sh REGULAR REGULAR\n"

    expect {
        "finished"
        "> " {}
        "# " { }
        default {}
    }

    send "/bin/reboot&; exit\n"
    expect {
        "closed." {}
        "# " {}
        "> " {}
        "$ " {}
        default {}
    }

    send_user "finished\n"
EOF
}
#======================================
# Main
#======================================
#
# Copy file into tmp 
#
cp $fw_orig $fw_tmp

export counter=0
while [ $counter -lt 1 ]; do
    echo "-----------Count:$counter---------"
    for i in $cal1_ipv6; do
        echo ">>>>>>>>>>>>$i"
        #
        # Try to ping it 
        #
        echo "Ping HOST"
        ping6 -c 1 ${i} > /dev/null
        if [ $? != 0 ]; then 
            echo "Can't ping host"
            continue
        else    
            #
            # Copy files to remote host
            #
            echo "COPY FILES TO CAL"
            copy_remote $i
            #
            # Run the script
            #
            echo "RUN UPDATE ON CAL"
            run_remote_script $i
        fi
    done
    #
    # Incremnt counter
    #
    let counter=$counter+1
done
