#
# Copyright (c) 2012,2014,2016,2019 LAAS/CNRS
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without   fee is hereby granted, provided   that the above  copyright
# notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS  SOFTWARE INCLUDING ALL  IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR  BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR  ANY DAMAGES WHATSOEVER RESULTING  FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION,   ARISING OUT OF OR IN    CONNECTION WITH THE USE   OR
# PERFORMANCE OF THIS SOFTWARE.
#
#                                            Anthony Mallet on Wed Feb  8 2012
#


# --- usage ----------------------------------------------------------------
#
proc usage { } {
  global argv0
  return [format {usage: %s [-p port] [-v] [--version]} [file tail $argv0]]
}


# --- main -----------------------------------------------------------------
#
proc main {} {
  global argc argv
  global verbose

  # process options
  set verbose 0
  set port 8080
  while {[llength $argv] > 0} {
    set arg [lindex $argv 0]
    if {[regexp -- {(--?.+)=(.+)} $arg m o a]} {
      set arg $o
      set argv [linsert $argv 1 $a]
    }
    set opt [lindex $argv 1]

    switch -glob -- $arg {
      -p - --port {
        if {![string is integer -strict $opt]} {
          error "invalid port number -- $opt"
        }
        set port $opt
        set argv [lrange $argv 1 end]
      }

      -v { incr verbose }
      --version { puts [package present genomixd]; return }
      -h - --help { puts [usage]; return }
      -* { error [format "unknown option -- $arg\n%s" [usage]] }
      * { break }
    }

    set argv [lrange $argv 1 end]
  }

  # process extraneous arguments
  if {[llength $argv] > 0} {
    error [format "too many arguments -- $argv\n%s" [usage]]
  }

  # start server
  infox "accepting connections on port $port"
  server start $port

  # loop forever
  global forever
  vwait forever
  infox "terminating on $forever request"

  foreach m [info commands ::genomix::client::*] {
    set sm [lindex [split $m ::] end]
    infox "unloading $sm"
    rename $m {}
    session::broadcast unload $sm
  }
  session::kill
}


# --- infox ----------------------------------------------------------------
#
proc infox { msg {level 0} } {
  global verbose

  if {$verbose <= $level} return

  global argv0
  set t [clock micro]
  set s [expr $t/1000000]
  set ts [clock format $s -format "%T"]
  set tm [format "%06d" [expr $t-1000000*$s]]
  puts stdout "$ts.$tm:[file tail $argv0]: $msg"
}


# --- warnx ----------------------------------------------------------------
#
proc warnx { msg } {
  global argv0
  set t [clock micro]
  set s [expr $t/1000000]
  set ts [clock format $s -format "%T"]
  set tm [format "%06d" [expr $t-1000000*$s]]
  puts stderr "$ts.$tm:[file tail $argv0]: $msg"
}


# --- bgerror --------------------------------------------------------------
#
proc bgerror { msg } {
  global verbose
  if {$verbose} { warnx $::errorInfo } else { warnx $msg }
}


# --- genomix --------------------------------------------------------------
#
namespace eval genomix {
  namespace export rpath
  namespace export load
  namespace export usage

  # create 'genomix' command
  namespace ensemble create
}
