#!/usr/bin/sh

VRSN="[2.25]"
# Truncated Julian Date / Calendar Date Script
# 01/08/1997 acs 1.00
#   if 1 arg is supplied; converts JD to MM DD YYYY
#   if 3 args are supplied converts MM DD YYYY to Julian Date
#   if no args are supplied converts current date to Julian Date
#   if -w arg supplied return weekday instead (0 - Sun; 6 - Sat)
# 10/12/2001 acs 1.01
#   added -n & -p args to add or subtract days before outputting
#   added -s arg to remove spaces from MM DD YYYY for better filename
#   generation; trap now removes tmp awk files
# 10/20/2001 acs 1.02
#   corrected stupid integer arithmetic in cal_jdate
# 10/21/2001 acs 1.1
#   added -e argument for input/output in DD MM YYYY format and 
#   -y argument for input/output in YYYY MM DD format; added -U argument
#   to compute current date in UTC rather than local time
# 10/22/2001 acs 2.0  added -x arguments to exclude weekdays
# 10/29/2001 acs 2.01 added -h argument to skip past holidays
# 11/14/2001 acs 2.02 added -D -M, & -Y arguments to output day, month, & year
# 11/15/2001 acs 2.03 -D, -M, & -Y arguments now supported in case 3 input 
#                     args this is useful if skipping forwards or backwards
# 12/27/2001 acs 2.04 looked for Holidays_2001 before Holidays
# 02/06/2002 acs 2.05 added -S sep argument to allow custom separators for MM
#                     DD YYYY output
# 05/30/2002 acs 2.06 minor improvements; e.g. changed `cmd` to $(cmd);
#                     replaced awk function INT() with builtin int();
#                     used 'nawk' rather than 'awk' if available
# 06/26/2002 acs 2.1  added the -c argument to allow 12/31/2001 to be
#                     processed; replaced if [ ... ] with if [[ .... ]]
#                     to use the shell's internal test; added Julian Day
#                     explanation to usage() to match the Perl version;
#                     added -i and -o options so that months could be 
#                     input and output in 'Jan','Feb', ... format or 
#                     weekdays could be output in 'Sun','Mon', ... format;
#                     the - I and -O options do the same but now full
#                     names are used. e.g. 'January','February', ... and
#                     'Sunday','Monday' ...
#                     For example, '04-July-1776' could be parsed as
#                     valid input with the '-c -e -I -S "-"' options;
#                     NLS is provied with -i,-o,-I, and -O options;
#                     examples added to usage(). 
# 08/04/2003 acs 2.11 minimized use of awk; replaced 3 awk scripts with
#                     shell functions; only build awk files if actually
#                     needed; no temp files needed so no trap needed to
#                     remove temp files; smarter evaluation of awk/nawk;
#                     considerably faster execution
# 09/05/2003 acs 2.2  added -f option for file input; TDIR not needed
# 09/22/2003 acs 2.21 added -N option to output the occurrence of a given
#                     weekday within the month
# 10/01/2003 acs 2.22 added -C option to convert 2-digit years
# 03/25/2004 acs 2.23 fixed bug introduced in 2.22; BCE years were not
#                     displaying the "-" sign. 
# 04/12/2005 acs 2.24 minor change; a few usage messages were going to stdout
#                     rather than stderr -- no functional changes
#
# 11/02/2005 acs 2.25 changed "typeset -i10 X=1" to "typeset -iX=1"; not all
#                     shells understand the radix syntax; no functional
#                     changes
#
# The Julian Day to Gregorian conversion routines are based upon algorithms
# developed by Henry F. Fliegel and Thomas C. Van Flandern and from sources 
# published in Sky & Telescope magazine
#
#

typeset -i PID=${$}
typeset HOLIDAY_FILE="/etc/acct/holidays"
typeset PROG=${0##*/}
typeset -i DEFAULT_CUTOFFYR_WINDOW=50 # window size for 2-digit year
                                      # conversions

readonly PID VRSN HOLIDAY_FILE PROG DEFAULT_CUTOFFYR_WINDOW

cal_jdate() # expects 3 args: month, day, year; outputs truncated Julian Day
{
  typeset -i MO=${1}
  typeset -i DA=${2}
  typeset -i YR=${3}
  shift 3
  typeset -i JD=0
  JD=$((${DA} - 32075 + (1461 * (${YR} + 4800 + ((${MO} - 14)/12)) / 4) + \
         ((367 * (${MO} - 2 - ((${MO} - 14)/12) * 12))/12) - \
         ((3 * ((${YR} + 4900 + ((${MO} - 14)/12))/100))/4)))
  echo "${JD}"
  return 0
} # cal_jdate

# jdate_cal() expects 4 args
# Arg#   Value
#   1 -- Julian Day
#   2 -- Separator -- remember to quote if "<space>"
#   3 -- 0 output MM DD YYYY
#     -- 1 output DD MM YYYY
#     -- 2 output YYYY MM DD
#   4 -- 0 output 4-digit years
#     -- 1 output 2-digit years
#
jdate_cal()
{
  typeset -i JD=${1}
  typeset SEP="${2}"
  typeset -i MDY_MODE=${3}
  typeset -i TWO_DIGIT_MD=${4}
  shift 4
  typeset -i L=$((${JD} + 68569))
  typeset -i N=$(((4 * ${L}) / 146097))
  typeset -i LL=$(((146097 * ${N} + 3) / 4))
  ((L -= ${LL}))
  typeset -i I=$(((4000 * (${L} + 1)) / 1461001))
  LL=$(((1461 * ${I}) / 4))
  L=$((${L} - ${LL} + 31))
  typeset -i J=$(((80 * ${L}) / 2447))
  typeset -Z2 DA=$((${L} - ((2447 * ${J}) / 80)))
  L=$((${J} / 11))
  typeset -Z2 MO=$((${J} + 2 - (12 * ${L})))
  typeset -i YR=$((100 * (${N} - 49) + ${I} + ${L}))
  if [[ ${TWO_DIGIT_MD} -eq 0 ]]
    then
      if [[ ${YR} -ge 0 ]]
        then
          typeset -Z4 YR_OUT=${YR}
        else
          typeset -Z5 YR_OUT=${YR}
        fi
    else
      if [[ ${YR} -ge 0 ]]
        then
          typeset -Z2 YR_OUT=$((${YR} % 100))
        else
          typeset -Z3 YR_OUT=$((${YR} % 100))
        fi
    fi 
  case ${MDY_MODE} in
      2) echo "${YR_OUT}${SEP}${MO}${SEP}${DA}"   
         ;;
      1) echo "${DA}${SEP}${MO}${SEP}${YR_OUT}"
         ;;
    0|*) echo "${MO}${SEP}${DA}${SEP}${YR_OUT}"
         ;;
  esac   
  return 0 
} # jdate_cal


