#!/bin/ksh

#    Developer: Carlos Cordova, DSM/SRCB
#      Version: 1.0 Patch 2
# Release Date: 2003 OCT 11

# bxphdrpt - Create the Facility Data Header Report

INFILE=$1
infile=$2
MODE=$3
TYP=$(expr ${INFILE} : "^.\(...\).*")
NAME=$(awk -F: '$1==x {print $3}' x=${TYP} ${SCRIPTS}/bxpdata.tbl)

getanswer(){
  # flush the input queue, display the message, read the response
  ${BIN}/inputflush
  print "\t$1"
  read ANS
  ANS=$(print ${ANS} | tr "[a-z]" "[A-Z]")
}

PATTERN1=$1'[0-9][0-9][0-9][0-9][0-9][0-9].[0-9]*'
PATTERN2=$1'[0-9][0-9][0-9][0-9].[0-9]*'
cd ${PUBDIR}
if [ -n "$(ls ${PATTERN1} ${PATTERN2} > /dev/null 2>&1)" ]
then
  print " 1 No Service Unit ${INFILE} Source Files found in ${PUBDIR}"
  exit 1
fi

SAVE=${SAVE}/${infile}
[ ! -d ${SAVE} ] &&  mkdir ${SAVE}

for FILE in $(ls ${PATTERN2} 2> /dev/null)
do
  case "${FILE##*.}" in
    gz) CAT=gzcat; EXT=".gz" ;;
    Z)  CAT=zcat; EXT=".Z";;
    z)  CAT=pcat; EXT=".z" ;;
    *)  CAT=cat; EXT="" ;;
  esac
  if [ ${infile} = "apc" ]
  then
     ASUFAC=$(${CAT} ${FILE} | awk -F\^ 'NR==4 {print $2}')
  else
     ASUFAC=$(${CAT} ${FILE} | awk -F\^ 'NR==4 {print $1}')
  fi
     DATE=$(print ${FILE} | awk -F\. '{print $2}')

  print "Renaming ${FILE} to ${INFILE}${ASUFAC}.${DATE}${EXT}"
  mv ${FILE} ${INFILE}${ASUFAC}.${DATE}${EXT}
  if [ $? -ne 0 ]
  then
    print "ERROR: ${FILE} not renamed - Aborting"
    exit 1
  fi
 done

DATE=$(date +%Y%j)
TARPATTERN=${infile}fac.'[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'.tar
PATTERN=${INFILE}'[0-9][0-9][0-9][0-9][0-9][0-9].[0-9]*'
TARFILELIST=${SAVE}/tarfilelist.$$

TARFILES=$(find ${SAVE} -name ${TARPATTERN} -ctime -45 -exec basename {} \; 2> /dev/null)
cd ${SAVE}
for TARFILE in $(print ${TARFILES})
do
  if [ ${infile} = "apc" ] 
    then 
   JJJ=$(print ${TARFILE} | awk -F\. '{print $3}') 
   tar tvf ${TARFILE} |\
   awk '{printf "%s Processed On %d\n",$10,x}' x=${JJJ}    
    else
    JJJ=$(print ${TARFILE} | awk -F\. '{print $2}')
   tar tvf ${TARFILE} |\
   awk '{printf "%s Processed On %d\n",$9,x}' x=${JJJ}
  fi
done > ${TARFILELIST}

cd ${PUBDIR}
FILES=$(ls ${PATTERN} 2>/dev/null)
if [ -z "${FILES}" ]
then
  print " 2 No Service Unit ${INFILE} Source Files found in ${PUBDIR}"
  exit 1
fi

for FILE in $(print ${FILES})
do
    if [ ${infile} = "apc" ] 
    then
  FIL=$(print ${FILE} | awk -F\. '{printf "%s.%s",$2,$3}')
    else
  FIL=$(print ${FILE} | awk -F\. '{printf "%s.%s",$1,$2}')
    fi
  if grep ${FIL} ${TARFILELIST}
  then
    if [ "${MODE}" = "AUTO" ]
    then
      print "This is a REDO process that requires Interactive BXP."
      print "Start BXP in Menu Mode to perform the PROC option."
      rm -f ${TARFILELIST}
      exit 1
    else
      while true
      do
        getanswer "Is this a reprocess? N//o \c"
        case ${ANS:-N} in
          N*) rm -f ${FILE}; break ;;
          Y*) break ;;
          Q*|\^) rm -f ${TARFILELIST}; exit 1 ;;
          *) print "\c"; continue ;;
        esac
      done
    fi
  fi
