#!/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
4.3	Java14.sdk	IY84053
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
    FILESET="TZ Timezone Variable"
    print -u 2 -- "# ${CNAME} ${OS} ${FILESET} ${APARNUM}"
    APAR=""
    if [[ "_${OS}" == _${OSLEVEL}* ]]
    then
      TZVAL=$( ssh -x root@${CNAME} "grep '^TZ=' /etc/environment" 2>/dev/null)
      APAR="${TZVAL}"
    fi

    if [[ "_${APAR}" != "_" ]]
    then

      COMPLY="<STRONG><FONT Color=\"red\">Non-Compliant</FONT></STRONG>"
      [[ "_${APAR}" == "_TZ=CST6CDT,M3.2.0,M11.1.0" ]] && COMPLY="Compliant"

      print -u 2 -- "# ${CNAME} ${OS} ${FILESET} ${COMPLY} ${APAR}"
      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}tzbyhostname.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}tzbyfileset.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}tzbycomply.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}tzbyoslevel.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