wkday() # expects Julian Day; outputs day of week; 0 = Sun, 6 = Sat  
{
  typeset -i X=${1}
  shift
  typeset -i WD=$(((${X} + 1) % 7))
  echo "${WD}"
  return 0  
} # wkday

which_wkday() # expects Julian Day; returns 1 if first occurrence of weekday
              # within the month; 2 if the second occurrence;
              # 3 if the third, ...
{
  typeset -i CURRJD=${1}
  shift
  typeset -i CURRMO=0
  typeset -i DUMMY1=0
  typeset -i DUMMY2=0
  jdate_cal "${CURRJD}" " " 0 0 | read CURRMO DUMMY1 DUMMY2
  typeset -i TESTMO=${CURRMO}
  typeset -i WKKNT=0
  while [[ ${TESTMO} -eq ${CURRMO} ]]
    do
      ((CURRJD -= 7))
      jdate_cal "${CURRJD}" " " 0 0 | read TESTMO DUMMY1 DUMMY2
      ((WKKNT += 1))
    done
  echo "${WKKNT}"
  return 0
} # which_wkday

# -----------------------------------------------------------------

typeset -i CUTOFF=-1

century_year()
{
  typeset -i YR=${1}
  typeset Y_UTC_ARG=${2}
  typeset -i TWO_DIGIT_MD=${3}

  shift 3
  if [[ ${TWO_DIGIT_MD} -ne 0 ]]
    then
      if [[ ${CUTOFF} -lt 0 ]]
        then
          typeset -i YRCUTOFF=${CUTOFFYEAR:-'-1'}
          if [[ ${YRCUTOFF} -lt 0 ]]
            then 
              CUTOFF=$(($(date ${Y_UTC_ARG} '+%Y') - \
                        ${DEFAULT_CUTOFFYR_WINDOW}))
            else
              CUTOFF=${YRCUTOFF}
            fi
        fi
      if [[ ${YR} -ge 0 && ${YR} -le 99 ]]
        then
          typeset -i HINGE=$((${CUTOFF} % 100))
          if [[ ${YR} -lt ${HINGE} ]]
            then
              ((YR += 100))
            fi
          YR=$((((${CUTOFF} / 100) * 100) + ${YR}))
        fi
    fi
  echo "${YR}"
  return 0
} # century_year


# -----------------------------------------------------------------
# If nawk is in PATH use it rather than awk (oawk).
# function does the evaluation only first time
#
typeset AWKEXE=""

AWK()
{
  if [[ -z "${AWKEXE}" ]]
    then
      typeset DUMMY=""
      typeset -i ASTAT=0
      DUMMY=$(type nawk 2>/dev/null)
      ASTAT=${?}
      if [[ ${ASTAT} -eq 0 ]]
        then
          AWKEXE=nawk
        else
          AWKEXE=awk
        fi
      readonly AWKEXE
    fi
  echo "${AWKEXE}"
  return 0
} # AWK


build_A4() # builds awk script 'on the fly'
{
  echo "function IS_REGULAR_FILE(fname)"
  echo "{"
  echo "  XX_s_cmd = sprintf(\"test -f %s -a -r %s\",fname,fname)"
  echo "  return(system(XX_s_cmd))"
  echo "} # IS_REGULAR_FILE"
  echo ""
  echo "function LOAD_ARRAY(fname,xarry,     knt)"
  echo "{"
  echo "  knt = -1"
  echo "  if (IS_REGULAR_FILE(fname) == 0)"
  echo "    {"
  echo "      while ((getline  < fname) > 0)"
  echo "        {"
  echo "          if (!(\$0 ~ /^\*/))"
  echo "            {"
  echo "              if (NF > 0)"
  echo "                {"
  echo "                  ++knt"
  echo "                  xarry[knt] = \$1"
  echo "                }"
  echo "            }"
  echo "        }"
  echo "    }"
  echo "  return(knt)"
  echo "} # LOAD_ARRAY"
  echo ""
  echo "BEGIN {"
  echo "  fnd = 0"
  echo "  cnt = LOAD_ARRAY(hol_fname,arry1)"
  echo "  if (cnt > 0)"
  echo "    {"
  echo "      if (arry1[0] == year)"
  echo "        {"
  echo "          i = 1"
  echo "          while (i <= cnt && fnd == 0)"
  echo "            {"
  echo "              if (arry1[i] == target_day)"
  echo "                {"
  echo "                  fnd = 1"
  echo "                }"
  echo "              else"
  echo "                {"
  echo "                  ++i"
  echo "                }"
  echo "            }"
  echo "        }"
  echo "      else"
  echo "        {"
  echo "#         Year entry in file does not match; output to stderr"
  echo "          s_err = sprintf\c"
  echo "(\"File %s year %d does not match expected year %d.\","
  echo "                          hol_fname,arry1[0],year)"
  echo "          s_cmd = sprintf(\"echo \\\"%s\\\" >&2\",s_err)"
  echo "          system(s_cmd)"
  echo "        }"
  echo "    }"
  echo "  exit(fnd)"
  echo "}"
  return 0
} # build_A4


# mode values:  0 - abbreviated weekday names
#               1 - full weekday names
#               2 - abbreviated month names
#               3 - full month names
# used by ${A5} script

