#
# Initialize the board. The function is executed before any test.
#
proc __boardname___init { board } {
  set hostname [board_info $board hostname]
  set timeout [board_info $board timeout]
  set ssh_options [board_info $board ssh,options]
  set runtimes [board_info $board runtimes]
  set tmpdir [board_info $board tmpdir]
  verbose -log "Opening persistent connection ..." 1
  eval "exec ssh -N -f $ssh_options root@$hostname &"
  local_exec "ssh -n $ssh_options root@$hostname sh -c 'mkdir -p $tmpdir'" \
    {} {}  $timeout
}

#
# Remove test run by-products. The function is executed at DejaGNU exit.
#
proc __boardname___exit {} {
  set board "__boardname__"
  set hostname [board_info $board hostname]
  set ssh_options [board_info $board ssh,options]
  set tmpdir [board_info $board tmpdir]
  verbose -log "Closing persistent connection ..." 1
  local_exec "ssh $ssh_options -O exit root@$hostname" {} {} 10
  verbose -log "Cleaning up - executing on board 'rm -fr $tmpdir' ..." 1
  local_exec "ssh -n $ssh_options root@$hostname sh -c 'rm -fr $tmpdir'" \
    {} {} 10
}

#
# Upload a file to the board. Uses scp over persistent SSH connection.
#
proc __boardname___download { board file args } {
  set hostname [board_info $board hostname]
  set tmpdir [board_info $board tmpdir]
  set timeout [board_info $board timeout]
  set ssh_options [board_info $board ssh,options]
  set destfile [lindex [file split $file] end]
  verbose -log "scp -q $ssh_options $file root@$hostname:$tmpdir/"
  set result [local_exec "scp -q $ssh_options $file root@$hostname:$tmpdir/" \
                {} {} $timeout]
  if { [lindex $result 0] != 0 } {
    verbose -log "failed to upload \'$file\' to \'$tmpdir/$destfile\'"
  } else {
    verbose -log "uploaded \"$file\' to remote board@\'$tmpdir/$destfile\'"
    return "$tmpdir/$destfile"
  }
}

#
# Download a file to the host machine. Uses scp over persistent SSH connection.
#
proc __boardname___upload { board file args } {
  set hostname [board_info $board hostname]
  set tmpdir [board_info $board tmpdir]
  set timeout [board_info $board timeout]
  set ssh_options [board_info $board ssh,options]
  set filen [file tail $file]
  verbose -log "scp -q $ssh_options \"root@$hostname:$tmpdir/$filen\" ."
  set result [local_exec \
                "scp -q $ssh_options \"root@$hostname:$tmpdir/$filen\" ." \
                {} {} $timeout]
  if { [lindex $result 0] != 0 } {
    verbose -log \
      "failed to transfer \"root@$hostname:$tmpdir/$filen\" to \".\""
  } else {
    verbose -log "transferred \"root@$hostname:$tmpdir/$filen\" to \".\""
    # In case of success, always return the original file.
    return "$file"
  }
}

#
# Cache program output within different invoking of __boardname___exec.
# For example, the following command sequence will be executed
#   > cd /tmp/dejagnu_xxxx/ && ./xxx.x0
#   <output1 here>
#   return [0, <output1>]   (a)
#   > rm /tmp/dejagnu_xxxx/xxxx.x0
#   <output2 here>
#   return [0, <output2>]   (b)
# We need <output1>, not <output2>. What we do here is to keep <output1> in
# $program_output and in (b) we return [0, <output1>].
#
set program_output ""

#
# Execute a test on remote machine. Log into the target machine using
# persistent SSH connection and run a command in modified environment.
#
proc __boardname___exec { board program args } {
  global program_output
  if { [llength $args] > 0 } {
    set pargs [lindex $args 0]
  } else {
    set pargs ""
  }

  if { [llength $args] > 1 } {
    set inp "[lindex $args 1]"
  } else {
    set inp ""
  }

  if { [llength $args] > 2 } {
    set outp "[lindex $args 2]"
  } else {
    set outp ""
  }

  if { [llength $args] > 3 } {
    set timeout "[lindex $args 3]"
  } else {
    set timeout [board_info $board timeout]
  }

  set hostname [board_info $board hostname]
  set tmpdir [board_info $board tmpdir]
  set other_file ""

  # Check if a file to be executed was copied from host machine.  If so, we
  # need to run it in copied runtimes.
  set is_program "0"
  if { [string match "$tmpdir/*" $program] } {
    set path [file dirname $program]
    # "$program" would usually be like "/x/y/z.out", set command to be "z.out".
    set command [file tail $program]
    set rootname [file rootname $command]
    # TODO(shenhan): using rsync to copy all test case relatd stuff to host
    # machine in case ".o" files are different from the exe files.
    set other_file [file join $path "${rootname}.*"]
    # Change directory to "/x/y", then execute "./z.out" - we want the working
    # directory to be "/x/y". Setting GCOV_PREFIX_STRIP and GCOV_PREFIX is to
    # force generating ".gcda" file under "/x/y" instead of some host path.
    set program "cd $path && GCOV_PREFIX_STRIP=999 GCOV_PREFIX=$tmpdir/ \
                 [file join "." $command]"
    set is_program "1"
  }
  verbose -log "Exec: $program"
  set ssh_options [board_info $board ssh,options]
  set retv [local_exec \
              "ssh -n $ssh_options root@$hostname sh -c '$program $pargs'" \
              $inp $outp $timeout]
  set status [lindex $retv 0]
  if { $is_program == "1" } {
    set program_output [lindex $retv 1]
  }

  # Before returning the execution status, we try to transfer the ".gcda"
  # (and/or other files that have the same base name as the program) file to
  # host, though for every program that runs, there is no corresponding "other"
  # file. We have no idea when such an other file will be generated for the
  # program, so every time, we assume there is an "other" file and try to do the
  # transfer.
  if { $status == 0 && $other_file != "" } {
    set upv [${board}_upload $board $other_file ""]
    if { $upv == "" } {
      verbose -log "Safely ignored - \"$other_file\" does not exist."
    }
  }

  return [list $status $program_output]
}

load_generic_config "unix"
load_base_board_description "linux-libremote"

set_board_info hostname "__board_hostname__"
set_board_info tmpdir "__tmp_dir__"

set_board_info isremote 1
set_board_info timeout 60
set_board_info ssh,options "-i __tmp_testing_rsa__ -o ControlMaster=auto \
-o ControlPath=__tmp_dir__/%r@%h:%p -o StrictHostKeyChecking=no "
