#!/usr/local/bin/perl


# MicroDowell SpA Community License
# 
# Preamble:
# 
# The intent of this document is to state the conditions under which the 
# Package (ups.pl) may be copied and distributed, such that MicroDowell SpA 
# maintains control over the development and distribution of the Package, 
# while allowing the users of the Package to use the Package in a variety of 
# ways.  
# 
# Authorized electronic distributors of the Package are:
# 
#   * Any MicroDowell SpA site
# 
# Definitions:
# 
# "MicroDowell" refers to MicroDowell SpA., the Copyright Holder of the 
# Package.
# 
# "Package" refers to those files, including, but not limited to, source 
# code, binary executables, images, and scripts, which are distributed by the 
# Copyright Holder.
# 
# "You" is you, if you are thinking about copying or distributing this 
# Package.
# 
# Terms:
# 
# 1. You may use this Package for commercial or non-commercial purposes 
# without charge.
# 
# 2. You may make and give away verbatim copies of this Package for personal 
# use, or for use within your organization, provided that you duplicate all 
# of the original copyright notices and associated disclaimers.  You may not 
# distribute copies of this Package, or copies of packages derived from this 
# Package, to others outside your organization without specific prior written 
# permission from MicroDowell (although you are encouraged to direct them to 
# sources from which they may obtain it for themselves).
# 
# 3. You may apply bug fixes, portability fixes, and other modifications 
# derived from MicroDowell.  A Package modified in such a way shall still be 
# covered by the terms of this license.
# 
# 4. MicroDowell's name and trademarks may not be used to endorse or promote 
# packages derived from this Package without specific prior written 
# permission from ActiveState.
# 
# 5. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED 
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
# 
# ==============================================================================

use MyGlobals qw(UPSInit HandleSocket HandleUDP HandleSchedule PollUPS ParseCommandLine CloseSockets 
					  $INTERACTIVE $ups $connection $OS_win HandleExit WriteEventToLog) ;

my %iniTable; 
my $iniFile = "enterprise.ini";
my $ProgPath ;
use constant eveSTOP => 1 ;

#-----------------------------------------------------------------------------------
# Debug management
#-----------------------------------------------------------------------------------
sub prefix 
{
  my $now = localtime;

  join "", map { "[$now] [${$}] $_\n" } split /\n/, join "", @_;
}
   $SIG{__WARN__} = sub { warn prefix @_ };
   $SIG{__DIE__} = sub { die prefix @_ };
   setup_signals();

#-----------------------------------------------------------------------------------
#                               Program start
#-----------------------------------------------------------------------------------
	$INTERACTIVE = 0 ;
	ParseCommandLine() ;
	$_ = $0 ;
	s/upseye\.pl/enterprise\.ini/g ;
	$iniFile = $_ ;
	s/enterprise\.ini//g ;
	$ProgPath = $_ ;
	UPSInit($iniFile, $ProgPath) ; 
           
   while (1)   		# repeat this cicle forever
   	{
      HandleSocket() ;
      HandleUDP() ;
      HandleSchedule() ;
      PollUPS() ;
   	}
   exit(0) ;   # exit from program

#-----------------------------------------------------------------------------------
# SIG handler
#-----------------------------------------------------------------------------------
sub setup_signals 
{
   if (not $OS_win)
      {
      setpgrp;                                              # I *am* the leader
      $SIG{HUP} = $SIG{QUIT} = $SIG{INT} = $SIG{TERM} = $SIG{KILL} = $SIG{STOP} = sub {
		WriteEventToLog (eveSTOP, 0) ;
      sleep (2) ;
      my $sig = shift;
      undef $ups ;
   	CloseSockets () ;
      $SIG{$sig} = 'IGNORE';
      kill $sig, 0;                                         # death to all-comers
      die "UPSEye: killed by $sig\n";
      };
   }
}