build_A5() # builds awk script 'on the fly'
{
  echo "BEGIN {"
  echo "knt = -1"
  echo "while ((knt < 4) && \"locale LC_TIME 2>/dev/null\" | \c"
  echo "getline x)"
  echo "  {"
  echo "    ++knt"
  echo "    n = split(x,tmp_arry,\";\")"
  echo "    j = 0"
  echo "    while (j < n)"
  echo "      {"
  echo "        sub(/^\"/,\"\",tmp_arry[j + 1])"
  echo "        sub(/\"\$/,\"\",tmp_arry[j + 1])"
  echo "        arry[knt,j] = tmp_arry[j + 1]"
  echo "        ++j"
  echo "      }"
  echo "    arry[knt,j] = \"\""
  echo "  }"
  echo "if (knt < 4) # locale failed; fall back on defaults"
  echo "  {"
  echo "    arry[0,0] = \"Sun\"; arry[0,1] = \"Mon\"; \c"
  echo "arry[0,2] = \"Tue\";"
  echo "    arry[0,3] = \"Wed\"; arry[0,4] = \"Thu\"; \c"
  echo "arry[0,5] = \"Fri\";"
  echo "    arry[0,6] = \"Sat\"; arry[0,7] = \"\";"
  echo "    arry[1,0] = \"Sunday\"; arry[1,1] = \"Monday\"; \c"
  echo "arry[1,2] = \"Tuesday\";"
  echo "    arry[1,3] = \"Wednesday\"; arry[1,4] = \"Thursday\"; \c"
  echo "arry[1,5] = \"Friday\";"
  echo "    arry[1,6] = \"Saturday\"; arry[1,7] = \"\";"
  echo "    arry[2,0] = \"Jan\"; arry[2,1] = \"Feb\"; \c"
  echo "arry [2,2] = \"Mar\";"
  echo "    arry[2,3] = \"Apr\"; arry[2,4] = \"May\"; \c"
  echo "arry [2,5] = \"Jun\";"
  echo "    arry[2,6] = \"Jul\"; arry[2,7] = \"Aug\"; \c"
  echo "arry [2,8] = \"Sep\";"
  echo "    arry[2,9] = \"Oct\"; arry[2,10] = \"Nov\"; \c"
  echo "arry [2,11] = \"Dec\";"
  echo "    arry[2,12] = \"\""
  echo "    arry[3,0] = \"January\"; arry[3,1] = \"February\"; \c"
  echo "arry [3,2] = \"March\";"
  echo "    arry[3,3] = \"April\"; arry[3,4] = \"May\"; \c"
  echo "arry [3,5] = \"June\";"
  echo "    arry[3,6] = \"July\"; arry[3,7] = \"August\"; \c"
  echo "arry [3,8] = \"September\";"
  echo "    arry[3,9] = \"October\"; arry[3,10] = \"November\"; \c"
  echo "arry [3,11] = \"December\";"
  echo "    arry[3,12] = \"\""
  echo "    knt = 4"
  echo "  }"
  echo "}"
  echo "{"
  echo "  s_out = \"\""
  echo "  cc = 0"
  echo "  if (((mode + 0) >= 0) && ((mode + 0) <= 3))"
  echo "    {"
  echo "      fnd = 0"
  echo "      i = 0"
  echo "      if (to_number + 0 != 0)"
  echo "        {"
  echo "          key = tolower(\$0)"
  echo "          while ((length(arry[mode + 0,i]) > 0) && \c"
  echo "(fnd == 0))"
  echo "            {"
  echo "              if (key == tolower(arry[mode + 0,i])) fnd = 1"
  echo "              else ++i"
  echo "            }"
  echo "          if (fnd) s_out = sprintf(\"%02d\",i + 1)"
  echo "          else cc = 1"
  echo "        }"
  echo "      else"
  echo "        {"
  echo "          key = sprintf(\"%d\",\$0) + 0"
  echo "          while (length(arry[mode + 0,i]) > 0) ++i;"
  echo "          if ((key >= 1) && (key <= i)) \c"
  echo "s_out = arry[mode + 0,key - 1]"
  echo "          else cc = 1;"
  echo "        }"
  echo "    }"
  echo "  else cc = 255"
  echo "  printf(\"%s\\\n\",s_out)"
  echo "  exit(cc)"
  echo "}"
} # build_A5


# -----------------------------------------------------------------

examples()
{
  echo "" >&2
  echo "" >&2
  echo "EXAMPLES:" >&2
  echo "" >&2
  echo "  Determine the current Julian Day:" >&2 
  echo "  JD=\$(${PROG})" >&2
  echo "" >&2
  echo "  Determine the Julian Day 25 days from now in UTC:" >&2 
  echo "  JD=\$(${PROG} -U -n 25)" >&2
  echo "" >&2
  echo "  Determine the Julian Day 5 days prior to now:" >&2 
  echo "  JD=\$(${PROG} -p 5)" >&2
  echo "" >&2
  echo "  Determine the Julian Day 5 days from now unless that falls \c" >&2 
  echo "on a weekend:" >&2
  echo "  JD=\$(${PROG} -n 5 -x 6 -x 0)" >&2
  echo "" >&2
  echo "  Determine the Julian Day 5 days from now unless that falls \c" >&2 
  echo "on a weekend" >&2
  echo "  or a holiday:" >&2
  echo "  JD=\$(${PROG} -n 5 -x 6 -x 0 -h)" >&2
  echo "" >&2
  echo "  Determine the month 2 days from now unless that falls \c" >&2 
  echo "on a weekend" >&2
  echo "  or a holiday; output in numeric format:" >&2
  echo "  JD=\$(${PROG} -n 2 -x 6 -x 0 -h -M)" >&2
  echo "" >&2
  echo "  Determine the month 3 days from now unless that falls \c" >&2 
  echo "on a weekend;" >&2
  echo "  output in full monthname format:" >&2
  echo "  JD=\$(${PROG} -n 3 -x 6 -x 0 -M -O)" >&2
  echo "" >&2
  echo "  Determine the Julian Day of 26-Jun-2002 MM DD YYYY format:" >&2 
  echo "  JD=\$(${PROG} 6 26 2002)" >&2
  echo "" >&2
  echo "  Determine the Julian Day of 26-Jun-2002 YYYY MM DD format:" >&2 
  echo "  JD=\$(${PROG} -y 2002 6 26)" >&2
  echo "" >&2
  echo "  Determine the Julian Day of 26-Jun-2002 entered as \c" >&2
  echo "'26-JUN-2002':" >&2 
  echo "  JD=\$(${PROG} -i -S \"-\" -e -c \"26-JUN-2002\")" >&2
  echo "" >&2
  echo "  Determine the Julian Day 7 days from 26-Jun-2002 entered as \c" >&2
  echo "'JUNE/26/2002':" >&2 
  echo "  JD=\$(${PROG} -I -S \"/\" -n 7 -c \"JUNE/26/2002\")" >&2
  echo "" >&2
  echo "  Determine the calendar date of JD 2452452; output in \c" >&2
  echo "MM DD YYYY" >&2 
  echo "  format:" >&2
  echo "  CD=\$(${PROG} 2452452)" >&2
  echo "" >&2
  echo "  Determine the calendar date of JD 2452452; output in \c" >&2
  echo "DD MM YYYY" >&2 
  echo "  format:" >&2
  echo "  CD=\$(${PROG} -e 2452452)" >&2
  echo "" >&2
  echo "  Determine the calendar date 12 days after JD 2452452;" >&2
  echo "  output in YYYY MM DD format:" >&2 
  echo "  CD=\$(${PROG} -y -n 12 2452452)" >&2
  echo "" >&2
  echo "  Determine the calendar date 2 days before JD 2452452" >&2
  echo "  unless that falls on a Sunday; output in YYYY MM DD \c" >&2 
  echo "format:" >&2
  echo "  CD=\$(${PROG} -y -p 2 -x 0 2452452)" >&2
  echo "\f\c" >&2
  echo "" >&2
  echo "  Determine the calendar date 20 days after JD 2452452" >&2
  echo "  unless that falls on holiday; output in DD-FullMonthName-YYYY" >&2 
  echo "  format:" >&2
  echo "  CD=\$(${PROG} -e -n 20 -h -O -S \"-\" 2452452)" >&2
  echo "" >&2
  echo "  Determine the weekday 5 days after JD 2452452" >&2
  echo "  unless that falls on holiday; output in nummeric format:" >&2 
  echo "  CD=\$(${PROG} -n 5 -h -w 2452452)" >&2
  echo "" >&2
  echo "  Determine the weekday 4 days prior to JD 2452452; output in" >&2
  echo "  abbreviated weekday name format:" >&2 
  echo "  CD=\$(${PROG} -p 4 -w -o 2452452)" >&2
  echo "" >&2
  echo "  Get the calendar date in DD MM YYYY format 9 days from now \c" >&2
  echo "unless that" >&2
  echo "  falls on a Saturday:" >&2
  echo "  CD=\$(${PROG} -e \$(${PROG} -n 9 -x 6))" >&2
  echo "" >&2
  echo "  Get the calendar date in YYYYMMDD format 3 days prior to now \c" >&2
  echo "unless that" >&2
  echo "  falls on a Sunday:" >&2
  echo "  CD=\$(${PROG} -y -s \$(${PROG} -p 3 -x 0))" >&2
  echo "" >&2
  echo "  Get the calendar date in DD-MM-YY format 5 days from now \c" >&2
  echo "unless that" >&2
  echo "  falls on a Friday or Saturday:" >&2
  echo "  CD=\$(${PROG} -e -S \"-\" -C \$(${PROG} -n 5 -x 5 -x 6))" >&2
  echo "" >&2
  echo "  Get the calendar date in YYYYAbbrevMonthNameDD format 3 days" >&2
  echo "  prior to now unless that falls on a Sunday:" >&2
  echo "  CD=\$(${PROG} -y -s -o \$(${PROG} -p 3 -x 0))" >&2
  echo "\f\c" >&2
  return 254
} # examples
  
