#!/usr/bin/expect -f

# HTTP Expect module for sawdog
# this script requests a HEADer from the server given in the first command
# line argument. If the response contains the string 'HTTP', for example in
# 'HTTP/1.1 200 OK', the script terminates sucessfully.
# If, after 10 seconds, there is no 'HTTP' response, we assume a failure and
# exit with error code 1 
# You can also use something like 'wget --spider' instead of telnet. 

set env(TERM) vt100

set timeout 8
match_max 100000

for {set x 0} {$x<3} {incr x} {
   spawn telnet $argv 10000
   expect "Connection refused" {
      exit 2
   } "Escape character is '^\]'.\r\r" {
      send -- "HEAD / HTTP/1.1\r"
      send -- "Host: $argv\r\r"
      expect "HTTP" {
         exit 0
      }
   } 
   sleep 5
}
exit 1
