Saturday, January 1, 2000

checkpatches.ksh

#!/bin/ksh
#####################################################################################
# #
# checkpatches.ksh #
# #
# Author: Alan T. Landucci-Ruiz #
# Organization: AZDHS #
# Description: This script automatically downloads the latest patchdiag.xref file #
# from sunsolve, runs the sunsolve, then re-formats the output to #
# one of three formats: txt, csv, or html. #
# #
# Pseudocode: #
# #
# check command-line options #
# download patchdiag.xref file #
# run patchdiag #
# generate output based on options #
# #
# Revision History: #
# 2008-01-14 Alan T. Landucci-Ruiz #
# Initial revision. #
# #
#####################################################################################

#####################################################################################
# #
# Global Variables #
# BASENAME The name of the script, without the leading directory path #
# DIRNAME The name of the directory where this script resides #
# WGET The absolute path (location) of the command 'wget.' #
# XREF_URL The absolute URL where we get patchdiag.xref (source) #
# XREF_LOCAL The absolute path where we want to store the local #
# patchdiag.xref #
# PATCHDIAG_TMP The absolute location where the patchdiag output will be #
# stored #
# WGET_LOG The absolute path for the wget (xref) log file #
# WGET_LOG_OPT The command-line option to log to wget (xref) log file #
# VERBOSE Flag whether or not we want output to stdout #
# MAILTO Unix address where to mail file #
# OUTPUT_TYPE The type of output we want (txt, csv, html) #
# #
#####################################################################################
BASENAME=$(basename $0)
DIRNAME=$(dirname $0)
WGET=/opt/csw/bin/wget
XREF_URL=http://sunsolve.sun.com/patchdiag.xref
XREF_DEFAULT=/opt/SUNWpatchdiag/etc/patchdiag.xref
XREF_LOCAL=/tmp/patchdiag.xref
PATCHDIAG=/opt/SUNWpatchdiag/bin/patchdiag
PATCHDIAG_TMP=/tmp/patchdiag.tmp
PATCHDIAG_OUT=""
USE_LOCAL_XREF=FALSE
WGET_LOG="/tmp/wget.tmp"
WGET_LOG_OPT=""
VERBOSE=TRUE
OUTPUT_TYPE=txt
MAILTO=""
HOSTNAME=`hostname`

usage() {
echo "USAGE: $BASENAME [-h] [-wct] [-q] [-o <output_file>] [-m <address>] [-l]"
echo ""
echo "OPTIONS:"
echo " -h - Print usage/help screen."
echo " -w - Create HTML(web) output."
echo " -c - Create CSV output."
echo " -t - Create TEXT output."
echo " -q - Suppress Diagnostic Output."
echo " -o <ouput_file> - Print output to file specified by <output_file>."
echo " -m <address> - Email results to <address>."
echo " -l - Use cached (local) patchdiag.xref file."
echo ""
}

perror()
{
printf "ERROR: $1\n"
exit $2;
}

pwarn()
{
[ "$VERBOSE" = "TRUE" ] && printf "WARNING: $1\n"
}

file_checks()
{
if [ -e ${PATCHDIAG_TMP} ]; then
pwarn "File ${PATCHDIAG_TMP} exists. Removing. "
rm ${PATCHDIAG_TMP}
fi
if [ ! -e ${WGET} ]; then
WGET=/usr/sfw/bin/wget
fi
}

get_xref_web()
{
[ "$VERBOSE" = "TRUE" ] && echo "Retrieving XREF"
${WGET} ${WGET_LOG_OPT} -O ${XREF_LOCAL} ${XREF_URL}
if [ $? -ne 0 ]; then
[ ! -z $WGET_LOG ] && (cat $WGET_LOG; rm $WGET_LOG)
pwarn "Cannot retrieve xref from ${XREF_URL}. Using existing ${XREF_DEFAULT}"
XREF_LOCAL=${XREF_DEFAULT}
fi
}

do_patchdiag()
{
[ "$VERBOSE" = "TRUE" ] && echo "Creating tmpfile to ${PATCHDIAG_TMP}"
${PATCHDIAG} -l -x ${XREF_LOCAL} > ${PATCHDIAG_TMP}
}