usage()
{
  echo "" >&2
  echo "Usage: ${PROG} [-w|-D|-M|-Y|-N] [-u] [-U] [-n days | -p days] \\" >&2
  echo "          [-h] [-x wday] [-o|-O] [-f] [-C]" >&2
  echo "        or" >&2
  echo "       ${PROG} [-w|-D|-M|-Y|-N] [-n days | -p days] \\" >&2
  echo "               [-h] [-x wday] [-o|-O] [-i|-I] [-c] [-f] [-C] \\" >&2
  echo "               MM DD YYYY" >&2
  echo "        or" >&2
  echo "       ${PROG} [-w|-D|-M|-Y|-N] [-n days | -p days] \\" >&2
  echo "               [-h] [-x wday] [-o|-O] [-i|-I] [-c] [-f] [-C] -e \c" >&2
  echo "DD MM YYYY" >&2
  echo "        or" >&2
  echo "       ${PROG} [-w|-D|-M|-Y|-N] [-n days | -p days] \\" >&2
  echo "               [-h] [-x wday] [-o|-O] [-i|-I] [-c] [-f] [-C] -y \c" >&2
  echo "YYYY MM DD" >&2
  echo "        or" >&2
  echo "       ${PROG} [-w|-D|-M|-Y|-N] [-n days | -p days] [-e | -y]  \\" >&2
  echo "               [-h] [-x wday] [-o|-O] [-s] [-S sep] [-f] [-C] \\" >&2
  echo "               JulianDay" >&2
  echo "" >&2
  echo "If MM DD YYYY is supplied, convert to Julian Day or" >&2
  echo "weekday if -w is supplied." >&2
  echo "If JulianDay is supplied, convert to MM DD YYYY or" >&2 
  echo "weekday if -w is supplied." >&2
  echo "If no args are supplied, convert current date to Julian" >&2
  echo "Day or weekday is -w is supplied alone." >&2
  echo "" >&2
  echo "-w return Weekday 0-Sun 6-Sat" >&2
  echo "-D return Day of Month (01-31)" >&2
  echo "-M return Month Number (01-12)" >&2
  echo "-Y return Year (YYYY)" >&2
  echo "-N return the occurrence (1-5) of this weekday within the month." >&2
  echo "-u print this usage message and exit." >&2
  echo "-U use UTC rather than local time to compute current date." >&2
  echo "-n days - add days to result before outputting." >&2
  echo "-p days - subtract days from result before outputting." >&2
  echo "-s - remove spaces from output; MM DD YYYY --> MMDDYYYY." >&2
  echo "-S sep - separate MM DD YYYY with sep (default space)." >&2 
  echo "-x wday (wday = 0 - Sun; 6 - Sat.) skip past wday. Multiple" >&2
  echo "   -x arguments may to used. The skip is positive if using \c" >&2
  echo "-n days" >&2 
  echo "   (or no -n or -p argument) and negative if using -p days." >&2
  echo "-h - skip past holidays as defined in ${HOLIDAY_FILE} for the" >&2
  echo "     current year or in ${HOLIDAY_FILE}_yyyy for other years." >&2
  echo "-c - the supplied date is a calendar date. This is normally used" >&2
  echo "     in conjunction with the -S sep argument to identify the field" >&2
  echo "     separator. For example -S \"-\" -c -y would accept" >&2
  echo "     \"2000-12-31\" as a calendar date and output a Julian Day" >&2
  echo "     just as if the normal three arguments, 2000 12 31, had been" >&2
  echo "     supplied. (Note the use of the -y argument to order the year," >&2
  echo "     month, and day values.)" >&2   
  echo "-i - on input, abbreviated case-insensitive month names" >&2
  echo "     (e.g. 'Jan','Feb','Mar') are recognized." >&2
  echo "-o - On output, abbreviated month names and weekday names," >&2
  echo "     (e.g. 'Sun','Mon','Tue') \c" >&2
  echo "are used rather than numeric values." >&2
  echo "-I - on input, full case-insensitive month names" >&2
  echo "     (e.g. 'January','February','March') are \c" >&2
  echo "recognized." >&2
  echo "-O - On output, full month names and weekday names" >&2
  echo "     (e.g. 'Sunday','Monday','Tuesday') are used rather than" >&2
  echo "     numeric values." >&2
  echo "" >&2
  echo "     Native Language Support (NLS) is provided with the -i,\c" >&2
  echo "-I,-o, and" >&2
  echo "     -O options." >&2
  echo "\f\c" >&2
  echo "" >&2
  echo "By default, year, month, and day arguments are input and output" >&2
  echo "in 'MM DD YYYY' order. This behavior can be changed to DD MM YYYY" >&2
  echo "with the '-e' argument or to 'YYYY MM DD' with the '-y' argument." >&2
  echo "" >&2
  echo "-f - Normally the date operands are passed on the command line" >&2
  echo "     but -f instructs ${PROG} to examine each line of stdin for" >&2
  echo "     date operands and process them as if they were command line" >&2
  echo "     operands. For each line of input, a line of output is written" >&2
  echo "     on stdout. The options (-y, -e, -S, ...) are processed only" >&2
  echo "     once when ${PROG} is launched.\n" >&2 
  echo "-C - Allow 2-digit years to be interpreted as 4-digit years on" >&2
  echo "     input. Normally 2-digit years are taken to be 1st Century" >&2
  echo "     CE dates. By default, a 50-year (from the current year)" >&2
  echo "     sliding window is used. For example in 2003, 53-99 are taken" >&2
  echo "     to be 1953-1999 while 00-52 are taken to be 2000-2052. This" >&2
  echo "     behavior can be overridden by setting a CUTOFFYEAR" >&2
  echo "     environment variable. If CUTOFFYEAR is set to 1920, for" >&2
  echo "     example, years 20-99 become 1920-1999 and years 00-19 become" >&2
  echo "     2000-2019." >&2
  echo "     On output, all years are output as 2-digit values.\n" >&2
  echo "NOTE: Julian Days count sequentially from 4713 BCE; they are \c" >&2
  echo "used by" >&2
  echo "      astronomers to simplify orbital calculations. Julian \c" >&2
  echo "Days are actually" >&2
  echo "      floating point values that can represent days, hours, \c" >&2
  echo "minutes, and" >&2
  echo "      seconds to any desired precision. They begin at noon UTC \c" >&2
  echo "so that midnight" >&2
  echo "      is JD.500000. The values returned by this script are \c" >&2
  echo "Truncated (integer)" >&2
  echo "      Julian Days and begin at midnight local time." >&2
  echo "" >&2
  echo "Version: ${VRSN}" >&2
  echo "\f\c" >&2
  examples
  return 255
} # usage 


