#!/usr/bin/expect -f

# argv 0 remote hostname
# argv 1 dir + filename 
# argv 2 userid 
# argv 3 passwd 

#system "echo argv [string length $argv]\n"
#system "echo  0 = [lindex $argv 0]\n"
#system "echo  1 = [lindex $argv 1]\n"
#system "echo  2 = [lindex $argv 2]\n"
#system "echo  3 = [lindex $argv 3]\n"

#Do not want to time out before the tar completes
set timeout 1500

spawn  bash 

set prompt "(%|#|\\\$) $"
catch {set prompt $env(EXPECT_PROMPT)}


#cd to base directoy
send -- "cd /backup/disk\r" 
expect -- "#"

send -- "/usr/bin/ssh -o StrictHostKeyChecking=no [lindex $argv 2]@[lindex $argv 0] cat \"<\" [lindex $argv 1] | tar -xPzps --atime-preserve 2>/dev/null\r"

while 1 {
 expect -re "yes/no" {
      send  "yes\r"
  } -re "assword:" {
      send  "[lindex $argv 3]\r"
      break
  }
}

expect eof {send -- "echo $?\r "} "#" {send -- "echo $?\r"} $prompt {send -- "echo $?\r"}
expect -re "\r\n(.*)\r\n"
exit $expect_out(1,string) 







       
       

