#!/usr/bin/expect
log_user 0

set L_OPES [lindex $argv 0]
set L_IP  [lindex $argv 1]
set L_USERNAME [lindex $argv 2]
set L_PASSWORD_FILE [lindex $argv 3]
set L_OPTION [lindex $argv 4]
set L_PARAMETER1 [lindex $argv 5]
set L_PARAMETER2 [lindex $argv 6]
set L_PARAMETER3 [lindex $argv 7]

set L_PASSWORD ""
set L_ROOT_PASSWORD ""

set L_ROW_INDEX 1
set L_FD [open "${L_PASSWORD_FILE}" r]
set L_DATA_PWDS [read $L_FD]
foreach pwd $L_DATA_PWDS {
    if { 1 == ${L_ROW_INDEX} } {
        set L_ROOT_PASSWORD "${pwd}"
    } else {
        set L_PASSWORD "${pwd}"
        break
    }
    set L_ROW_INDEX [expr {${L_ROW_INDEX} + 1}]
}
close $L_FD

set L_TMPFILE [exec sh -c { tr -dc '0-9' < /dev/urandom | head -c 10 } ]
exec /tmp/upgrade/enctool/AES2KMC dec $L_PASSWORD $L_TMPFILE &
exec sleep 5
set L_PASSWORD [exec cat /tmp/upgrade/enctool/$L_TMPFILE]

set L_TMPFILE [exec sh -c { tr -dc '0-9' < /dev/urandom | head -c 10 } ]
exec /tmp/upgrade/enctool/AES2KMC dec $L_ROOT_PASSWORD $L_TMPFILE &
exec sleep 5
set L_ROOT_PASSWORD [exec cat /tmp/upgrade/enctool/$L_TMPFILE]

set L_CONVERTED_IP "${L_IP}"
set L_IPV6_FLAG_POST [ string first ":" "${L_IP}" ]
if { ${L_IPV6_FLAG_POST} >= 0 } {
    set L_CONVERTED_IP "\[${L_IP}\]"
}