is_holiday()
{
  typeset -i YSTAT=0
  if [[ ${#} -lt 2 ]]
    then
      echo "${PROG}: Function is_holiday requires 2 args." >&2
      return ${YSTAT}
    fi
  typeset -i YS_IN=${1}
  typeset Y_UTC_ARG=${2}
  shift 2
  typeset -i Y_DUMMY1=0
  typeset -i Y_DUMMY2=0
  typeset -i Y_YR=0
  jdate_cal "${YS_IN}" " " 0 0 | read Y_DUMMY1 Y_DUMMY2 Y_YR
# get jdate of 1st day of ${Y_YR}
  typeset -i Y_DAY1=$(cal_jdate 1 1 ${Y_YR})
  typeset -i Y_TARGET_DAY=$((${YS_IN} - ${Y_DAY1} + 1))
  typeset -i Y_CURR_YR=$(date ${Y_UTC_ARG} "+%Y")
  if [[ ${Y_YR} -eq ${Y_CURR_YR} ]]
    then
      typeset YFNAME="${HOLIDAY_FILE}_${Y_YR}"
      if [[ ! ( -f ${YFNAME} && -r ${YFNAME} ) ]]
        then
          YFNAME="${HOLIDAY_FILE}"
        fi
    else
      YFNAME="${HOLIDAY_FILE}_${Y_YR}"
    fi
  $(AWK) -v "hol_fname=${YFNAME}" -v "year=${Y_YR}" \
     -v "target_day=${Y_TARGET_DAY}" "${A4}"
  YSTAT=${?}
  return ${YSTAT}
} # is_holiday 

skip_days()
{
  typeset -i XSTAT=0
  if [[ ${#} -lt 5 ]]
    then
      echo "${PROG}: Function skip_days requires 5 args." >&2
      XSTAT=249
      return ${XSTAT}
    fi
  typeset -i XS_IN=${1}
  typeset -i X_DAY_OFFSET=${2}
  typeset X_WDAYS=${3}
  typeset -i X_HOL=${4}
  typeset X_UTC=${5}
  shift 5
  if [[ -n "${X_WDAYS}" || ${X_HOL} -ne 0 ]]
    then
      typeset -i X_FND=1
      until [[ ${X_FND} -eq 0 ]]
        do 
          typeset -i X_FND=0
          if [[ -n "${X_WDAYS}" ]]
            then
              typeset -i X_S3=$(wkday "${XS_IN}")
              for X_WDAY in ${X_WDAYS}
                do
                  if [[ ${X_WDAY} -eq ${X_S3} ]]
                    then
                      X_FND=1
                      break
                    fi
                done
            fi
          if [[ ${X_FND} -eq 0 && ${X_HOL} -ne 0 ]]
            then
              is_holiday "${XS_IN}" "${X_UTC}"
              X_FND=${?} 
            fi 
          if [[ ${X_FND} -ne 0 ]]
            then
              if [[ ${X_DAY_OFFSET} -ge 0 ]]
                then
                  XS_IN=$((${XS_IN} + 1))
                else
                  XS_IN=$((${XS_IN} - 1))
                fi
            fi
        done
    fi 
  echo "${XS_IN}"
  return ${XSTAT}
} # skip_days 




typeset -i STAT=0
typeset -i WKDAY=0
typeset -i SPACES=1
typeset -i CAL_MODE=0
typeset SEP=" "
typeset -i SPACE_KNT=0
typeset -i DAY_OFFSET=0
typeset -i DAY_ARG_KNT=0
typeset -i MDY_MODE=0
# MDY_MODE 0 -> MDY; 1 -> DMY; 2 -> YMD
typeset -i MDY_ARG_KNT=0
typeset UTC_ARG=""
typeset WDAYS=""
typeset -i HOL=0
typeset -i DMY_KNT=0
typeset -i DMY_MODE=0
typeset -i IN_WORDS=0
typeset -i IN_WORDS_KNT=0
typeset -i OUT_WORDS=0
typeset -i OUT_WORDS_KNT=0
typeset -i TWO_DIGIT_MODE=0

# ==========================================================================
process_1_date()
{
  typeset -i DSTAT=0
  typeset S=""
  typeset S0=""
  typeset S2=""
  typeset S3=""
  typeset D0=""
  typeset D1=""
  typeset D2=""
  typeset D3=""
  typeset -i TMP_MDY_MODE=0
  typeset -i KNT=0


  KNT=${#}
  if [[ ${CAL_MODE} -ne 0 && -n "${SEP}" ]]
    then
      # user supplied -c && separator not null
      if [[ ${KNT} -eq 1 && "${SEP}" != " " ]]
        then
          echo "${1}" | $(AWK) -F"${SEP}" '{print $1,$2,$3}' | read D1 D2 D3
          if [[ -z "${D1}" || -z "${D2}" || -z "${D3}" ]]
            then
              echo "${PROG}: -c argument expected 3 parts" >&2
              return 238
            else
              if [[ ${IN_WORDS} -ne 0 ]]
                then 
                  case ${IN_WORDS} in
                    2) A5mode="mode=3"
                       ;;
                    *) A5mode="mode=2"
                       ;;
                  esac
                  case ${MDY_MODE} in
                  1|2) D2=$(echo "${D2}" | \
                         $(AWK) -v "to_number=1" -v "${A5mode}" "${A5}")
                       STAT=${?}
                       ;;
                    *) D1=$(echo "${D1}" | \
                        $(AWK) -v "to_number=1" -v "${A5mode}" "${A5}")
                       STAT=${?}
                       ;;
                  esac
                  if [[ ${STAT} -ne 0 ]]
                    then
                      echo "${PROG}: Could not convert month name; \c" >&2
                      echo "status = ${STAT}." >&2
                      return 237 
                    fi
                fi
              set ${D1} ${D2} ${D3}
              KNT=3
            fi
        fi
    fi
  case ${KNT} in
     0) S0=$(cal_jdate $(date ${UTC_ARG} "+%m %d %Y"))
        S=$((${S0} + ${DAY_OFFSET}))
        S3=$(skip_days ${S} ${DAY_OFFSET} "${WDAYS}" ${HOL} "${UTC_ARG}")
        DSTAT=${?}
        if [[ ${DSTAT} -eq 0 ]]
          then
            S=${S3}
          fi
        if [[ ${WKDAY} -ne 0 || ${DMY_MODE} -ne 0 ]]
          then
            S2=${S}
            if [[ ${WKDAY} -ne 0 ]]
              then
                S=$(wkday "${S2}")
                if [[ ${OUT_WORDS} -ne 0 ]]
                  then
                    case ${OUT_WORDS} in
                      2) A5mode="mode=1"
                         ;;
                      *) A5mode="mode=0"
                         ;;
                    esac
                    S3=$(echo "$((${S} + 1))" | \
                         $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                    DSTAT=${?}
                    if [[ ${DSTAT} -eq 0 ]]
                      then
                        S=${S3}
                      fi
                  fi
              else
                if [[ ${DMY_MODE} -eq 4 ]]
                  then
                    S=$(which_wkday "${S2}")
                  else
                    case ${DMY_MODE} in
                        2) TMP_MDY_MODE=0
                           ;;
                        3) TMP_MDY_MODE=2
                           ;; 
                      1|*) TMP_MDY_MODE=1
                           ;;
                    esac
                    S=$(jdate_cal "${S2}" "${SEP}" ${TMP_MDY_MODE} \
                        ${TWO_DIGIT_MODE} | $(AWK) '{print $1}')
                    if [[ ${OUT_WORDS} -ne 0 && ${DMY_MODE} -eq 2 ]]
                      then
                        case ${OUT_WORDS} in
                          2) A5mode="mode=3"
                             ;;
                          *) A5mode="mode=2"
                             ;;
                        esac
                        S3=$(echo "${S}" | \
                             $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                        DSTAT=${?}
                        if [[ ${DSTAT} -eq 0 ]]
                          then
                            S=${S3}
                          fi
                      fi 
                  fi
              fi
          fi    
        ;;
     1) S=$((${1} + ${DAY_OFFSET}))
        shift
        S3=$(skip_days ${S} ${DAY_OFFSET} "${WDAYS}" ${HOL} "")
        DSTAT=${?}
        if [[ ${DSTAT} -eq 0 ]]
          then
            S=${S3}
          fi
        S0=${S}
        if [[ ${WKDAY} -ne 0 || ${DMY_MODE} -ne 0 ]]
          then
            if [[ ${WKDAY} -ne 0 ]]
              then
                S=$(wkday "${S0}")
                if [[ ${OUT_WORDS} -ne 0 ]]
                  then
                    case ${OUT_WORDS} in
                      2) A5mode="mode=1"
                         ;;
                      *) A5mode="mode=0"
                         ;;
                    esac
                    S2=$(echo "$((${S} + 1))" | \
                         $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                    DSTAT=${?}
                    if [[ ${DSTAT} -eq 0 ]]
                      then
                        S=${S2}
                      fi
                  fi
              else
                if [[ ${DMY_MODE} -eq 4 ]]
                  then
                    S=$(which_wkday "${S0}")
                  else
                    case ${DMY_MODE} in
                        2) TMP_MDY_MODE=0
                           ;;
                        3) TMP_MDY_MODE=2
                           ;; 
                      1|*) TMP_MDY_MODE=1
                           ;;
                    esac
                    S=$(jdate_cal "${S0}" "${SEP}" ${TMP_MDY_MODE} \
                        ${TWO_DIGIT_MODE} | $(AWK) '{print $1}')
                    if [[ ${OUT_WORDS} -ne 0 && ${DMY_MODE} -eq 2 ]]
                      then
                        case ${OUT_WORDS} in
                          2) A5mode="mode=3"
                             ;;
                          *) A5mode="mode=2"
                             ;;
                        esac
                        S3=$(echo "${S}" | \
                             $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                        DSTAT=${?}
                        if [[ ${DSTAT} -eq 0 ]]
                          then
                            S=${S3}
                          fi
                      fi
                  fi 
              fi
          else
            if [[ ${OUT_WORDS} -eq 0 ]]
              then
                S=$(jdate_cal "${S0}" "${SEP}" ${MDY_MODE} ${TWO_DIGIT_MODE})
              else
                jdate_cal "${S0}" "${SEP}" 0 ${TWO_DIGIT_MODE} | read D1 D2 D3
                case ${OUT_WORDS} in
                  2) A5mode="mode=3"
                     ;;
                  *) A5mode="mode=2"
                     ;;
                esac
                D0=$(echo "${D1}" | \
                       $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                DSTAT=${?}
                if [[ ${DSTAT} -eq 0 ]]
                  then 
                    D1=${D0}
                  fi
                case ${MDY_MODE} in
                  1) if [[ ${TWO_DIGIT_MODE} -ne 0 ]]
                       then
                         S=$(echo "${D1}" "${D2}" "${D3}" | 
                               $(AWK) -v "sp=${SEP}" \
                               '{printf("%02d%s%s%s%02d\n",$2,sp,$1,sp,$3)}')
                       else
                         S=$(echo "${D1}" "${D2}" "${D3}" |
                               $(AWK) -v "sp=${SEP}" \
                               '{printf("%02d%s%s%s%04d\n",$2,sp,$1,sp,$3)}')
                       fi
                     ;;
                  2) if [[ ${TWO_DIGIT_MODE} -ne 0 ]]
                       then
                         S=$(echo "${D1}" "${D2}" "${D3}" | 
                               $(AWK) -v "sp=${SEP}" \
                               '{printf("%02d%s%s%s%02d\n",$3,sp,$1,sp,$2)}')
                       else
                         S=$(echo "${D1}" "${D2}" "${D3}" |
                              $(AWK) -v "sp=${SEP}" \
                              '{printf("%04d%s%s%s%02d\n",$3,sp,$1,sp,$2)}')
                       fi
                     ;;
                0|*) if [[ ${TWO_DIGIT_MODE} -ne 0 ]]
                       then
                         S=$(echo "${D1}" "${D2}" "${D3}" | 
                              $(AWK) -v "sp=${SEP}" \
                              '{printf("%s%s%02d%s%02d\n",$1,sp,$2,sp,$3)}')
                       else
                         S=$(echo "${D1}" "${D2}" "${D3}" |
                              $(AWK) -v "sp=${SEP}" \
                              '{printf("%s%s%02d%s%04d\n",$1,sp,$2,sp,$3)}')
                       fi
                     ;;
                esac
              fi
          fi
        ;;
     3) D1=${1}
        D2=${2}
        D3=${3}
        shift 3
        typeset D0=""
        case ${IN_WORDS} in
          2) A5mode="mode=3"
             ;;
          *) A5mode="mode=2"
             ;;
        esac
        case ${MDY_MODE} in
           1) if [[ ${IN_WORDS} -ne 0 && ${CAL_MODE} -eq 0 ]]
                then
                  D0=$(echo "${D2}" | \
                       $(AWK) -v "to_number=1" -v "${A5mode}" "${A5}")
                  DSTAT=${?}
                  if [[ ${DSTAT} -eq 0 ]]
                    then 
                      D2=${D0}
                    else
                      echo "${PROG}: Could not convert month name; \c" >&2
                      echo "status = ${DSTAT}." >&2
                      return 234 
                    fi
                fi
              S0=$(cal_jdate ${D2} ${D1} $(century_year ${D3} "${UTC_ARG}" \
                   ${TWO_DIGIT_MODE}))
              ;;
           2) if [[ ${IN_WORDS} -ne 0 && ${CAL_MODE} -eq 0 ]]
                then
                  D0=$(echo "${D2}" | \
                       $(AWK) -v "to_number=1" -v "${A5mode}" "${A5}")
                  DSTAT=${?}
                  if [[ ${DSTAT} -eq 0 ]]
                    then 
                      D2=${D0}
                    else
                      echo "${PROG}: Could not convert month name; \c" >&2
                      echo "status = ${DSTAT}." >&2
                      return 234 
                    fi
                fi
              S0=$(cal_jdate ${D2} ${D3} $(century_year ${D1} "${UTC_ARG}" \
                   ${TWO_DIGIT_MODE}))
              ;;
         *|0) if [[ ${IN_WORDS} -ne 0 && ${CAL_MODE} -eq 0 ]]
                then
                  D0=$(echo "${D1}" | \
                       $(AWK) -v "to_number=1" -v "${A5mode}" "${A5}")
                  DSTAT=${?}
                  if [[ ${DSTAT} -eq 0 ]]
                    then 
                      D1=${D0}
                    else
                      echo "${PROG}: Could not convert month name; \c" >&2
                      echo "status = ${DSTAT}." >&2
                      return 234 
                    fi
                fi
              S0=$(cal_jdate ${D1} ${D2} $(century_year ${D3} "${UTC_ARG}" \
                   ${TWO_DIGIT_MODE}))
              ;;
        esac
        S=$((${S0} + ${DAY_OFFSET})) 
        S3=$(skip_days ${S} ${DAY_OFFSET} "${WDAYS}" ${HOL} "")
        DSTAT=${?}
        if [[ ${DSTAT} -eq 0 ]]
          then
            S=${S3}
          fi
        if [[ ${WKDAY} -ne 0 || ${DMY_MODE} -ne 0 ]]
          then
            S2=${S}
            if [[ ${WKDAY} -ne 0 ]]
              then
                S=$(wkday "${S2}")
                if [[ ${OUT_WORDS} -ne 0 ]]
                  then
                    case ${OUT_WORDS} in
                      2) A5mode="mode=1"
                         ;;
                      *) A5mode="mode=0"
                         ;;
                    esac
                    S2=$(echo "$((${S} + 1))" | \
                         $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                    DSTAT=${?}
                    if [[ ${DSTAT} -eq 0 ]]
                      then
                        S=${S2}
                      fi
                  fi
              else
                if [[ ${DMY_MODE} -eq 4 ]]
                  then
                    S=$(which_wkday "${S2}")
                  else
                    case ${DMY_MODE} in
                        2) TMP_MDY_MODE=0
                           ;;
                        3) TMP_MDY_MODE=2
                           ;; 
                      1|*) TMP_MDY_MODE=1
                           ;;
                    esac
                    S=$(jdate_cal "${S2}" "${SEP}" ${TMP_MDY_MODE} \
                        ${TWO_DIGIT_MODE} | $(AWK) '{print $1}')
                    if [[ ${OUT_WORDS} -ne 0 && ${DMY_MODE} -eq 2 ]]
                      then
                        case ${OUT_WORDS} in
                          2) A5mode="mode=3"
                             ;;
                          *) A5mode="mode=2"
                             ;;
                        esac
                        S3=$(echo "${S}" | \
                             $(AWK) -v "to_number=0" -v "${A5mode}" "${A5}")
                        DSTAT=${?}
                        if [[ ${DSTAT} -eq 0 ]]
                          then
                            S=${S3}
                          fi
                      fi 
                  fi
              fi
          fi
        ;;
     *) echo "${PROG} Invalid args" >&2
        usage
        DSTAT=${?}
  esac
  echo "${S}"
  return ${DSTAT}
} # process_1_date       




