# rundiskcache                                                    -*-Tcl-*-
#
# Does initializations for the DiskCache Manager.
#
# Copyright (c) 2007-2026 Electric Cloud, Inc.
# All rights reserved
#-----------------------------------------------------------------------------

set ::signalled ""

# parse options
array set ::opts {
    logfile                             /var/log/diskcache.log
    numagents                           1
    pidfile                             /var/run/diskcache/pid
    portfile                            /var/run/diskcache/port
    tempdir                             /tmp
}

foreach {key val} $argv {
    set key [string range $key 1 end]
    set ::opts($key) $val
}

# Tell the diskcache manager to use the specified directory, or the system
# temp directory if one wasn't specified.

if {$::opts(tempdir) == ""} {
    set ::opts(tempdir) [diskcacheman systemtempdir]
}
diskcacheman directory $::opts(tempdir)

# Reattach the logfile.
diskcacheman logfile $::opts(logfile)

catch {close stdout}
open [diskcacheman logfile] {WRONLY APPEND CREAT}    ; # new stdout
fconfigure stdout -buffering line

catch {close stderr}
open [diskcacheman logfile] {WRONLY APPEND CREAT}    ; # new stderr
fconfigure stderr -buffering line

# print a header

set date [clock format [clock seconds] -format {%a %b %d %Y %H:%M:%S}]
puts stdout "\n"
puts stdout "-----------------------------------------------------------------"
puts stdout "DiskCache Manager started at [format %s $date]"
puts stdout "\t\tlogfile:      $::opts(logfile)"
puts stdout "\t\tnumagents:    $::opts(numagents)"
puts stdout "\t\tportfile:     $::opts(portfile)"
puts stdout "\t\ttempdir:      [diskcacheman directory]"

if {$::tcl_platform(platform) != "windows"} {
    puts stdout "\t\tpidfile:      $::opts(pidfile)"
}

puts stdout "-----------------------------------------------------------------"
puts stdout "\n"

# Only log info level messages by default.

diskcacheman loglevel error

# Start the diskcache manager, with as many worker threads as there are
# agents on the machine.

diskcacheman reset 3000 $::opts(numagents)

# Write the port information for our server so the agent knows where to
# connect.

set fd ""
if {[info exists ::env(DISKCACHE_MANAGER_PORTFILE)]} {
    set fd [open $::env(DISKCACHE_MANAGER_PORTFILE) w]
} else {
    catch {file mkdir [file dirname $::opts(portfile)]}
    set fd [open $::opts(portfile) w]
}
puts $fd [diskcacheman port]
puts $fd [diskcacheman tclport]
close $fd

# write PID file so that Unix can know which process to kill when the service
# is stopped.  On windows, we also do it so that we can kill the diskcache
# server in testing.

if {$::tcl_platform(platform) != "windows"} {
    catch {file mkdir [file dirname $::opts(pidfile)]}
    set fd [open $::opts(pidfile) w]
    puts -nonewline $fd [pid]
    close $fd
}


# Set up a dummy socket so that Tcl thinks there is an event in the event
# loop so that we can vwait for a signal below.

proc dummyServer {sock args} { close $sock }
set ::dummySocket [socket -server dummyServer 0]

# $::signalled becomes nonempty when the first terminal signal arrives.
# It is not affected by other kinds of signals.

update
if { $::signalled == "" } {
    vwait ::signalled
}

set shutdownDate [clock format [clock seconds] -format {%a %b %d %Y %H:%M:%S}]
puts stdout ""
puts stdout "-----------------------------------------------------------------"
puts stdout "$shutdownDate Received signal '$::signalled', shutting down..."
puts stdout "-----------------------------------------------------------------"
puts stdout ""

close $::dummySocket
file delete -force $::opts(pidfile)
file delete -force $::opts(portfile)


# EOF

