/****************************************************************************/
/*  DayNight.ppak 10/26/94 Copyright 1994 Atlantis Design Group, Inc.       */
/*  PhonePak ARexx script for Day/Night system - install in REXX:           */
/*                                                                          */
/*  To use this script, set the constants (below) to your particular        */
/*  environment.  Then, add an item to the schedule with the group name set */
/*  to "DayNight".  The date and time should be set to when you want the    */
/*  first transition to occur.  This script will remain active in the       */
/*  schedule forever.                                                       */
/*                                                                          */
/*  When this script is initially scheduled, or upon system reboot, it will */
/*  figure out whether to transition to the day or night system depending   */
/*  on whether the current time is before or after noon.  The night system  */
/*  will be used on weekends.                                               */
/*                                                                          */
/*  This script is designed to change the main setup for each line.         */
/****************************************************************************/

/***************/
/*  Constants  */
/***************/

DaySystem = 'Day'           /* Name of AM system */
NightSystem = 'Night'       /* Name of PM system */
GoToDay = 900               /* Time to go to day system in military HHMM */
GoToNight = 1700            /* Time to go to night system in military HHMM */
NumLines = 2                /* Number of PhonePaks installed */

/****************************************************************************/

TryAgain = FALSE

/**************************************/
/*  Figure out which system to go to  */
/**************************************/

TargetSystem = GetClip(TargetSystem)    /* Set variable to clip value */

if TargetSystem = '' then do            /* First time execution */
    CurrentDay = Date('w')              /* Retrieve day of week */
    if CurrentDay = 'Saturday' | CurrentDay = 'Sunday' then
        TargetSystem = NightSystem
    else do
        CurrentTime = Time('m')             /* Retrieve minutes since midnight */
        if CurrentTime > 719 then
            TargetSystem = NightSystem
        else
            TargetSystem = DaySystem
        Call SetClip(TargetSystem, TargetSystem)
        end
    end


/******************************************/
/*  Attempt to change the system(s) over  */
/******************************************/

do x=1 to NumLines
    CurrentSystem = GetClip(System || x)
    if CurrentSystem ~= TargetSystem then do
        PortName = 'LINEMAN.' || x              /* Assemble the port name */
        if show('P', PortName) then do          /* See if the port exists */
            address value PortName              /* Modify host address */
            'SETLINE 1 SYSNAME=' || TargetSystem
            if rc = 0 then
                Call SetClip(System || x, TargetSystem)
            end
        end
    end
        

/*******************************************************/
/*  Determine if changeover was completely successful  */
/*******************************************************/

do x=1 to NumLines
    CurrentSystem = GetClip(System || x)
    if CurrentSystem ~= TargetSystem then do
        exit "1 'In Transition'"       
    end

/*****************************************************************/
/*  Transition was successful.  Reckon time till next changeover */
/*****************************************************************/
    CurrentDay = Date('w')              /* Retrieve day of week */
    CurrentTime = Time('m')             /* Retrieve minutes since midnight */

if TargetSystem = DaySystem then do
    Call SetClip(TargetSystem, NightSystem)     /* Prepare to go to night */
    
    Hours = GoToNight % 100
    Minutes = GotoNight // 100
    Minutes = Minutes + (Hours * 60)
    Delta = Minutes - CurrentTime
    exit Delta "'Waiting->Night'"
    
    end
else do
    Call SetClip(TargetSystem, DaySystem)       /* Prepare to go to day */

    Hours = GoToDay % 100
    Minutes = GotoDay // 100
    Minutes = Minutes + (Hours * 60)
    Delta = 1440 - CurrentTime + Minutes
    
    if CurrentDay = 'Friday' then               /* Compensate for weekend */
        Delta = Delta + 2880
    else if CurrentDay = 'Saturday' then
        Delta = Delta + 1440
    
    exit Delta "'Waiting->Day'"

    end