# ==========================================================================


typeset -i USEFILE=0
STAT=0

while getopts DMYwWuUciIoOseyHhn:p:Nx:X:S:FfC opts
  do
     case ${opts} in
         D) DMY_MODE=1
            DMY_KNT=$((${DMY_KNT} + 1))
            ;;
         M) DMY_MODE=2
            DMY_KNT=$((${DMY_KNT} + 1))
            ;;
         Y) DMY_MODE=3
            DMY_KNT=$((${DMY_KNT} + 1))
            ;;
         N) DMY_MODE=4
            DMY_KNT=$((${DMY_KNT} + 1))
            ;;
       w|W) WKDAY=1
            ;;
         u) STAT=251
            ;;
         U) UTC_ARG="-u"
            ;;
         c) CAL_MODE=1
            ;;
         i) IN_WORDS=1
            IN_WORDS_KNT=$((${IN_WORDS_KNT} + 1))
            ;;
         I) IN_WORDS=2
            IN_WORDS_KNT=$((${IN_WORDS_KNT} + 1))
            ;;
         o) OUT_WORDS=1
            OUT_WORDS_KNT=$((${OUT_WORDS_KNT} + 1))
            ;;
         O) OUT_WORDS=2
            OUT_WORDS_KNT=$((${OUT_WORDS_KNT} + 1))
            ;;
         s) SPACES=0
            SEP=""
            SPACE_KNT=$((${SPACE_KNT} + 1))
            ;;
         e) MDY_MODE=1
            MDY_ARG_KNT=$((${MDY_ARG_KNT} + 1))
            ;;
         y) MDY_MODE=2
            MDY_ARG_KNT=$((${MDY_ARG_KNT} + 1))
            ;;
         n) DAY_OFFSET=$((${OPTARG} + 0))
            DAY_ARG_KNT=$((${DAY_ARG_KNT} + 1))
            ;;
         p) DAY_OFFSET=$((-(${OPTARG})))
            DAY_ARG_KNT=$((${DAY_ARG_KNT} + 1))
            ;;
       x|X) if [[ -n "${WDAYS}" ]]
              then
                WDAYS="${WDAYS} ${OPTARG}"
              else
                WDAYS="${OPTARG}"
              fi
            ;;
       h|H) HOL=1
            ;;
         S) SEP="${OPTARG}"
            SPACE_KNT=$((${SPACE_KNT} + 1))
            ;;
       f|F) USEFILE=1
            ;;
         C) TWO_DIGIT_MODE=1
            ;;
         ?) echo "${PROG}: Unknown arg.\n" >&2
            echo "" >&2
            STAT=252
            ;;
     esac
  done

