#!/usr/bin/tcl
#
# Bounces an empty server.  
#
# Usage: bounceEmptyServer <mod>
#        e.g. "bounceEmptyServer tfc"
#
if [lempty $argv] {
	echo "Usage: bounceEmptyServer <mod>"
	exit 1
}

# Hard-code these, if you like.
set mod $argv
set hldir $env(HLDIR)

set pidfile $hldir/$mod/hlds_l.pid
if ![file exists $pidfile] {
	echo "hlds_run for $mod isn't running."
	exit 1
}

if [catch {
	set userInfo [exec hlcmd -g $mod users | grep "users\$"]
	set userCnt  [lindex $userInfo 0]
} err] {
	echo "Error determining # of users: $err"
	exit 1
}

if { $userCnt == 0 } {
	if [catch { read_file $hldir/$mod/hlds_l.pid } pid] {
		echo "Error getting PID of hlds_run: $pid"
		exit 1
	}

	if [catch { kill 9 $pid } err] {
		echo "Error killing hlds_run on pid $pid: $err"
		exit 1
	}

	echo "hlds_run on pid $pid killed."
} else {
	echo "$userCnt users, not killing hlds_run"
}

exit 0
