set prompt "% "
set stty_init {-onlcr -opost intr \^C kill \^U}
set kadmin_local $KADMIN_LOCAL

# Backward compatibility until we're using expect 5 everywhere
if {[info exists exp_version_4]} {
	global wait_error_index wait_errno_index wait_status_index
	set wait_error_index 0
	set wait_errno_index 1
	set wait_status_index 1
} else {
	set wait_error_index 2
	set wait_errno_index 3
	set wait_status_index 3
}

if { [string length $VALGRIND] } {
    rename spawn valgrind_aux_spawn
    proc spawn { args } {
	global VALGRIND
	upvar 1 spawn_id spawn_id
	set newargs {}
	set inflags 1
	set eatnext 0
	foreach arg $args {
	    if { $arg == "-ignore" \
		     || $arg == "-open" \
		     || $arg == "-leaveopen" } {
		lappend newargs $arg
		set eatnext 1
		continue
	    }
	    if [string match "-*" $arg] {
		lappend newargs $arg
		continue
	    }
	    if { $eatnext } {
		set eatnext 0
		lappend newargs $arg
		continue
	    }
	    if { $inflags } {
		set inflags 0
		# Only run valgrind for local programs, not
		# system ones.
#&&![string match "/bin/sh" $arg] sh is used to start kadmind!
		if [string match "/" [string index $arg 0]]&&![string match "/bin/ls" $arg]&&![regexp {/kshd$} $arg] {
		    set newargs [concat $newargs $VALGRIND]
		}
	    }
	    lappend newargs $arg
	}
	set pid [eval valgrind_aux_spawn $newargs]
	return $pid
    }
}

# Hack around Solaris 9 kernel race condition that causes last output
# from a pty to get dropped.
if { $PRIOCNTL_HACK } {
    catch {exec priocntl -s -c FX -m 30 -p 30 -i pid [getpid]}
    rename spawn oldspawn
    proc spawn { args } {
	upvar 1 spawn_id spawn_id
	set newargs {}
	set inflags 1
	set eatnext 0
	foreach arg $args {
	    if { $arg == "-ignore" \
		     || $arg == "-open" \
		     || $arg == "-leaveopen" } {
		lappend newargs $arg
		set eatnext 1
		continue
	    }
	    if [string match "-*" $arg] {
		lappend newargs $arg
		continue
	    }
	    if { $eatnext } {
		set eatnext 0
		lappend newargs $arg
		continue
	    }
	    if { $inflags } {
		set inflags 0
		set newargs [concat $newargs {priocntl -e -c FX -p 0}]
	    }
	    lappend newargs $arg
	}
	set pid [eval oldspawn $newargs]
	return $pid
    }
}

# Variables for keeping track of api process state
set api_pid "0"

proc api_exit {} {
	global spawn_id
        global api_pid

#	puts stdout "Starting api_exit (spawn_id $spawn_id)."
	catch {close} errMsg
        catch {wait} errMsg
#       puts stdout "Finishing api_exit for $api_pid."
        set api_pid "0"
}

proc api_isrunning {pid} {
        global api_pid
    
#        puts stdout "testing $pid, api_pid is $api_pid"
        if {$pid == $api_pid} {
	    return 1;
	} else {
	    return 0;
	}
}

proc api_version {} {
}

proc api_start {} {
	global API 
	global env
	global spawn_id
	global prompt
        global api_pid

	set pid [spawn $API]
	expect {
		-re "$prompt$" {}
		eof { perror "EOF starting API" }
		timeout { perror "Timeout starting API" }
	}
	if {! [info exists env(TCLUTIL)]} {
		perror "TCLUTIL environment variable isn't set"
	}
	# tcl 8.4 for some reason screws up autodetection of output
	# EOL translation.  Work around it for now.
	send "if { \[info commands fconfigure\] ne \"\" } { fconfigure stdout -translation lf }\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF starting API" }
		timeout { perror "Timeout starting API" }
	}
	send "source $env(TCLUTIL)\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF starting API" }
		timeout { perror "Timeout starting API" }
	}
	send "set current_struct_version \[expr \$KADM5_STRUCT_VERSION &~ \$KADM5_STRUCT_VERSION_MASK\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set current_api_version \[expr \$KADM5_API_VERSION_3 &~ \$KADM5_API_VERSION_MASK\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set bad_struct_version_mask \[expr 0x65432100 | \$current_struct_version\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set bad_api_version_mask \[expr 0x65432100 | \$current_api_version\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set no_api_version_mask \$current_api_version\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set no_struct_version_mask \$current_struct_version\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set old_api_version \[expr \$KADM5_API_VERSION_MASK | 0x00\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set old_struct_version \[expr \$KADM5_STRUCT_VERSION_MASK | 0x00\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set new_api_version \[expr \$KADM5_API_VERSION_MASK | 0xca\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}
	send "set new_struct_version \[expr \$KADM5_STRUCT_VERSION_MASK | 0xca\]\n"
	expect {
		-re "$prompt$" {}
		eof { perror "EOF setting API varibles"}
		timeout { perror "timeout setting API varibles"}
	}

	set api_pid $pid
#	puts stdout "Finishing api_start (spawn_id $spawn_id, pid $api_pid)."
	return $pid
}
api_start

