#!/usr/bin/expect

set timeout 30
set pythonversion [lindex $argv 0]
set gccoptions [lindex $argv 1]
set withcuda [lindex $argv 2]
set withopencl [lindex $argv 3]
set cudapath [lindex $argv 4]
set cudacap [lindex $argv 5]
set withmkldnn [lindex $argv 6]
set mkldnnpath [lindex $argv 7]
set pythonpath /usr/bin/python$pythonversion

if {[regexp "^2\." $pythonversion]} {
  set pythondistpath /usr/lib/python$pythonversion/dist-packages/
} else {
  set pythondistpath /usr/lib/python3/dist-packages/
}

spawn ./configure

expect "Please specify the location of python.*"
send "$pythonpath"
send "\r"

expect "Please input the desired Python library path to use.*"
send "$pythondistpath"
send "\r"

expect "Do you wish to build TensorFlow with MKL support*"
if {$withmkldnn == Yes} {
  send "Y\r"
  expect "Do you wish to download MKL LIB from the web*"
  send "N\r"
  expect "Please specify the location where MKL is installed.*"
  send "$mkldnnpath"
  send "\r"
} else {
  send "N\r"
}

expect "Please specify optimization flags to use during compilation when bazel option*"
send -- $gccoptions
send "\r"

expect "Do you wish to use jemalloc as the malloc implementation*"
send "Y\r"

expect "Do you wish to build TensorFlow with Google Cloud Platform support*"
send "Y\r"

expect "Do you wish to build TensorFlow with Hadoop File System support*"
send "Y\r"

# Marked as being experimental...
expect "Do you wish to build TensorFlow with the XLA just-in-time compiler*"
send "N\r"

expect "Do you wish to build TensorFlow with VERBS support*"
send "Y\r"

# Requires computecpp tool I've packaged yet
expect "Do you wish to build TensorFlow with OpenCL support*"
if {$withopencl == Yes} {
  send "Y\r"
} else {
  send "N\r"
}

expect "Do you wish to build TensorFlow with CUDA support*"
if {$withcuda == Yes} {
  send "Y\r"
} else {
  send "N\r"
}

if {$withcuda == Yes} {
  expect "Do you want to use clang as CUDA compiler*"
  send "N\r"

  # Enable if you said Yes above
  #expect "Please specify which clang should be used as device and host compiler.*"
  #send "/usr/bin/gcc-4.9\r"

  expect "Please specify the CUDA SDK version you want to use*"
  send "\r"

  expect "Please specify the location where CUDA  toolkit is installed.*"
  send "$cudapath"
  send "\r"

  # Disable this if using clang
  expect "Please specify which gcc should be used by nvcc as the host compiler.*"
  send "/usr/bin/gcc-4.9\r"

  expect "Please specify the cuDNN version you want to use.*"
  send "\r"

  expect "Please specify the location where cuDNN  library is installed.*"
  send "\r"

  expect "Please specify a list of comma-separated Cuda compute capabilities you want to build with.*"
  send "$cudacap"
  send "\r"
}

# Both related to OpenCL support using computecpp
if {$withopencl == Yes} {
  expect "Please specify which C++ compiler should be used as the host C++ compiler.*"
  send "/usr/bin/g++-4.9\r"

  expect "Please specify which C compiler should be used as the host C compiler.*"
  send "/usr/bin/gcc-4.9\r"

  expect "Please specify the location where ComputeCpp for SYCL 1.2 is installed.*"
  send "/opt/computecpp\r"
}

expect "Configuration finished"