done
rm -f ${TARFILELIST}

FILES=$(ls ${PATTERN} 2>/dev/null)
if [ -z "${FILES}" ]
then
  print "3 No Service Unit ${INFILE} Source Files found in ${PUBDIR}"
  exit 1
fi
HDR=${SAVE}/${infile}hdr.${DATE}

{
print "ASUFAC                                           BEGIN DATE NUMBER"
print "CODE   FACILITY NAME                  RUN DATE   END DATE   RECORDS"
print "====== ============================== ========== ========== ======="
for FILE in ${FILES}
do
  case "${FILE##*.}" in
    gz) CAT=gzcat ;;
    Z)  CAT=zcat ;;
    z)  CAT=pcat ;;
    *)  CAT=cat ;;
  esac
  if [ ${infile} = "apc" ] 
      then
    ${CAT} ${FILE} |\
    awk -F\^ 'NR==4 {
    if ( length($4) >= 8 && length($5) >= 8 && length($6) >= 8 ) {
        printf "%s %-30s %2d/%2s/%d %2d/%2s/%d %7d\n\t\t\t\t\t\t %2d/%2s/%d\n",
          $2,$3,substr($4,5,2),substr($4,7,2),substr($4,1,4),
          substr($5,5,2),substr($5,7,2),substr($5,1,4),$8,
          substr($6,5,2),substr($6,7,2),substr($6,1,4)
    }
    else {
        printf "%s %-30s %s\n",$2,$3,"EXPORT DATA NOT Y2K COMPLIANT"
    }
  }                                                                          
  NR>4 { exit 0 }'
  fi
   if [ ${infile} != "apc" ]
    then
  ${CAT} ${FILE} |\
  awk -F\^ 'NR==4 {
    if ( length($3) >= 8 && length($4) >= 8 && length($5) >= 8 ) {
        printf "%s %-30s %2d/%2s/%d %2d/%2s/%d %7d\n\t\t\t\t\t\t %2d/%2s/%d\n",
          $1,$2,substr($3,5,2),substr($3,7,2),substr($3,1,4),
          substr($4,5,2),substr($4,7,2),substr($4,1,4),$7,
          substr($5,5,2),substr($5,7,2),substr($5,1,4)
    }
    else {
        printf "%s %-30s %s\n",$1,$2,"EXPORT DATA NOT Y2K COMPLIANT"
    }
  }
  NR>4 { exit 0 }'
  fi
done |\
awk '{ print $0;
       if ( NF > 4 && $NF != "COMPLIANT" ) TOT+=$NF }
  END {
    printf "                                                            =======\n"
    printf "                                                     TOTAL: %7d\n",TOT
   }'
} | tee ${HDR}

if grep "EXPORT DATA NOT Y2K COMPLIANT" ${HDR}  > /dev/null 2>&1
then
  print "\n*********************************************************************"
  print "** Facility Data includes NON Y2K Compliant Data                   **"
  print "** BXP processing is being Aborted                                 **"
  print "** Facilities that have NON Y2K Compliant Data needs to be updated **"
  print "** with the Y2K Compliant Applications.                            **"
  print "** After Applications Update - Then redo the Data Extract.         **"
  print "*********************************************************************"
  exit 1
fi

if [ "${MODE}" != "AUTO" ]
then
  while true
  do
    print "Do you wish to process the"
    getanswer "${NAME} Service Unit input files? Y//es \c"
    case ${ANS:-Y} in
      Y*) break ;;
      N*|Q*|\^) exit 1 ;;
      *) print "^G\c"; continue ;;
    esac
  done
fi

print "Processing the ${NAME} Service Unit input files."

