# TWAPI example
# Restart the specified Windows service
# Usage example: tclsh restartservice.example SERVICENAME

package require twapi 3.0

if {$argc != 1} {
    puts stderr "Usage: [info nameofexecutable] $argv0 SERVICENAME"
    exit 1
}

# Convert the service name to the internal name
set svcname [twapi::get_service_internal_name [lindex $argv 0]]

# Abort if other running services are dependent on it
array set dependents [twapi::get_dependent_service_status $svcname -active]
if {[array size dependents]} {
    puts stderr "Service not restarted as the following services are dependent on it:"
    puts [join [array names dependents] \n]
    exit 1
}

# Stop the service - wait up to 10 seconds
if {![twapi::stop_service $svcname -wait 10000]} {
    puts stderr "Service could not be stopped"
    exit 1
}

# Start the service - wait up to 30 seconds
if {![twapi::start_service $svcname -wait 30000]} {
    puts stderr "Service could not be started"
    exit 1
}
