#!/bin/sh
# ALL the continued lines following this one are interpreted
# by the bourne shell and ignored by Tcl, which picks up
# execution after the exec line.\
exec tclsh "$0" ${1+"$@"}

set ::HOST localhost

#--------------------------------------------------------------------
# exit codes
#--------------------------------------------------------------------

set ::exit_code(OK)           0
set ::exit_code(USAGE)        1
set ::exit_code(NETWORKERROR) 2

#--------------------------------------------------------------------
# Explain proper use of the command
#--------------------------------------------------------------------
proc usage { } {
    puts stderr "\nUsage: $::argv0 <port>\n"
    puts stderr "     <port> - The port id of the ldas manager."
    puts stderr "              (port must be an integer)."
    exit $::exit_code(USAGE)
}

#--------------------------------------------------------------------
# Ensure that at least one option has been specified and that it is an
#   integer (port address)
#--------------------------------------------------------------------

if { ( [ llength $::argv ] != 1 ) || \
     ( ! [ string is integer [ lindex $::argv 0 ] ] ) } {
	usage
}

set ::PORT [ lindex $::argv 0 ]

#--------------------------------------------------------------------
# Establish the connection
#--------------------------------------------------------------------
if { [ catch {
   set sid [ socket $::HOST $::PORT ]
} err ] } {
  set msg    "\nUnable to establish a connection to the\n"
  append msg "manager on host [ info hostname ]\n"
  append msg "using port: $::PORT\n\n"
  append msg "error caught: '$err'"
  puts stderr $msg
  exit $::exit_code(NETWORKERROR)
}
close $sid ;# Close down the port so this script does not
           ;# become a denial of service!

#--------------------------------------------------------------------
# Gather information about who is shutting down the system
#--------------------------------------------------------------------
puts -nonewline stdout "\nLDAS User: "
flush stdout
gets stdin tmp
set ::LDAS_USER            [ string trim $tmp ]
puts -nonewline stdout "LDAS User password (will not be echoed): "
flush stdout
exec stty -echo ;# Disable echoing to the controlling tty
gets stdin tmp
set ::LDAS_USER_PASSWORD   [ string trim $tmp ]
exec stty echo  ;# Enable echoing to the controlling tty
puts stdout {}
puts -nonewline stdout "LDAS System password (will not be echoed): "
flush stdout
exec stty -echo ;# Disable echoing to the controlling tty
gets stdin tmp
set ::LDAS_SYSTEM_PASSWORD [ string trim $tmp ]
exec stty echo  ;# Enable echoing to the controlling tty
puts stdout {}

#--------------------------------------------------------------------
# Establish the connection now that all information has been gathered
#--------------------------------------------------------------------
if { [ catch {
   set sid [ socket $::HOST $::PORT ]
} err ] } {
  set msg    "\nUnable to establish a connection to the\n"
  append msg "manager on host [ info hostname ]\n"
  append msg "using port: $::PORT\n\n"
  append msg "error caught: '$err'"
  puts stderr $msg
  exit $::exit_code(NETWORKERROR)
}

#--------------------------------------------------------------------
# Service the request
#--------------------------------------------------------------------
set cmd    "$::LDAS_SYSTEM_PASSWORD "
append cmd "$::LDAS_USER "
append cmd "$::LDAS_USER_PASSWORD "
append cmd "mgr::sHuTdOwN"
puts $sid $cmd
flush $sid

while { [ eof $sid ] == 0 } {
  gets $sid results
  if { [ string length $results ] > 0 } {
    regsub -all -- $::LDAS_SYSTEM_PASSWORD $results ****** results
    regsub -all -- $::LDAS_USER_PASSWORD   $results ****** results
    puts "Result: $results"
  }
}
close $sid