if [[ ${DAY_ARG_KNT} -gt 1 && ${STAT} -eq 0 ]]
  then
    echo "${PROG}: Only one -n or -p argument allowed." >&2
    echo "" >&2
    STAT=255 
  fi

if [[ $((${DMY_KNT} + ${WKDAY})) -gt 1 ]]
  then
    echo "${PROG}: Only one -w, -N, -D, -M, or -Y argument allowed." >&2
    echo "" >&2
    STAT=248 
  fi

if [[ ${MDY_ARG_KNT} -gt 1 && ${STAT} -eq 0 ]]
  then
    echo "${PROG}: Only one -y or -e argument allowed." >&2
    echo "" >&2
    usage
    STAT=254 
  fi

if [[ ${SPACE_KNT} -gt 1 && ${STAT} -eq 0 ]]
  then
    echo "${PROG}: Only one -s or -S argument allowed." >&2
    echo "" >&2
    usage
    STAT=247 
  fi

if [[ ${IN_WORDS_KNT} -gt 1 && ${STAT} -eq 0 ]]
  then
    echo "${PROG}: Only one -i or -I argument allowed." >&2
    echo "" >&2
    usage
    STAT=236 
  fi

if [[ ${OUT_WORDS_KNT} -gt 1 && ${STAT} -eq 0 ]]
  then
    echo "${PROG}: Only one -o or -O argument allowed." >&2
    echo "" >&2
    usage
    STAT=235 
  fi
    
    
