
#!/bin/ksh
#--------------------------------------------------------------------------#
# Name: universal_parser_html.sh                                           #
# Description: Script that can parse csv to html output and percent to some#
#              kind of progressbars.                                       #
#              Assumption is that all percents have the same warning and   #
#              error tresholds ie: 80/95                                   #
# References:                                                              #
# Documentation:                                                           #
# Global Description:                                                      #
# Author(s): Atos                                                          #
# Change log:                                                              #
# aut   Date        Vers Comments                                          #
# MBOE  13-04-2014  1.0  Parse csv to html                                 #
#                                                                          #
# IF You put in a file containing the word HEADER it will be shown as an   #
# header, also column 8 and 11 are always treated as percent and progress  #
#--------------------------------------------------------------------------#
set +x
#set -x
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
#Thresholds for progressbars
HI=95
LO=80

#Default font settings and colors for progressbar
FONTNORMAL="<font face=arial size=2>"
FONTTITLE="<font face=arial size=1 color=white>"
RED="salmon"
ORANGE="orange"
GREEN="lightgreen"

INPUTFILE=""
OUTPUTFILE=""
PGM="`basename ${0}|cut -d\. -f1`"
VERSION="1.0"
TMP="/tmp/${PGM}.$$"
DATUM=`date '+%m-%d-%Y'`
DATADIR=${HOME}/data/out
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
MAXLOGSIZE=1024   # In K's
DATE=`date '+%Y-%m-%d'`
TICKS=`date +%s`
LOG="${HOME}/log/${PGM}.log"
THREADS=4
ERRORBG="white"
# ColorScheme
NOTOK="\e[91m"
OK="\e[92m"
NORMAL="\e[0m"
BOLD="\e[1m"
RH="0"
TOTALROWS=0
TITLE="No Title Given"
REDCOUNT=0
GREENCOUNT=0
ORANGECOUNT=0
ADDITIONALHEADER=0
PB="none"

# Functions
USAGE()
{
  echo "Usage: ${PGM} <options>"
  echo "  Version: ${VERSION}"
  echo "  options     :"
  echo "    -i|--input: Input CSV file"
  echo "    -o|--out  : Output file non CSV (${FILERS})"
  echo "    -h|--help : this Help"
  echo "    -t|--title: Add title to html file -t Report_Of_Everything"
  echo "    -a        : Additional header for error and warning counters"
  echo "    -r|--repeatheader : Repeat the header line every 30 rows"
  echo "    -v        : Verbose (gives some progres information)"
  echo "    -x        : set -x"
  echo "    -V        : show Version"
  echo ""
  echo " Please note that the delimiter always needs to be a [;]"
  echo " Please note that the first line is always an header"
  echo " Please note that lines containing the word HEADER are also treated as an header"
  echo " Also note that [&] is an illegal string in the text"
}

if [ ${#} -eq 0 ]; then
  echo "No option(s) given. So not to know what to do. Exiting..."; echo; USAGE;
  exit 1
fi
while [ ${#} -ge 1 ]
  do
  case ${1} in
    -i | --input) INPUTFILE=${2}; shift ;;
    -o | --outputfile) FILERS=${2}; shift ;;
    -t | --title) TITLE=${2}; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -r | --repeatheader); RH="1";;
    -a); ADDITIONALHEADER="1";;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -v) VERBOSE="1";;
    -x) set -x ;;
    *)  echo "Option ${1} not known. Exiting..."; echo; USAGE; exit 1 ;;
  esac
    shift
done

#echo "DATE=${DATE}"
#echo "FILE=${FILE}"
#echo "LOG=${LOG}"
#echo "PGM=${PGM}"
#echo "VERBOSE=${VERBOSE}"
#echo "VERSION=${VERSION}"
#echo "INPUTFILE=${INPUTFILE}"
#echo "OUTPUTFILE=${OUTPUTFILE}"

#  LOG rotating
touch ${LOG} ${TMP}
# Check & move LOG-file if longer then max.
LOGSIZE=`du -ka $LOG | cut -f1`
if [ $LOGSIZE -ge $MAXLOGSIZE ]; then
  mv $LOG $LOG.old
  touch $LOG
fi

#functions
DEL_FILE()
{
if [ -f ${1} ]; then
 #rm ${1}
 DEBUG "DEL_FILE $1 has been deleted" 1
else
 DEBUG "DEL_FILE $1 doesn't exist so nothing to delete" 2
fi
}

DEBUG()
{
#select color
color=msgnone.sh
  case ${2} in
    b) color=msginvert.sh ;;
    1 | OK) color=msggreen.sh ;;
    2 ) color=msgblue.sh ;;
    3 | NOTOK) color=msgred.sh ;;
    4 ) color=msgnone.sh ;;
  esac

RUNTIME=`date +%s`
RUNTIME2=`expr ${RUNTIME} - ${TICKS}`
#color folder contains files described in the case statements also the colors can be changed in those scripts
if [[ ${VERBOSE} -eq 1 ]]; then
./color/$color "$VERBOSE: `date '+%Y-%m-%d %H:%M:%S'`  ${RUNTIME2} ${PGM} ${1}"
fi
./color/$color "$VERBOSE: `date '+%Y-%m-%d %H:%M:%S'`  ${RUNTIME2} ${PGM} ${1}" | tee -a >> ${LOG}
 return
}


