#!/usr/bin/ksh93
################################################################
function dst_header {
  FD="${1:?# ERROR: file descriptor number not specified}"
  DC="${2:?# ERROR: data center identifier not specified}"
  print -u ${FD} -- "<!-- Begin \"dst${DC}Updates.content.shtml\" -->"
  print -u ${FD} -- "\n<P><H2>Daylight Savings Time (DST) System Compliance</H2></P>"
  print -u ${FD} -- "\n<P><TABLE Border=\"1\">"
  print -u ${FD} -- "<P><TR>"
  print -u ${FD} -- "<TH Bgcolor=\"lightgrey\"><A Href=\"dst${DC}byhostname.shtml\">Host Name</A></TH>"
  print -u ${FD} -- "<TH Bgcolor=\"lightgrey\"><A Href=\"dst${DC}byoslevel.shtml\">OS Level</A></TH>"
  print -u ${FD} -- "<TH Bgcolor=\"lightgrey\"><A Href=\"dst${DC}byfileset.shtml\">Fileset</A></TH>"
  print -u ${FD} -- "<TH Bgcolor=\"lightgrey\"><A Href=\"dst${DC}bycomply.shtml\">Compliance</A></TH>"
  print -u ${FD} -- "<TH Bgcolor=\"lightgrey\">Description</TH>"
  print -u ${FD} -- "</TR></P>"
}
################################################################
function dst_footer {
  FD="${1:?# ERROR: file descriptor number not specified}"
  DC="${2:?# ERROR: data center identifier not specified}"
  DATESTAMP="$( date )"

  print -u ${FD} -- "</TABLE></P>"
  print -u ${FD} -- "<P><I>${DATESTAMP}</I></P>"
  print -u ${FD} -- "\n<!-- End \"dst${DC}Updates.content.shtml\" -->"
}
################################################################

typeset -L3 DC="${1:?# ERROR: 3 character lowercase datacenter identifier must be specified (mx0|mx1)}"
if [[ "${DC}" != @(mx0|mx1) ]]
then
  print -u 2 -- "# ERROR: invalid datacenter identifier \"${DC}\": valid identifiers are mx0|mx1"
  exit 1
fi

cat << ENDCAT > /tmp/cat${$}.out
5.3	bos.rte.lib	IY75211
5.3	bos.adt.prof	IY75211
5.2	bos.rte.lib	IY75213
5.2	bos.adt.prof	IY75213
5.1	bos.rte.lib	IY75214
5.1	bos.adt.prof	IY75214
5.3	bos.rte.date	IY91662
5.2	bos.rte.date	IY91985
5.3	Java14.sdk	IY84053
5.3	Java14_64.sdk	IY84054
5.2	Java14.sdk	IY84053
5.2	Java14_64.sdk	IY84054
5.1	Java14.sdk	IY84053
5.1	Java14_64.sdk	IY84054
5.3	Java13.sdk	IY85293
5.3	Java13_64.sdk	IY85294
5.2	Java13.sdk	IY85293
5.2	Java13_64.sdk	IY85294
5.3	Java13.sdk	IY85293
5.3	Java13_64.sdk	IY85294
5.2	Java13.sdk	IY85293
5.2	Java13_64.sdk	IY85294
5.1	Java13.sdk	IY85293
5.1	Java13_64.sdk	IY85294
5.3	Java5.sdk	IY82213
5.3	Java5_64.sdk	IY82254
5.2	Java5.sdk	IY82213
5.2	Java5_64.sdk	IY82254
5.1	Java5.sdk	IY82213
5.1	Java5_64.sdk	IY82254
4.3	Java14.sdk	IY84053
4.3	Java14_64.sdk	IY84054
4.3	Java130.rte.bin	IY85293
4.3	Java130.rte.lib	IY85294
4.3	Java5.sdk	IY82213
4.3	Java5_64.sdk	IY82254
5.3	sysmgt.websm.framework	IY83394
5.2	sysmgt.websm.framework	IY86260
ENDCAT

exec 3>/tmp/dstscript${$}.out
FD="3"

####  amms.list is copied from NIMserver:/root/amms.list to users home directory
cut -d"|" -f2 ./amms.list |
while read -- CNAME
do
  OS=$( ssh -x root@${CNAME} 'oslevel' )

  sort /tmp/cat${$}.out |
  while read OSLEVEL FILESET APARNUM
  do
    print -u 2 -- "# ${CNAME} ${OS} ${FILESET} ${APARNUM}"
    APAR=""
    if [[ "_${OS}" == _${OSLEVEL}* ]]
    then
      APAR=$( ssh -x root@${CNAME} "if lslpp -l ${FILESET} 1>&2; then instfix -ik ${APARNUM} 2>&1; fi" )
    fi
    if [[ "_${APAR}" != "_" ]]
    then
      APAR="${APAR//[!A-Za-z0-9. -]/}"
      APAR=${APAR##*([ ])}

      COMPLY="<STRONG><FONT Color=\"red\">Non-Compliant</FONT></STRONG>"
      [[ "_${APAR}" == _"All filesets"* ]] && COMPLY="Compliant"

      print -u ${FD} -n -- "<P><TR>"
      print -u ${FD} -n -- "<TD>${CNAME}</TD><!-- | -->"
      print -u ${FD} -n -- "<TD>${OS}</TD><!-- | -->"
      print -u ${FD} -n -- "<TD>${FILESET}</TD><!-- | -->"
      print -u ${FD} -n -- "<TD>${COMPLY}</TD><!-- | -->"
      print -u ${FD} -n -- "<TD>${APAR}</TD><!-- | -->"
      print -u ${FD} -- "</TR></P>"

    fi

  done
  
done

exec 3>&-

#### create HTML content file sorted by hostname
exec 3>/tmp/dst${DC}byhostname.content.shtml
  dst_header 3 ${DC}
    sort -f -t '|' +0 +2 /tmp/dstscript${$}.out >&3
  dst_footer 3 ${DC}
exec 3>&-

#### create HTML content file sorted by fileset name
exec 3>/tmp/dst${DC}byfileset.content.shtml
  dst_header 3 ${DC}
    sort -f -t '|' +2 +0 /tmp/dstscript${$}.out >&3
  dst_footer 3 ${DC}
exec 3>&-

#### create HTML content file sorted by whether it is DST compliant
exec 3>/tmp/dst${DC}bycomply.content.shtml
  dst_header 3 ${DC}
    sort -f -t '|' +3 +2 +0 /tmp/dstscript${$}.out >&3
  dst_footer 3 ${DC}
exec 3>&-

#### create HTML content file sorted by oslevel
exec 3>/tmp/dst${DC}byoslevel.content.shtml
  dst_header 3 ${DC}
    sort -f -t '|' +1 +0 /tmp/dstscript${$}.out >&3
  dst_footer 3 ${DC}
exec 3>&-

rm -f /tmp/cat${$}.out
rm -f /tmp/dstscript${$}.out