typeset -i N_WDAYS=0
if [[ -n "${WDAYS}" && ${STAT} -eq 0 ]]
  then
    for WDAY in ${WDAYS}
      do
        if [[ "${WDAY}" -lt 0 || "${WDAY}" -gt 6 ]]
          then
            echo "${PROG}: Invalid -w argument; must be in range (0-6)." >&2
            echo "" >&2
            STAT=253
            break
          else
            N_WDAYS=$((${N_WDAYS} + 1))
          fi
      done
  fi

if [[ ${N_WDAYS} -gt 6 && ${STAT} -eq 0 ]]
  then
    echo "${PROG}: Invalid No more than 6 -x arguments allowed." >&2
    echo "" >&2
    STAT=250
  fi   


if [[ ${STAT} -ne 0 ]]
  then
    usage
    exit ${STAT}
  fi

# build awk scripts but only if needed
if [[ ${HOL} -ne 0 ]]
  then
    A4=$(build_A4)
  fi
if [[ ${IN_WORDS} -ne 0 || ${OUT_WORDS} -ne 0 ]]
  then
    A5=$(build_A5)
  fi 

typeset -i KNT=1
while [[ ${KNT} -lt ${OPTIND} ]]
  do
    shift
    KNT=$((${KNT} + 1))
  done

typeset S_IN=""
typeset S_OUT=""

if [[ ${USEFILE} -eq 0 ]]
  then # input data on command line
    S_OUT=$(process_1_date ${@})
    STAT=${?}
    echo "${S_OUT}"
  else # input data on stdin
    while read S_IN
      do
        set ${S_IN}
        S_OUT=$(process_1_date ${@})
        STAT=${?}
        echo "${S_OUT}"
        if [[ ${STAT} -ne 0 ]]
          then
            exit ${STAT}
          fi
      done
  fi
exit ${STAT}