ROWHEADER()
{
output="<tr>"
for var in "$@"
do
    output="${output} <th bgcolor=grey>${FONTTITLE} $var </font></th>"
    header="${output} <th bgcolor=grey>${FONTTITLE} $var </font></th>"
done
output="${output} </tr>"
echo ${output} | sed 's/\&//g' >> ${TMP}
}

ROWNORMAL()
{
#make progressbar from column 8 and 11
#increment total rows to know when we reach 30 for a new header

if [ $RH -gt 0 ]; then #row header is not disabled, if row header is enabled the value is always greater then 0
  RH=`expr $RH + 1`
  #if rh is 30 repeat header and reset counter
  if [ $RH -gt 30 ]; then
    RH=1
    echo ${header} >> ${TMP}
  fi
fi

#check if it needs to be a header
if [ `echo $1 | grep -c HEADER` -eq 1 ]; then
      input=`echo "$@" | cut -d' ' -f2-`
      ROWHEADER $input
      return
fi

output="<tr>"
object=0
for var in "$@"
do
object=$((object+1))
      var=`echo $var | sed 's/\&//g'`
#if column 8 or 11 then create a pbar instead of simple text
    if [ ${object} -eq 8 ]; then
      PBAR $var
      output="${output} <td>$PB </font></td>"
    elif [ ${object} -eq 11 ]; then
      PBAR $var
      output="${output} <td>$PB </font></td>"
    else
      output="${output} <td>${FONTNORMAL} $var</font></td>"
    fi
done
output="${output} </tr>"
echo ${output} | sed 's/\&//g' >> ${TMP}
#echo "</tr>"
}

PBAR()
{
PCT=$1
PCTREST=$((100-PCT))
#create progress bar depending if value is higher then $HI or $LO change the color (bgcolor)
if [ ${PCT} -gt $HI ]; then
 #change the bg color of the entire row
 #ERRORBG="$RED"
 REDCOUNT=`expr $REDCOUNT + 1`
 PB="<table><tr><td width=$PCT bgcolor=$RED></td><td width=$PCTREST bgcolor=grey></td><td bgcolor=$RED>$FONTNORMAL$PCT%</td></tr></table>"
elif [ ${PCT} -gt $LO ]; then
 #ERRORBG="$ORANGE"
 ORANGECOUNT=`expr $ORANGECOUNT + 1`
 PB="<table><tr><td width=$PCT bgcolor=$ORANGE></td><td width=$PCTREST bgcolor=grey></td><td bgcolor=$ORANGE>$FONTNORMAL$PCT%</td></tr></table>"
else
 #ERRORBG="white"
 GREENCOUNT=`expr $GREENCOUNT + 1`
 PB="<table><tr><td width=$PCT bgcolor=$GREEN></td><td width=$PCTREST bgcolor=grey></td><td>$FONTNORMAL$PCT%</td></tr></table>"
fi
}

#Payload
#headers
DEL_FILE ${OUTPUTFILE}
DEL_FILE ${TMP}
echo "<html><title>${TITLE} - ${PGM} - `date` - ${VERSION} </font></title>"
#top headers
#HEADER 1 2 5 3
#cat ${INPUTFILE}
INPUT=`cat ${INPUTFILE} | head -1`
#echo ${INPUT}
line=0
echo "<table style="width:100%"><tr>${FONTTITLE}<td>" >> ${TMP}
while read p; do
  line=$((line + 1))
  STR=`echo $p | sed 's/ /\&/g'`
  STR=`echo $STR | sed 's/;/ /g'`
  #STR=`echo $p`
  if [ $line -eq 1 ]; then
 	ROWHEADER ${STR}
  else
   	ROWNORMAL ${STR}
  fi
done < ${INPUTFILE}
echo "</td></tr></table>" >> ${TMP}
echo "<br>" >> ${TMP}

#ADDITIONAL HEADER TIME AND COUNTERS OF GREEN RED ORANGE
if [ ${ADDITIONALHEADER} -eq 1 ]; then
	echo "<table>"
	echo "<tr><td bgcolor=grey>${FONTTITLE}. </font></td><td bgcolor=grey>${FONTTITLE} .</td></tr>"
	echo "<tr><td>${FONTNORMAL} Username </td><td>${FONTNORMAL} $USER </td></tr>"
	echo "<tr><td>${FONTNORMAL} Day and Time </td><td>${FONTNORMAL} `date` </td></tr>"
	echo "<tr><td>${FONTNORMAL} System Name </td><td>${FONTNORMAL} `hostname` </td></tr>"
	echo "<tr><td>${FONTNORMAL}</td><td>.</td></tr>"
	echo "<tr><td>${FONTNORMAL} High Value </td><td>${FONTNORMAL} ${HI} </td></tr>"
	echo "<tr><td>${FONTNORMAL} Low Value </td><td>${FONTNORMAL} ${LO} </td></tr>"
	echo "<tr><td>${FONTNORMAL}</td><td>.</td></tr>"
	echo "<tr><td>${FONTNORMAL} CRITICAL </td><td>${FONTNORMAL} $REDCOUNT </td></tr>"
	echo "<tr><td>${FONTNORMAL} WARNING </td><td>${FONTNORMAL} $ORANGECOUNT </td></tr>"
	echo "<tr><td>${FONTNORMAL} NORMAL </td><td>${FONTNORMAL} $GREENCOUNT </td></tr>"
	echo "</table>"
	echo "<br>"
fi
cat ${TMP}
#Add some footer
echo "<font size=0 face=arial color=grey align=right><i>Copyright ATOS Devision BTN NL MS 2015</i></font>" 

