!----------------------------------------------------------------------------
!     HOLDAY.BAS - Lets user know how many days till user defined holiday
!----------------------------------------------------------------------------
! Edit History:
!
!.100  04/15/88        Original Version        Stephen Caldwell (CALD/AM)
!                                              (213) 947-3771
!----------------------------------------------------------------------------
! NOTES:
!
! A regular VUE type file must be in the account defined in the "HFILE" map
! statement and contain the following information:
!
! 053088               <-- Date of holiday (must use leading zeros).
! Memorial Day         <-- Holiday name (up to fifty characters).
! Happy Memorial Day   <-- Message that is to be output on the holiday.
!
! "ROW" and "COL" have to be given initial values for proper message output.
!
!  The variables that can have there initial values changed are marked
!  with an "*".
!
!----------------------------------------------------------------------------
program HOLDAY,1.0A(100)

map1 BINDATE,B,4,DATE
map1 FILLDATE,@BINDATE
      map2 MM,B,1
      map2 DD,B,1
      map2 YY,B,1

map1 HDATE,S,6                                 ! Holiday date.
map1 HNAME,S,50                                ! Holiday name.
map1 HMSG,S,70                                 ! Holiday message.
map1 OUTMSG,S,70                               ! Output message.
map1 HFILE,S,21,"DSK0:HOLDAY.DAT[20,0]"        ! Data file + location.*
map1 ROW,F,6,14                                ! Row for message display.*
map1 COL,F,6,20                                ! Column for message display.*
map1 ERRMSG,S,70                               ! Error output message.
map1 ERRFLG,F,6                                ! Error flag.

!-------------------------------Program Area---------------------------------

START: lookup HFILE,EXISTS                     ! Is the file here?

      if EXISTS=0 goto NO'FILE                ! No. Process error.

      open #1,HFILE,input                     ! Open it up.

      input line #1,HDATE                     ! Get the holiday date
      if eof(1) goto FILE'ERR                 ! Everything O.K. ?

      input line #1,HNAME                     ! Get the holiday name
      if eof(1) goto FILE'ERR                 ! Everything O.K. ?

      input line #1,HMSG                      ! Get the holiday message
      if eof(1) goto FILE'ERR                 ! Everything O.K. ?

      MO=HDATE[1,2]                           ! Put the holiday date
      DA=HDATE[3,4]                           !  into the variables
      YR=HDATE[5,6]                           !   that JULDAT expects.

      call CHK'DATE                           ! Check the date format.

      if ERRFLG goto DSPERR                   ! Format error?

      call JULDAT                             ! No, convert date to Julian.

      N=JD                                    ! Save Julian date in "N"
      MO=MM                                   ! Put todays date
      DA=DD                                   !  into the variables
      YR=YY                                   !   that JULDAT expects.

      call JULDAT                             ! Convert date to Julian.

      CDAYS=(N-JD)                            ! Subtract todays date from
                                              ! holiday date.
      if CDAYS=0 goto HOLIDAY'TODAY           ! Is it today?
      if CDAYS<0 goto EXIT                    ! Is it past?
      if CDAYS=1 then &
              D$="day" &
      else &
              D$="days"                       ! Must be in the future.

      OUTMSG="Only "+str(CDAYS)+" more "+ &
              D$+" till "+HNAME               ! Define output message.


      goto MSGOUT                             ! We're outta here.

!--------------------------------
! Today is the target holiday.
!--------------------------------
HOLIDAY'TODAY:

      OUTMSG=HMSG

      goto MSGOUT

!------------------------
! Our file is missing.
!------------------------
NO'FILE:

      ERRMSG="?"+HFILE+" not found in HOLDAY.RUN"

      goto DSPERR

!----------------------------------------------------
! File less than three lines. Create error message.
!----------------------------------------------------
FILE'ERR:

      ERRMSG="?Format error in "+HFILE

      goto DSPERR

!-----------------------------
! Display any error message.
!-----------------------------
DSPERR:
      ? chr(7);

      OUTMSG=ERRMSG

      goto MSGOUT                             ! Set cursor position.

!----------------------------------------------
! Position the cursor and clear to end of line.
!----------------------------------------------
MSGOUT:
      COL=int(40-len(OUTMSG)/2)               ! Calculate starting column
                                              ! to center the message.
      ? tab(ROW,COL);tab(-1,9);OUTMSG         ! Display output message.

      goto EXIT                               ! All done!

!-------------------------------
! Check for valid date format.
!-------------------------------
CHK'DATE:

      ERRMSG=chr(0)

      C=instr(1,HDATE,"/")

      if C>0 then &
              ERRMSG="?Invalid date format do not use slashes"

      if len(HDATE)<6 and C=0 then &
              ERRMSG="?Invalid date format use leading zeros."

      if val(MO)<1 or val(MO)>12 and len(ERRMSG)=0 then &
              ERRMSG="?Invalid Month"

      if val(DA)<1 or val(DA)>31 and len(ERRMSG)=0 then &
              ERRMSG="?Invalid Day"

      ERRFLG=len(ERRMSG)                      ! Error flag > 0 if error.

      return

!-----------------------------------
! Calculate the Julian date.
!-----------------------------------
JULDAT:
      YR=(1900+YR)
      MO=(MO-2)

      if MO<1 then &
              call CVTMNY                     ! Convert month & year

      JD=int(YR*1461/4)+int(MO*367/12)+DA+1721075

      return

CVTMNY:
      MO=MO+12
      YR=YR-1

      return

!------------------
! End of program
!------------------
EXIT:  if eof(1)=>0 then &
              close #1

      end