if { $L_OPES == "dispatch" } {
    set timeout 300
    set L_LOGIN_COUNT 0
    set L_MKDIR_CMD "rm -rf /tmp/upgrade;mkdir /tmp/upgrade;mv \"/home/$L_USERNAME/$L_OPTION\" \"/tmp/upgrade\""
    set L_UNZIP_CMD "cd /tmp/upgrade;tar -zxvf \"$L_OPTION\""
    
    spawn scp -r -P 22 "/tmp/upgrade/$L_OPTION" "$L_USERNAME@$L_CONVERTED_IP:~"
    expect {
        "*(yes/no)*" {send "yes\n";exp_continue;}
        "*assword*" {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send "$L_PASSWORD\n";
            exp_continue; } 
        "*passphrase*" {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send "$L_PASSWORD\n";
            exp_continue; } 
        -re {.*100%} { }
        timeout {exit 44}
    }
    
    set L_LOGIN_COUNT 0
    spawn -noecho ssh -t $L_USERNAME@$L_IP
    expect {
        *(yes/no)* {send -- yes\r;exp_continue;}
        *assword:* {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send -- $L_PASSWORD\r;exp_continue;}
        *passphrase* {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send "$L_PASSWORD\n";
            exp_continue; } 
        -re {.*>} { }
        -re {.*#} { }
        -re {.*\$} { }
        timeout {exit 44}
    }

    set L_LOGIN_COUNT 0
    send "su - root\r"
    expect {
        *(yes/no)* {send -- yes\r;exp_continue;}
        *assword:* {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send -- $L_ROOT_PASSWORD\r;exp_continue;
        }   
        *>* {exit 42;}
        -re {.*>} { }
        -re {.*#} { }
        -re {.*\$} { }
        -re {.*refused.*} {exit 45;}
        -re {.*Permission denied.*} {exit 42;}
        timeout {send "exit\r";exit 44;}
    }
    
    set L_PASSWORD "******"
    set L_ROOT_PASSWORD "********"
    
    send "$L_MKDIR_CMD;echo RESULT:$?\r"
    expect -re "RESULT:(\[0-9\]+)" 
    if {$expect_out(1,string)!="0"} {
         exit $expect_out(1,string)
    }
    send "$L_UNZIP_CMD;echo RESULT:$?\r"
    expect -re "RESULT:(\[0-9\]+)" 
    if {$expect_out(1,string)!="0"} {
         exit $expect_out(1,string)
    }
    
} elseif { $L_OPES == "test_login" } {
    set timeout 30 
    spawn -noecho ssh -t $L_USERNAME@$L_IP
    set L_LOGIN_COUNT 0
    expect {
        *(yes/no)* {send -- yes\r;exp_continue;}
        *assword:* {
            incr L_LOGIN_COUNT 1
            if { $L_LOGIN_COUNT > 1 } {exit 41;}
            send -- $L_PASSWORD\r;exp_continue;}
        *passphrase* {
            incr L_LOGIN_COUNT 1
            if { $L_LOGIN_COUNT > 1 } {exit 46;}
            send "$L_PASSWORD\n"; exp_continue; }
        -re {.*>} { }
        -re {.*#} { }
        -re {.*\$} { }
        -re {.*Permission denied \(publickey\).*} {exit 43;}
        -re {.*Host key verification failed.*} {exit 41;}
        -re {.*No route to host.*} {exit 41;}
        -re {.*Received disconnect.*} {exit 41;}
        -re {.*Invalid argument.*} {exit 41;}
        -re {.*Name or service not known.*} {exit 41;}
        -re {.*refused.*} {exit 45;}
        timeout {exit 44;}
    }

    set L_LOGIN_COUNT 0
    send "su - root\r"
    expect {
        *(yes/no)* {send -- yes\r;exp_continue;}
        *assword:* {
            incr L_LOGIN_COUNT 1
            if { $L_LOGIN_COUNT > 1 } {exit 42;}
            send -- $L_ROOT_PASSWORD\r;exp_continue;
        }
        *>* {exit 42;}
        -re {.*>} { }
        -re {.*#} { }
        -re {.*\$} { }
        -re {.*refused.*} {exit 45;}
        -re {.*Permission denied.*} {exit 42;}
        timeout {send "exit\r";exit 44;}
    }
    
} else {
    set timeout 600
    set L_LOGIN_COUNT 0
    spawn -noecho ssh -t $L_USERNAME@$L_IP
    expect {
        *(yes/no)* {send -- yes\r;exp_continue;}
        *assword:* {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send -- $L_PASSWORD\r;exp_continue;}
        *passphrase* {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send "$L_PASSWORD\n";
            exp_continue; } 
        -re {.*>} { }
        -re {.*#} { }
        -re {.*\$} { }
        timeout {exit 44;}
    }

    set L_LOGIN_COUNT 0
    send "su - root\r"
    expect {
        *(yes/no)* {send -- yes\r;exp_continue;}
        *assword:* {
            incr L_LOGIN_COUNT 1;
            if { $L_LOGIN_COUNT>1 } {exit 1}
            send -- $L_ROOT_PASSWORD\r;exp_continue;
        }
        *>* {exit 42;}
        -re {.*>} { }
        -re {.*#} { }
        -re {.*\$} { }
        -re {.*refused.*} {exit 45;}
        -re {.*Permission denied.*} {exit 42;}
        timeout {send "exit\r";exit 44;}
    }
    
    set L_PASSWORD "******"
    set L_ROOT_PASSWORD "********"
    
    send "ls \"/tmp/upgrade/\"|grep \"^patch\\.sh\\$\";echo \"RESULT:$?\"\r"
    expect -re "RESULT:(\[0-9\]+)" 
    if {$expect_out(1,string)!="0"} {
         exit 80;
    }
    
    set timeout 1000
    send "sh /tmp/upgrade/patch.sh \"$L_OPTION\" \"$L_PARAMETER1\" \"$L_PARAMETER2\" \"$L_PARAMETER3\";echo RESULT:$?\r"
    expect -re "RESULT:(\[0-9\]+)" {
        set ret $expect_out(1,string);
        send "exit\r";
        send "exit\r";
        exit $ret;
    }
    
}

set L_PASSWORD "******"
set L_ROOT_PASSWORD "********"
send "exit\r"
send "exit\r"
exit 0

