#!/bin/bash
# Copyright 2008-2013 Chad Lemmen http://www.lemmen.com

# Uncomment the following line to test file creation without sending...
#exit 1

achpath=$1
verifyach=$2
protocol=$3
username=$4
password=$5
host=$6

todos < "$verifyach" > /tmp/verifyach.tmp
mv /tmp/verifyach.tmp "$verifyach"

if [ "$protocol" = "sftp" ]; then
  /usr/bin/expect <<- EOD

  set timeout 60
  spawn sftp "$username@$host"
  expect {
    "*continue connecting (yes/no)?" {
       send "yes\r"
       exp_continue
    }
    "*Connection timed out" {
      exit 1
    }
    "*Connection reset by peer" {
      exit 1
    }
    "*password:*" {
      send "$password\r"
      exp_continue
    }
    "sftp> " { 
      send "put $achpath\r"
    }
  }

  expect {
    "*No such file or directory" {
      exit 1
    }
    "*not found." {
      exit 1
    }
    "sftp> " {
      send "put $verifyach\r"
    }
  }
  expect "sftp> " 
  send "exit\r"
  expect eof
  #interact

EOD
fi

status=$?

# Make a copy of the verify report
cp "$verifyach" /tmp/$LOGNAME-verifyach.bak

exit $status

