#!/usr/local/bin/tops -i -s /usr/local/tops/sys -u /opt/mytops/usr/
/* GNU General Public License

Program Tops - a stack-based computing environment
Copyright (C) 1999-2005  Dale R. Williamson

Author: Dale R. Williamson <dale.williamson@prodigy.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

------------------------------------------------------------------------

  File: dserv1  November 2005
  Copyright (C) 2005  D. R. Williamson

  This script runs tops as a daemon server, listening on a port.

  The daemon runs in /tmp (see daemon_init(), term.c).

  Log file is /tmp/YYYxxx_dserv1.log where xxx=PID.  If command line
  switch -logsave is present, the log file will not be deleted when
  the server exits.

  Command line switch -ntrace adds network trace output to the log:
     % dserv1 -ntrace

  The default port is 9877.  Another port to use can be specified with
  command line switch -port:
     % dserv1 -port 9880

  The default script runs for 12 hours, or as controlled by command
  line switch -exit; this limits script running to 100 seconds:
     % dserv1 -exit 100

  A file to source at start up can be specified by command line switch
  -source:
     % dserv1 -exit 100 -source collect.v -port 9880

----------------------------------------------------------------------*/

#  Logging:
      if(chars(argv("-logsave")))
       # LOG is a file in /tmp that will not be deleted upon exit:
         set_sysout((LOG="/tmp/"+host+runid1+"_dserv1.log"));
      else 
       # LOG is a file in /tmp that will be deleted upon exit:
         set_sysout((LOG="/tmp/"+runid+"_dserv1.log"));

#  Display first lines in the log file:
      nl(dot(nl(cats("-",72))));
      nl(dot(" PID " + spaced(intstr(getpid)) + date));

#  Command line switch -ntrace logs network trace output:
      if(argv("-ntrace") != "") ntrace();

#  Port (default def_port=9877):
      (PORT,f) = (def_port,number(argv("-port")));

#  End of life alarm (default is 12 hours):
      (EOL,f) = (12*3600,number(argv("-exit")));
      ALARM(EOL,"exit");

#  Source file:
      FILE=strchop(argv("-source"));
      if(chars(FILE)) (filefound(FILE), << IF source THEN >>);

#  Running DSERVER, listening until EOL;
      DSERVER("*",PORT);
