#!/usr/bin/expect -f

set env(EDITOR) "./fakeditor.sh"
set env(VISUAL) "./fakeditor.sh"
set crashdir [lindex $argv 0]
spawn abrt-cli report $crashdir

set timeout 60

# timeout handler
proc itstime {args} {
  puts "!! Timeout !!"
  exit 1
}

while {1} {
  expect {
    eof {break}
    timeout { itstime }
    # analyzers
    -re "\n (\[0-9\]).*?GNU Debugger" {
      puts "Got GNU Debbuger"
      set debugger_opt $expect_out(1,string)
      puts ">> $debugger_opt"
    }
    -re "\n (\[0-9\]).*?Retrace Server" {
      puts "Got Retrace Server"
      set retrace_opt $expect_out(1,string)
      puts ">> $retrace_opt"
    }
    "Select analyzer:" {
      send "$debugger_opt\r"
    }
    # debuginfo installation
    "Downloading" {
      send "N\r"
    }
    # collectors
    -re "\n (\[0-9\]).*?Smolt" {
      puts "Got Smolt"
      set smolt_opt $expect_out(1,string)
      puts ">> $smolt_opt"
    }
    "Select collector" {
      send "$smolt_opt\r"
    }
    # reporters
    -re "\n (\[0-9\]*)\\) Bugzilla" {
      puts "Got Bugzilla idx:$expect_out(1,string)"
      set bugzilla_opt $expect_out(1,string)
    }
    -re "\n (\[0-9\]*)\\) Mailx" {
      puts "Got Mailx idx:$expect_out(1,string)"
      set mailx_opt $expect_out(1,string)
    }
    -re "\n (\[0-9\]*)\\) Logger" {
      puts "Got Logger idx:$expect_out(1,string)"
      set logger_opt $expect_out(1,string)
    }
    -re "\n (\[0-9\]*)\\) New Red Hat Support case" {
      puts "Got RHCS idx:$expect_out(1,string)"
      set rhcs_opt $expect_out(1,string)
    }
    -re "\n (\[0-9\]*)\\) Save to tar archive" {
      set tar_opt $expect_out(1,string)
      puts "Got 'Save tar' idx:$expect_out(1,string)"
    }
    "Select reporter" {
      send "$tar_opt\r"
    }
    "Log File" {
      puts "Setting log file to /tmp/abrt.log"
      send "/tmp/abrt.log\r"
      break
    }
  }
}
wait