create_patch_output()
{
TYPE=$1
if [ "$TYPE" = "csv" ]; then
[ "$VERBOSE" = "TRUE" ] && echo "Creating $TYPE."
head -1 ${PATCHDIAG_TMP} > ${PATCHDIAG_TMP}.$TYPE
awk '/System Name:/,/\=\=\=\=\=\=/' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/Report Note:/,/\=\=\=\=\=\=/' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED RECOMMENDED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "Patch ID,Ins Rev,Lat Rev,Age,Require ID,Incomp ID,Synopsis\n"; \
} \
if ( $6 == "Incomp" || $_ ~ /Rev Rev/ ) \
{ printf ""; \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,%s,,",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,,,",$1,$2,$3,$4; \
for (i=5;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf ",,,,%s,,\n",$1; \
} \
else \
if ( $1 !~ /^[0-9]/ && $6 != "Incomp" && $_ !~ /Rev Rev/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED SECURITY PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "Patch ID,Ins Rev,Lat Rev,Age,Require ID,Incomp ID,Synopsis\n"; \
} \
if ( $6 == "Incomp" || $_ ~ /Rev Rev/ ) \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,%s,,",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,,,",$1,$2,$3,$4; \
for (i=5;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf ",,,,%s,,\n",$1; \
} \
else \
if ( $1 !~ /^[0-9]/ && $6 != "Incomp" && $_ !~ /Rev Rev/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED Y2K PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "Patch ID,Ins Rev,Lat Rev,Age,Require ID,Incomp ID,Synopsis\n"; \
} \
if ( $6 == "Incomp" || $_ ~ /Rev Rev/ ) \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,%s,,",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,,,",$1,$2,$3,$4; \
for (i=5;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf ",,,,%s,,\n",$1; \
} \
else \
if ( $1 !~ /^[0-9]/ && $6 != "Incomp" && $_ !~ /Rev Rev/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/^INSTALLED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "Patch ID,Ins Rev,Lat Rev,Synopsis\n"; \
} \
if ( $4 == "Synopsis" || $_ ~ /Revision Revision/ ) \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && NF > 2 && $_ !~ /CURRENT/ && $_ !~ /Obsoleted/) \
{ printf "%s,%s,%s,",$1,$2,$3; \
for (i=4;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 !~ /^[0-9]/ && $4 != "Synopsis" && $_ !~ /Revision Revision/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/^OTHER RELATED UNINSTALLED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "Patch ID,Flag,Ins Rev,Lat Rev,Synopsis\n"; \
} \
if ( $5 == "Synopsis" || $3 == "Rev") \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && $2 == "R" && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,%s,",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && $2 == "S" && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,%s,",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && $2 == "*" && $_ !~ /Obsoleted/ ) \
{ printf "%s,%s,%s,%s,%s,",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s\n",$NF \
} \
else \
if ( $1 !~ /^[0-9]/ && NF > 1 && $5 != "Synopsis" && $3 != "Rev" || $_ ~ /^$/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
elif [ $TYPE = "html" ]; then
[ "$VERBOSE" = "TRUE" ] && echo "Creating $TYPE."
printf "<HTML>\n" > ${PATCHDIAG_TMP}.$TYPE
printf "<BODY>\n" >> ${PATCHDIAG_TMP}.$TYPE
head -1 ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
printf "<BR>\n" >> ${PATCHDIAG_TMP}.$TYPE
awk '/System Name:/,/\=\=\=\=\=\=/ { print; printf "<BR>\n" }' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/Report Note:/,/\=\=\=\=\=\=/ { print; printf "<BR>\n" }' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED RECOMMENDED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "<TABLE BORDER=1><TR><TD>Patch ID</TD><TD>Ins Rev</TD><TD>Lat Rev</TD><TD>Age</TD><TD>Require ID</TD><TD>Incomp ID</TD><TD>Synopsis</TD></TR>\n"; \
} \
if ( $6 == "Incomp" || $_ ~ /Rev Rev/ ) \
{ printf ""; \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>&nbsp;</TD><TD>",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>",$1,$2,$3,$4; \
for (i=5;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>%s</TD><TD>&nbsp;</TD></TR>\n",$1; \
} \
else \
if ( $1 !~ /^[0-9]/ && $6 != "Incomp" && $_ !~ /Rev Rev/ && $_ !~ /\-\-\-\-\-\-/ && $_ !~ /\=\=\=\=\=\=/ ) { print; printf "<BR>\n" } \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
printf "</TABLE>\n" >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED SECURITY PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "<TABLE BORDER=1><TR><TD>Patch ID</TD><TD>Ins Rev</TD><TD>Lat Rev</TD><TD>Age</TD><TD>Require ID</TD><TD>Incomp ID</TD><TD>Synopsis</TD></TR>\n"; \
} \
if ( $6 == "Incomp" || $_ ~ /Rev Rev/ ) \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>&nbsp;</TD><TD>",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>",$1,$2,$3,$4; \
for (i=5;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>%s</TD><TD>&nbsp;</TD></TR>\n",$1; \
} \
else \
if ( $1 !~ /^[0-9]/ && $6 != "Incomp" && $_ !~ /Rev Rev/ && $_ !~ /\-\-\-\-\-\-/ && $_ !~ /\=\=\=\=\=\=/ ) { print; printf "<BR>\n" } \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
printf "</TABLE>\n" >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED Y2K PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "<TABLE BORDER=1><TR><TD>Patch ID</TD><TD>Ins Rev</TD><TD>Lat Rev</TD><TD>Age</TD><TD>Require ID</TD><TD>Incomp ID</TD><TD>Synopsis</TD></TR>\n"; \
} \
if ( $6 == "Incomp" || $_ ~ /Rev Rev/ ) \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>&nbsp;</TD><TD>",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>",$1,$2,$3,$4; \
for (i=5;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>%s</TD><TD>&nbsp;</TD></TR>\n",$1; \
} \
else \
if ( $1 !~ /^[0-9]/ && $6 != "Incomp" && $_ !~ /Rev Rev/ && $_ !~ /\-\-\-\-\-\-/ && $_ !~ /\=\=\=\=\=\=/ ) { print; printf "<BR>\n" } \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
printf "</TABLE>\n" >> ${PATCHDIAG_TMP}.$TYPE
awk '/^INSTALLED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "<TABLE BORDER=1><TR><TD>Patch ID</TD><TD>Ins Rev</TD><TD>Lat Rev</TD><TD>Synopsis</TD></TR>\n"; \
} \
if ( $4 == "Synopsis" || $_ ~ /Revision Revision/ ) \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && NF > 2 && $_ !~ /CURRENT/ && $_ !~ /Obsoleted/) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>",$1,$2,$3; \
for (i=4;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 !~ /^[0-9]/ && $4 != "Synopsis" && $_ !~ /Revision Revision/ && $_ !~ /\-\-\-\-\-\-/ && $_ !~ /\=\=\=\=\=\=/ ) { print; printf "<BR>\n" } \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
printf "</TABLE>\n" >> ${PATCHDIAG_TMP}.$TYPE
awk '/^OTHER RELATED UNINSTALLED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /------/ ) \
{ printf "<TABLE BORDER=1><TR><TD>Patch ID</TD><TD>Flag</TD><TD>Ins Rev</TD><TD>Lat Rev</TD><TD>Synopsis</TD></TR>\n"; \
} \
if ( $5 == "Synopsis" || $3 == "Rev") \
{ printf ""; \
} \
if ( $1 ~ /^[0-9]/ && $2 == "R" && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && $2 == "S" && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 ~ /^[0-9]/ && $2 == "*" && $_ !~ /Obsoleted/ ) \
{ printf "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>",$1,$2,$3,$4,$5; \
for (i=6;i<NF;i++) printf "%s ",$i; \
printf "%s</TD></TR>\n",$NF \
} \
else \
if ( $1 !~ /^[0-9]/ && NF > 1 && $5 != "Synopsis" && $3 != "Rev" && $_ !~ /\-\-\-\-\-\-/ && $_ !~ /\=\=\=\=\=\=/ || $_ ~ /^$/ ) { print; printf "<BR>\n" } \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
printf "</TABLE>\n" >> ${PATCHDIAG_TMP}.$TYPE
printf "</BODY>\n" >> ${PATCHDIAG_TMP}.$TYPE
printf "</HTML>\n" >> ${PATCHDIAG_TMP}.$TYPE
if [ "$MAILTO" != "" ]; then
[ "$VERBOSE" = "TRUE" ] && echo "Mailing results to $MAILTO."
$DIRNAME/inlinemailx -s "Patch Check Complete for $HOSTNAME" -m $MAILTO < ${PATCHDIAG_TMP}.$TYPE
else
[ "$PATCHDIAG_OUT" = "" ] && cat ${PATCHDIAG_TMP}.$TYPE
fi
else
TYPE="txt"
[ "$VERBOSE" = "TRUE" ] && echo "Creating $TYPE."
head -1 ${PATCHDIAG_TMP} > ${PATCHDIAG_TMP}.$TYPE
awk '/System Name:/,/\=\=\=\=\=\=/' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/Report Note:/,/\=\=\=\=\=\=/' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED RECOMMENDED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 !~ /^[0-9]/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED SECURITY PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 !~ /^[0-9]/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/UNINSTALLED Y2K PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 ~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && NF > 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && NF == 1 && $5 !~ /^[0-9]/ && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 !~ /^[0-9]/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/^INSTALLED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /^[0-9]/ && NF > 2 && $_ !~ /CURRENT/ && $_ !~ /Obsoleted/) \
{ print } \
else \
if ( $1 !~ /^[0-9]/ || $_ ~ /^$/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
awk '/^OTHER RELATED UNINSTALLED PATCHES/,/\=\=\=\=\=\=/{ \
if ( $1 ~ /^[0-9]/ && $2 == "R" && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && $2 == "S" && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 ~ /^[0-9]/ && $2 == "*" && $_ !~ /Obsoleted/ ) \
{ print } \
else \
if ( $1 !~ /^[0-9]/ || $_ ~ /^$/ ) print \
}' ${PATCHDIAG_TMP} >> ${PATCHDIAG_TMP}.$TYPE
fi

if [ "$MAILTO" != "" -a "$TYPE" != "html" ]; then
[ "$VERBOSE" = "TRUE" ] && echo "Mailing results to $MAILTO."
mailx -s "Patch Check Complete for $HOSTNAME" $MAILTO < ${PATCHDIAG_TMP}.$TYPE
elif [ "$TYPE" != "html" -a "$PATCHDIAG_OUT" = "" ]; then
cat ${PATCHDIAG_TMP}.$TYPE
fi
if [ "$PATCHDIAG_OUT" != "" ]; then
cp ${PATCHDIAG_TMP}.$TYPE $PATCHDIAG_OUT
fi
rm ${PATCHDIAG_TMP}.$TYPE
}


#while getopts "h(help)m:(mailto):l(local-xref)wcto:(output-file):q(quiet)" OPTION
while getopts "hm:lwcto:q" OPTION
do
case ${OPTION} in
h)
usage
exit 0
;;
w)
OUTPUT_TYPE=html
;;
c)
OUTPUT_TYPE=csv
;;
t)
OUTPUT_TYPE=txt
;;
o)
PATCHDIAG_OUT=$OPTARG
;;
l)
USE_LOCAL_XREF=TRUE
XREF_LOCAL=${XREF_DEFAULT}
;;
q)
VERBOSE=FALSE
WGET_LOG_OPT="-o ${WGET_LOG}"
;;
m)
MAILTO=$OPTARG
;;
*)
OUTPUT_TYPE=txt
;;
esac
done

[ "$USE_LOCAL_XREF" = "FALSE" ] && file_checks
[ "$USE_LOCAL_XREF" = "FALSE" ] && get_xref_web
do_patchdiag
create_patch_output $OUTPUT_TYPE
rm $PATCHDIAG_TMP
[ "$USE_LOCAL_XREF" = "FALSE" ] && rm ${XREF_LOCAL}