#!/bin/sh
#-------------------------------------------------------------------------------
# charon_showhostinfo
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2022 STROMASYS.
# All rights reserved.
#
#set -x
VERSION=1.40
#TODO (med): update the tables with HTML5 / CSS code (same as class=cfglog)

for P in `ls /etc/profile.d/charon* 2>/dev/null`;do . $P;done
if test -e `dirname $0`/charon_common
then
  . `dirname $0`/charon_common
  OUTOFLTK=0
else
  CHARONDIR=/opt/charon
  OUTOFLTK=1
fi
MAILFRFILE="${CHARONDIR}/utils/charon_check.mailfrom"

HL=""
IS_HTML=0
test "$1" = "-html" && IS_HTML=1

function Echo
{
  if test ${IS_HTML} = 0
  then
    echo "$1" "$2"
  else
    echo "$1 $2" >>/tmp/charon_showhostinfo.htm
  fi
}

function Usage
{
  cat <<EOF

`tput bold``basename $0``tput sgr0` (version ${VERSION})
    This script collects information on the CHARON server running on Linux
    `tput smul`Note`tput rmul`: must be root to execute the script

`tput bold`Parameters`tput sgr0`
    none           - Standard mode with enhanced display (full)
    -lite          - Lite mode, pure text (full)
    -html          - HTML mode. It is recommended then to route the display
                     to a file for reading with a browser (full)
    -mail=<to>     - creates an HTML file then send it to the mail recipient
                     Name and Company are then requested (full)
    -hostos        - displays only Hostname and Operating System information
    -hardware      - displays only Hardware information
    -processes     - displays only processes information & OOM settings
    -storage       - displays only Storage information
    -network       - displays only Network information
    -charonversion - displays only CHARON programs and packages information
    -charontoolkit - displays only CHARON utilies and services information

`tput bold`Example`tput sgr0`
    # $0 -mailto=spock@uss-enterprise.fed

EOF
}

function Title
{
  # $1 = Original $1
  # $2 = Level, 1 or 2
  # $3 = Title
  # $4 = Message to display on screen during execution
  if test "$1" != "-html"
  then
    Echo
    ${BOLD}
    TLV=$2
    TIT=$3
    if test -z "$3"
    then
      TLV=$1
      TIT=$2
      test "${TLV}" = "2" && Echo -e "\033[36m"
    fi      
    Echo "${TIT}"
    if test "$1" = "-lite" -a "${TLV}" = "2"
    then
      TLN=`echo ${TIT} | wc -c`
      Echo "-------------------------------------------------------------------------------" | cut -c2-${TLN}
    fi
    ${SGR0}
  else
    echo
    Echo "<br>"
    if test "$2" = "1"
    then
      Echo "<a name='$3'></a><table border='1' width='100%'>"
      Echo "<tr><td align=left bgcolor=#3AAADC>"
      Echo "<font color='#FFFFFF'><b>$3</b></font>"
      Echo "</td></tr>"
      Echo "</table>"
      tput bold
    else
      Echo "<a name='$3'></a><table border='0' width='100%'>"
      Echo "<tr><td align=left bgcolor=#00A096>"
      Echo "<font color='#FFFFFF'>$3</font>"
      Echo "</td></tr>"
      Echo "</table>"
      echo -n "  "
    fi
    Echo "</font></font>"
    if test -z "$4"
    then
      echo -n "- $3"
    else
      echo -n "- $3 : $4"
    fi
    tput sgr0
  fi
}


function collect_hostos
{
  #-----------------------------------------------------------------------------
  # Hostname and Operating system
  #-----------------------------------------------------------------------------
  Title $1 1 "Hostname"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)
  test "$1" = "-html" && Echo "<font style='font-family:monospace;'><br>"
  Echo `hostname`
  test "$1" = "-html" && Echo "</font><br>"
  Echo 
  test "$1" = "-html" && Echo "<br>"
  
  Title $1 1 "Operating system"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)
  test "$1" = "-html" && Echo "<font style='font-family:monospace;'><br>"
  OSR=`cat ${OS_RELEASE}`
  Echo "${OSR}"
  test "$1" = "-html" && Echo "<br>"
  SR="`uname -s` `uname -r`"
  Echo "${SR}"
  test "$1" = "-html" && Echo "<br>"
  Echo "Uptime: `uptime`"
  test "$1" = "-html" && Echo "</font><br>"
  Echo
  test "$1" = "-html" && Echo "<br>"
}

function collect_hardware
{
  #-----------------------------------------------------------------------------
  # Hardware
  #-----------------------------------------------------------------------------
  Title $1 1 "Hardware"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Memory"

  test "$1" = "-html" && Echo "<br><font style='font-family:monospace;'>"
  if test "$1" = "-html"
  then
    /usr/bin/free | sed "s= =\&nbsp;=g" | while read LINE
    do
      Echo "${LINE}<br>"
    done
    Echo
    Echo "</font><br>"  
    Echo "<br>"
  else
    Echo ""
    /usr/bin/free
  fi

  Title $1 2 "CPU"

  test "$1" = "-html" && Echo "<font style='font-family:monospace;'>"
  grep -e ^processor -e "^model name" /proc/cpuinfo | awk -F: '{print $2}' | while read LINE
  do
    P=`printf "%2d" ${LINE}`
    read LINE
    Echo "Processor ${P}: ${LINE}"
    test "$1" = "-html" && Echo "<br>"
  done
  test "$1" = "-html" && Echo "</font><br>"

  Title $1 2 "USB devices"

  test "$1" = "-html" && Echo "<br><font style='font-family:monospace;'>"
  /usr/bin/lsusb | while read LINE
  do
    Echo "${LINE}"
    test "$1" = "-html" && Echo "<br>"
  done
  test "$1" = "-html" && Echo "</font><br>"

  Title $1 2 "HASP USB devices"

  if test "$1" = "-html"
  then
    Echo "<br><font style='font-family:monospace;'>"
    /usr/bin/lsusb -d 0529:0001 -v | grep -e ^Bus -e id*Product | sed -e "s= =\&nbsp;=g" | while read LINE
    do
      Echo "${LINE}"
      Echo "<br>"
    done
    Echo "</font><br>"
  else
    /usr/bin/lsusb -d 0529:0001 -v | grep -e ^Bus -e id*Product
  fi
  Echo
}

function collect_storage
{
  #-----------------------------------------------------------------------------
  # Storage
  #-----------------------------------------------------------------------------
  Title $1 1 "Storage"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)

  Title $1 2 "Block devices (lsblk)"
  if test "$1" = "-html"
  then
    Echo "<br>"
    Echo "<font style='font-family:monospace;'>"
    Echo "<font color='#3aaadc'><b>"
    Echo `lsblk -i | head -1 | sed -e "s= =\&nbsp;=g"`
    Echo "</b></font><br>"
    lsblk -i | grep -v "$NAME.*MOUNTPOINT$" | sed -e "s= =\&nbsp;=g" | sed -e "s=|-=\&#9500;\&#9472;=g" -e "s=\`-=\&#9492;\&#9472;=g" | sed "s=$=<br>=g" | while read LSB
    do
      Echo "${LSB}"
    done
    Echo "</font><br>"
  else
    if test "$1" = "-lite"
    then
      lsblk -i
    else
      lsblk
    fi
  fi
  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "SCSI devices (lsscsi)"
  if test "$1" = "-html"
  then
    Echo "<br>"
    Echo "<font style='font-family:monospace;'>"
    lsscsi -g | sed -e "s= =\&nbsp;=g" | sed "s=$=<br>=g" | while read LSB
    do
      Echo "${LSB}"
    done
    Echo "</font><br>"
  else
    lsscsi -g
  fi
  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "File systems"
  if test "$1" = "-html"
  then
    Echo "<br>"
    typeset -i I=0
    df -h | sed "s= =\&nbsp;=g" | while read LINE
    do
      I=I+1
      test ${I} -eq 1 && Echo "<font color='#3aaadc'><b>"
      Echo "<font style='font-family:monospace;'>"
      Echo "${LINE}"
      Echo "</font><br>"
      test ${I} -eq 1 && Echo "</b></font>"
    done
  else
    df -h
  fi
  Echo
  test "$1" = "-html" && Echo "<br>"
}

function collect_network
{
  #-----------------------------------------------------------------------------
  # Network
  #-----------------------------------------------------------------------------
  Title $1 1 "Network"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)

  Title $1 2 "ifconfig"

  if test "$1" = "-html"
  then
    Echo "<br><table border='1'>"
    Echo "<tr>"
    Echo "<td bgcolor='#3aaadc'>Interface</font></td>"
    Echo "<td bgcolor='#3aaadc'>Settings</font></td>"
    Echo "</tr>"
    CONCAT=""
    ifconfig -a | sed -e "s=^ =#=g" -e "s=^$=.=g" | while read LINE
    do
      case "`echo ${LINE} | cut -c1`"
      in
        ".")
          Echo "<td><font style='font-family:monospace;'>${CONCAT}</font></td>"
          Echo "</tr>"
          ;;
        "#")
          LINE=`echo ${LINE} | sed "s=^#==g"`
          CONCAT="${CONCAT}<br>${LINE}"
          ;;
        *)
          Echo "<tr>"
          EL1=`echo ${LINE} | awk '{print $1}'`
          Echo "<td valign=top><font style='font-family:monospace;'>${EL1}</font></td>"
          LINE=`echo ${LINE} | sed "s=^${EL1}==g"`
          CONCAT="${LINE}"
          ;;
      esac
    done
    Echo "</table>"
  else
    ifconfig -a
  fi
  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Network interfaces configuration files"

  grep ':' /proc/net/dev | while read LINE
  do
    NETINT=`echo ${LINE} | awk -F ':' '{print $1}' | tr -d ' '`
    IFCFGFILE=/etc/sysconfig/network-scripts/ifcfg-${NETINT}
    if test -e ${IFCFGFILE}
    then
      if test "$1" = "-html"
      then
        Echo "<br><table border='1'>"
        Echo "<tr><td bgcolor='#3aaadc'>${IFCFGFILE}</font>"
        Echo "</td></tr>"
        Echo "<tr><td>"
        cat ${IFCFGFILE} | sed -e "s= =\&nbsp=g" -e "s=$=<br>=g" | while read LC
        do
          test "`echo ${LC} | cut -c1`" = "#" && Echo "<font color=#a0a0a0>"
          Echo "<font style='font-family:monospace;'>${LC}</font>"
          test "`echo ${LC} | cut -c1`" = "#" && Echo "</font>"
        done
        Echo "</td></tr>"
        Echo "</table><br>"
      else
        ${BOLD}
        Echo "- ${IFCFGFILE}"
        ${SGR0}
        cat ${IFCFGFILE}
        Echo
      fi
    fi
  done
  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "netstat -r"

  if test "$1" = "-html"
  then
    Echo "</a><br>"
    Echo "<font style='font-family:monospace;'>"
    netstat -r | sed "s= =\&nbsp;=g" | while read N
    do
      Echo "${N}<br>"
    done
    Echo "</font>"
  else
    netstat -r
  fi
  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "ncu configuration"

  if test "$1" = "-html"
  then
    Echo "</a><br>"
    Echo "<font style='font-family:monospace;'>"
    Echo "<b>ncu -l</b><br>"
    which ncu >/dev/null 2>&1
    if test $? = 0
    then
      ncu -l | sed -e "s= =\&nbsp;=g" | while read N
      do
        Echo "${N}<br>"
      done
    else
      Echo "ncu is not installed."
    fi
    Echo "</font><br>"
    Echo "<font style='font-family:monospace;'>"
    Echo "<b>chkconfig --list ncusrv</b><br>"
    chkconfig --list ncusrv 2>/dev/null | while read N
    do
      Echo "${N}<br>"
    done
    test $? != 0 && Echo "Failed."
    Echo "</font><br>"
    Echo "<font style='font-family:monospace;'>"
    Echo "<b>Content of: /usr/lib/systemd/system/ncu.service</b><br>"
    if test -s /usr/lib/systemd/system/ncu.service
    then 
      cat /usr/lib/systemd/system/ncu.service | while read N
      do
        Echo "${N}<br>"
      done
    else
      Echo "File not found. Service definition problem. Please check.<br>"
    fi
    Echo "</font><br>"
  else
    Echo "ncu -l"
    which ncu 2>/dev/null
    if test $? = 0
    then
      ncu -l
    else
      Echo "ncu is not installed."
    fi
    Echo
    Echo "chkconfig --list ncusrv"
    chkconfig --list ncusrv
    test $? != 0 && Echo "Failed."
    Echo
    Echo "Content of: /usr/lib/systemd/system/ncu.service"
    if test -s /usr/lib/systemd/system/ncu.service
    then 
      cat /usr/lib/systemd/system/ncu.service
    else
      Echo "File not found. Service definition problem. Please check."
    fi
    Echo
  fi
  Echo 
  test "$1" = "-html" && Echo "<br>"
}

function collect_processes
{
  #-----------------------------------------------------------------------------
  # Processes
  #-----------------------------------------------------------------------------
  Title $1 1 "Processes"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Top processes"

  if test "$1" = "-html"
  then
    Echo "<br><font style='font-family:monospace;'>"
    /usr/bin/top -b -n 1 | sed "s= =\&nbsp;=g" | while read LINE
    do
      Echo "${LINE}<br>"
    done
    Echo
    Echo "</font><br>"  
    Echo "<br>"
  else
    /usr/bin/top -b -n 1
  fi

  Title $1 2 "Top memory consumers"

  if test "$1" = "-html"
  then
    Echo "<br><font style='font-family:monospace;'>"
    Echo "<font color='#3aaadc'><b>"
    Echo `/bin/ps aux | head -1 | sed -e "s= =\&nbsp;=g"`
    Echo "</b></font><br>"
    /bin/ps aux | sort -n -k 5,6 | tail -20 | sort -nr -k 5,6 | sed "s= =\&nbsp;=g" | while read LINE
    do
      Echo "${LINE}<br>"
    done
    Echo
    Echo "</font><br>"  
    Echo "<br>"
  else
    /bin/ps aux | head -1
    /bin/ps aux | sort -n -k 5,6 | tail -20 | sort -nr -k 5,6
  fi

  Title $1 2 "OOM Killer settings"

  get_preferences 2>/dev/null
  if test "$1" = "-html"
  then
    Echo "<br><font style='font-family:monospace;'>"
    Echo "Charon VM process killing prevention settings (from preferences):<br><b>"
    if test "${OOMPRVPIDKILL}" = "enabled"
    then
      Echo "<font color='#3ea650'>"
    else
      Echo "<font color='#e95f40'>"
    fi
    Echo "${OOMPRVPIDKILL}</font></b><br></font><br>"
    Echo "<font style='font-family:monospace;'>"
    Echo "<b>vm.overcommit_memory values</b>:<br>"
    Echo "<li>0 = Heuristic overcommit handling</li>"
    Echo "<li>1 = Always overcommit</li>"
    Echo "<li>2 = Don't overcommit</li>"
    Echo "<font color='#3aaadc'><b>"
    Echo "`/sbin/sysctl -q vm.overcommit_memory`"
    Echo "</b></font></font><br>"
    Echo "<br>"
    Echo "<font style='font-family:monospace;'>"
    Echo "<b>vm.panic_on_oom values</b>:<br>"
    Echo "<li>0 = Disabled</li>"
    Echo "<li>1 = Enabled</li>"
    Echo "<font color='#3aaadc'><b>"
    Echo "`/sbin/sysctl -q vm.panic_on_oom`"
    Echo "</b></font></font><br>"
  else
    Echo "Charon VM process killing prevention settings (from preferences): ${OOMPRVPIDKILL}"
    Echo
    Echo "vm.overcommit_memory values:"
    Echo "0 = Heuristic overcommit handling"
    Echo "1 = Always overcommit"
    Echo "2 = Don't overcommit"
    Echo
    /sbin/sysctl -q vm.overcommit_memory
    Echo
    Echo "vm.panic_on_oom values:"
    Echo "0 = Disabled"
    Echo "1 = Enabled"
    Echo 
    Echo "`/sbin/sysctl -q vm.panic_on_oom`"
    Echo
  fi
}

function collect_charonversion
{
  #-----------------------------------------------------------------------------
  # Charon Product
  #-----------------------------------------------------------------------------
  Title $1 1 "Charon Product"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)
  
  #--- For Exe, look inside directory as there can be saved files with other versions
  #--- than the one installed
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Programs found"

  test "$1" = "-html" && Echo "<br><font style='font-family:monospace;'>"
  if test `find ${CHARONDIR}/bin/* -type l 2>/dev/null|wc -l` -eq 0
  then
    Echo "Not found."
    test "$1" = "-html" && Echo "<br>"
  else
    find ${CHARONDIR}/bin/* -type l | while read L
    do
      VER=`strings ${L} | grep ^CHARON | grep Version | sed "s= (\%s)==g"`
      test -z "${VER}" && VER=`strings ${L} | grep ^CHARON | grep Linux`
      test -z "${VER}" && VER=`file -b ${L}`
      test "$1" = "-html" && Echo "<font color=#3e3e40>"
      if test -n "`ps -ef | grep ${L} | grep -v grep`"
      then
        if test "$1" = "-html"
        then
          Echo "${L}, ${VER} (<font color='#00A000'><b>running</font></b>)"
        else
          Echo "${L}, ${VER} (running)"
        fi
      else
        Echo "${L}, ${VER}"
      fi
      test "$1" = "-html" && Echo "</font><br>"
    done
  fi
  test "$1" = "-html" && Echo "</font>"

  #--- For aksusb, python and Django, use 'rpm -q -a'
  Title $1 2 "Packages installed"

  test "$1" = "-html" && Echo "<br><font color=#3e3e40 style='font-family:monospace;'>"
  typeset -i N=0
  rm /tmp/charon_showhostinfo.rpm.$$ 2>/dev/null
  rpm -q -a | grep -e aksusb -e Django -e python-psutil -e ^charon | sort | while read LINE
  do
    touch /tmp/charon_showhostinfo.rpm.$$
    Echo "${LINE}"
    test "$1" = "-html" && Echo "<br>"
  done
  if test -e /tmp/charon_showhostinfo.rpm.$$
  then
    rm /tmp/charon_showhostinfo.rpm.$$ 2>/dev/null
  else
    ${SGR0}
    test "$1" = "-html" && Echo "<font style='font-family:monospace;'>"
    Echo "There is no Charon package installed."
    test "$1" = "-html" && Echo "</font><br>"
  fi
  test "$1" = "-html" && Echo "</font>"

  #--- Display aksusb daemons status
  Title $1 2 "AKSUSB daemons status"

  if test "$1" = "-html"
  then
    Echo "<br><font style='font-family:monospace;'><font color=#3e3e40>"
    systemctl -l --lines=20 status aksusbd.service 2>&1 | sed -e "s=├─=\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&#9500;\&#9472;=g" -e "s=└─=\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&#9492;\&#9472;=g" -e "s=●=\&diams;=g" | while read LINE
    do
      Echo "${LINE}<br>"
    done
  else
    systemctl -l --lines=20 status aksusbd.service 2>&1
  fi
  test "$1" = "-html" && Echo "</font>"

  #--- Display PATH
  Title $1 2 "PATH"

  test "$1" = "-html" && Echo "<br><font color=#3e3e40><font style='font-family:monospace;'>"
  Echo "PATH=${PATH}"
  test "$1" = "-html" && Echo "</font><br>"

  Echo
  test "$1" = "-html" && Echo "<br>"

  #--- Display license contents
  Title $1 2 "License contents"

  test "$1" = "-html" && Echo "<br><font color=#3e3e40><font style='font-family:monospace;'>"
  OPTHASP=""
  HASPEXE=`which hasp_srm_view 2>/dev/null`
  if test -z "${HASPEXE}"
  then
    Echo "Cannot find 'hasp_srm_view' executable file."
    test "$1" = "-html" && Echo "<br>"
    Echo "Please verify CHARON is installed and PATH is correctly set."
    test "$1" = "-html" && Echo "<br>"
  else
    test -n "`strings ${HASPEXE} | grep ^-all`" && OPTHASP="-all"

    OUTVIEW="/tmp/`basename $0`_`date +%Y%m%d_%H%M%S`.view"
    touch ${OUTVIEW}
    hasp_srm_view ${OPTHASP} >${OUTVIEW} 2>&1
    # if test -n "`grep 'key not found' ${OUTVIEW}`"
    if test -z "`grep 'License type:' ${OUTVIEW}`"
    then
      echo "hasp_srm_view ${OPTHASP} >${OUTVIEW} 2>&1" | at now >${OUTVIEW}.tmp 2>&1
      ATNUM=`grep ^job ${OUTVIEW}.tmp | awk '{print $2}'`
      rm -f ${OUTVIEW}.tmp
      typeset -i N=0
      while test 1
      do
        N=N+1
        sleep 2
        test -z "`at -l | grep -w ^${ATNUM}`" && break
        if test $N -gt 30
        then
          >${OUTVIEW}
          break
        fi
      done
    fi

    if test "$1" = "-html"
    then
      cat ${OUTVIEW} | sed "s=\r==g" | while read LO
      do
        CHKLIC=`echo ${LO} | grep "^The License Number"`
        test -n "${CHKLIC}" && Echo "<b>"
        Echo "${LO}<br>"
        test -n "${CHKLIC}" && Echo "</b>"
      done
    else
      cat ${OUTVIEW} | sed "s=\r==g"
    fi
  fi
  test "$1" = "-html" && Echo "</font><br>"

  Echo
  test "$1" = "-html" && Echo "<br>"
}

function collect_charontoolkit
{
  #-----------------------------------------------------------------------------
  # Toolkit version and settings
  #-----------------------------------------------------------------------------
  Title $1 1 "Toolkit"

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)

  Echo
  test "$1" = "-html" && Echo "<br>"
  ${BOLD}

  if test ${OUTOFLTK} = 1
  then
    ${SGR0}
    test "$1" = "-html" && Echo "<font style='font-family:monospace;'>"
    Echo "Not applicable, kit not installed."
    test "$1" = "-html" && Echo "</font><br>"
    return
  fi

  KITV=`cat ${CHARONDIR}/utils/kit.version 2>/dev/null` 
  test -n "${KITV}" && KITV=`echo ${KITV}|sed "s=\(^.*\);\(.*$\)=\1 (\2)=g"`

  Title $1 2 "Kit contents and versions"

  test "$1" = "-html" && Echo "<font style='font-family:monospace;'>"
  Echo "Version: ${KITV:-UNKNOWN}"

  if test "$1" = "-html"
  then
    Echo "<br><br><table border='1' style='font-family:monospace;'>"
    Echo "<tr>"
    Echo "<td bgcolor='#3aaadc'>Script</font></td>"
    Echo "<td bgcolor='#3aaadc'>Version</font></td>"
    Echo "</tr>"
  fi
  grep ^VERSION= ${CHARONDIR}/utils/* 2>/dev/null | grep :VERSION= | grep -v "~:" | sort | while read LINE
  do
    if test "$1" = "-html"
    then
      Echo "<tr><td>"
      V=`echo ${LINE} | cut -f1 -d':'`
      Echo "${V}"
      Echo "</td><td>"
      V=`echo ${LINE} | cut -f2 -d'='`
      Echo "${V}"
      INITFIL=`echo ${LINE} | cut -f1 -d':'`
      Echo "</td></tr>"
    else
      printf "`echo ${LINE} | cut -f1 -d':'` - `echo ${LINE} | cut -f2 -d':'`"
      INITFIL=`echo ${LINE} | cut -f1 -d':'`
      INITFIL="/etc/init.d/`basename ${INITFIL}`"
      if test -e ${INITFIL}
      then
        INITVER=`grep ^VERSION= ${INITFIL} | cut -f2 -d'='`
        Echo " (/etc/init.d version is ${INITVER})"
      else
        Echo
      fi
    fi
  done

  if test -e ${CHARONDIR}/utils/menu_mod.pl
  then
    if test -x ${CHARONDIR}/utils/menu_mod.pl
    then
      if test "$1" = "-html"
      then
        Echo "<tr><td>"
        Echo "${CHARONDIR}/utils/menu_mod.pl"
        Echo "</td><td>"
        Echo `${CHARONDIR}/utils/menu_mod.pl -v | head -1 | cut -f2 -d '='`
        Echo "</td></tr>"
      else
        printf "${CHARONDIR}/utils/menu_mod.pl - "
        ${CHARONDIR}/utils/menu_mod.pl -v | head -1
      fi
    else
      if test "$1" = "-html"
      then
        Echo "<tr><td>${CHARONDIR}/utils/menu_mod.pl</td>"
        Echo "<td>not executable!</td></tr>"
      else
        Echo "${CHARONDIR}/utils/menu_mod.pl - not executable!"
      fi
    fi
  else
    if test "$1" = "-html"
    then
      Echo "<tr><td>${CHARONDIR}/utils/menu_mod.pl</td>"
      Echo "<td>not found!</td></tr>"
    else
      Echo "${CHARONDIR}/utils/enu_mod.pl - not found"
    fi
  fi
  test "$1" = "-html" && Echo "<br>"

  if test -e ${CHARONDIR}/utils/guest_shutdown.exp
  then
    if test -x ${CHARONDIR}/utils/guest_shutdown.exp
    then
      INITVER=`grep ^#VERSION= ${CHARONDIR}/utils/guest_shutdown.exp|tr -d '#'`
      if test "$1" = "-html"
      then
        Echo "<tr><td>"
        Echo "${CHARONDIR}/utils/guest_shutdown.exp"
        Echo "</td><td>"
        Echo ${INITVER:-version not found} | cut -f2 -d'='
        Echo "</td></tr>"
      else
        printf "${CHARONDIR}/utils/guest_shutdown.exp - "
        Echo ${INITVER:-1=version not found}
      fi
    else
      if test "$1" = "-html"
      then
        Echo "<tr><td>"
        Echo "${CHARONDIR}/utils/guest_shutdown.exp"
        Echo "</td><td>"
        Echo "not executable!"
        Echo "</td></tr>"
      else
        Echo "${CHARONDIR}/utils/guest_shutdown.exp - not executable!"
      fi
    fi
  else
    if test "$1" = "-html"
    then
      Echo "<tr><td>"
      Echo "${CHARONDIR}/utils/guest_shutdown.exp"
      Echo "</td><td>"
      Echo "not found!"
      Echo "</td></tr>"
    else
      Echo "${CHARONDIR}/utils/guest_shutdown.exp - not found"
    fi
  fi
  test "$1" = "-html" && Echo "</table><br>"

  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Virtual machines list and status"

  if test "$1" = "-html"
  then
    Echo "<br><br><font style='font-family:monospace;'>"
    /opt/charon/utils/charon_cmdline vmlist | sed -e "s=\[m=</b>=g" -e "s=\[0m=</b>=g" -e "s=\[1m=<b>=g" -e "s=\[31m=<b>=g" -e "s=\[.m==g" -e "s=\[..m==g" -e "s=(0l(B=\&#9484=g" -e "s=(0x(B=\&#9474=g" -e "s=(0m(B=\&#9492=g" -e "s=(0==g" -e "s=(B==g" -e "s=\[..G= =g" -e "s=\[K==g" -e "s=$=<br>=g" -e "s=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqq qqqqqq qqqqqqqqqqqqqqqqqqqqqqqq<br>=<hr>=g" -e "s= =\&nbsp;=g" | while read N
    do
      Echo "${N}"
    done
    Echo "</font>"
  else
    if test "$1" = "-lite"
    then
      /opt/charon/utils/charon_cmdline vmlist | sed -e "s=\[m==g" -e "s=\[0m==g" -e "s=\[1m==g" -e "s=\[31m==g" -e "s=\[.m==g" -e "s=\[..m==g" -e "s=(0l(B=+=g" -e "s=(0x(B=|=g" -e "s=(0m(B=+=g" -e "s=(0==g" -e "s=(B==g" -e "s=\[..G= =g" -e "s=\[K==g" -e "s=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqq qqqqqq qqqqqqqqqqqqqqqqqqqqqqqq==g"
    else
      /opt/charon/utils/charon_cmdline vmlist
    fi
  fi
  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Configuration files"

  test "$1" = "-html" && Echo "<br><br>"

  cat <<EOF | while read F
/opt/charon/utils/charon_gstart.boot;Managed CHARON virtual machines: emulator exe, configuration file, start at boot Y/N
/opt/charon/utils/charon_gstart.stop;Clean shutdown script executed at service stop
/opt/charon/utils/charon_gstart.chkrun;Optional script executed when displaying running virtual machines to gather information from the Tru64 or OpenVMS system
/opt/charon/utils/charon_gstart.prestart;Optional script executed before the virtual machine starts
/opt/charon/utils/charon_logchk.list;Monitored CHARON virtual machines log files
/opt/charon/utils/charon_check.mailto;Recipients list for email alerts
/opt/charon/utils/charon_check.mailfooter;Mail footer added to email alerts
/opt/charon/utils/charon.preferences;File containing the preferences (optional)
/root/.charonmailmode;Mode used to send alert emails, TEXT or HTML (default)
/root/.charonwalloff;Exists if the wall messages are disabled
/root/.charon.expchk.nodongle;Exists if nodongle is detected to prevent from sending license expiration alerts
/root/.charon.expiration.alertsfromlog;If exists sends expiration alerts from the log file too and not only from the license expiration check utility running once (by default) a day
/root/.charonlogevent;Log message level generating an alert: 0 = None, 1 = INFORMATION + WARNING + ERROR, 2 = WARNING + ERROR, 3 = ERROR
/root/.charoncronNOcheck;File that can be created manually to prevent from checks in the crontab entries when using the Manage recursive jobs option
/root/.charonlicfolder;File containing the folder name used to contain the license files (.v2c)
/root/.charonkitfolder;File containing the folder name used to store the CHARON installation kits (with Install/Upgrade/Remove CHARON menu option)
/root/.charonsoftlicfolder;File containing the folder name used to store the software license kit (optional)
/root/.charon.hwspecwarning.ignore;Hardware specifications warning messages ignored (yes if file exists)
/root/.charon.less.warn.on.dongle.removal;Reduces the number of warning messages sent on dongle removal for V4.9 and above (yes if file exists)
/root/.charon.disable.monusb.immediate.alert;Disable the immediate alerts from the AKSUSB monitoring (yes if file exists)
/root/.telnetrc;telnet configuration file
EOF
  do
    FDESC=`echo $F | cut -f2 -d';'`
    F=`echo $F | cut -f1 -d';'`

    if test "$1" = "-html"
    then
      Echo "<b><font color=#0a71b4>File:</font></b> <font color=#0062ab>${F}</font><br>"
      Echo "<b><font color=#0a71b4>Description:</font></b> <font color=#0062ab>${FDESC}</font><br>"
      Echo "<table border='1'><tr><td>"
    else
      Echo "File: ${F}"
      Echo "Description: ${FDESC}"
    fi

    #test -z "`echo ${F} | grep '/root/'`" && F="${CHARONDIR}/utils/${F}"
    if test -e ${F}
    then
      if test -s ${F}
      then
        if test "$1" = "-html"
        then
          cat ${F} | sed -e "s= =\&nbsp=g" -e "s=$=</font><br>=g" -e "s=;=\&#59;=g" | while read LF
          do
            Echo "<font style='font-family:monospace;'>"
            test "`echo ${LF} | cut -c1`" = "#" && Echo "<font color=#a0a0a0>"
            Echo ${LF}
            Echo "</font>"
          done
        else
          cat ${F}
        fi
      else
        test "$1" = "-html" && Echo "<font style='font-family:monospace;'><font color=#a0a0a0>"
        Echo "Empty."
        test "$1" = "-html" && Echo "</font><br>"
      fi
    else
      test "$1" = "-html" && Echo "<font style='font-family:monospace;'><font color=#a0a0a0>"
      Echo "Does not exist."
      test "$1" = "-html" && Echo "</font><br>"
    fi
    test "$1" = "-html" && Echo "</font></td></tr></table>"
    Echo
    test "$1" = "-html" && Echo "<br>"
  done

  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Services"

  test "$1" = "-html" && Echo "<br>"
  
  if test -s ${CHARONDIR}/utils/charon_gstart.boot
  then
    cat ${CHARONDIR}/utils/charon_gstart.boot | while read L
    do
      LEXE=`echo ${L} | cut -f1 -d';'`
      LCFG=`echo ${L} | cut -f2 -d';'`
      LCFGBAS=`basename ${LCFG}|sed "s=\.cfg==g"`
      if test -s /etc/systemd/system/charon_${LCFGBAS}.service
      then
        if test "$1" = "-html"
        then
          Echo "<br>"
          Echo "<b>Executable:</b> ${LEXE}<br>"
          Echo "<b>Config file:</b> ${LCFG}<br>"
          Echo "<br><table border='1'>"
          Echo "<tr>"
          Echo "<td bgcolor='#3aaadc'>charon_${LCFGBAS}.service</td>"
          Echo "<td bgcolor='#3aaadc'>charon_logmon_${LCFGBAS}.service</td>"
          Echo "</tr>"
          Echo "<tr valign='top'>"
          Echo "<td><font style='font-family:monospace;'>"
          Echo "<b>File:</b><br>"
          cat /etc/systemd/system/charon_${LCFGBAS}.service | while read N
          do
            Echo "${N}<br>"
          done
          Echo "</font></td>"
          Echo "<td bgcolor='#f0f0f0'><font style='font-family:monospace;'>"
          if test -s /etc/systemd/system/charon_logmon_${LCFGBAS}.service
          then
            Echo "<b>File:</b><br>"
            cat /etc/systemd/system/charon_logmon_${LCFGBAS}.service | while read N
            do
              Echo "${N}<br>"
            done
          else
            Echo "Not defined"
          fi
          Echo "</font></td>"
          Echo "</tr>"
          Echo "<tr valign='top'>"
          Echo "<td><font style='font-family:monospace;'>"
          Echo "<b>Status:</b><br>"
          systemctl -l --lines=20 status charon_${LCFGBAS}.service 2>&1 | sed -e "s=├─=\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&#9500;\&#9472;=g" -e "s=└─=\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&#9492;\&#9472;=g" -e "s=$=<br>=g" -e "s=●=\&diams;=g"| while read N
          do
            Echo "${N}"
          done
          Echo "</font></td>"
          Echo "<td bgcolor='#f0f0f0'><font style='font-family:monospace;'>"
          Echo "<b>Status:</b><br>"
          systemctl -l --lines=20 status charon_logmon_${LCFGBAS}.service 2>&1 | sed -e "s=├─=\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&#9500;\&#9472;=g" -e "s=└─=\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&nbsp;\&#9492;\&#9472;=g" -e "s=$=<br>=g" -e "s=●=\&diams;=g"| while read N
          do
            Echo "${N}"
          done
          Echo "</font></td>"
          Echo "</tr>"
          Echo "</table>"
        else
          Echo
          Echo "Executable:  ${LEXE}"
          Echo "Config file: ${LCFG}"
          ${BOLD}
          Echo "Service file:"
          ${SGR0}
          cat /etc/systemd/system/charon_${LCFGBAS}.service
          ${BOLD}
          Echo "Service status:"
          ${SGR0}
          systemctl -l --lines=20 status charon_${LCFGBAS}.service 2>&1
          Echo
          ${BOLD}
          Echo "Log monitoring service file:"
          ${SGR0}
          if test -s /etc/systemd/system/charon_logmon_${LCFGBAS}.service
          then
            cat /etc/systemd/system/charon_logmon_${LCFGBAS}.service
          else
            Echo "Not defined"
          fi
          ${BOLD}
          Echo "Log monitoring service status:"
          ${SGR0}
          systemctl -l --lines=20 status charon_logmon_${LCFGBAS}.service 2>&1
          Echo
        fi
      else
        if test "$1" = "-html"
        then
          Echo "<b><font color='#FF0000'>Service not defined</font></b><br>"
        else
          Echo "Service not defined"
        fi
      fi
    done
  else
    Echo "No guest managed"
    test "$1" = "-html" && Echo "<br>"
  fi

  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Scheduled jobs (cron)"

  test "$1" = "-html" && Echo "<br>"
  if test -n "`crontab -l 2>/dev/null | grep charon`"
  then
    crontab -l | grep charon | while read LINE
    do
      if test "$1" = "-html"
      then
        if test "`echo ${LINE}|cut -c1`" = "#"
        then
          Echo "<font color='#8F8F8F' style='font-family:monospace;'>"
        else
          Echo "<font style='font-family:monospace;'>"
        fi
      fi
      Echo "${LINE}"
      test "$1" = "-html" && Echo "</font><br>"
    done
  else
    Echo "No scheduled job found for CHARON."
    test "$1" = "-html" && Echo "<br>"
  fi
  test "$1" = "-html" && Echo "</font>"

  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Running processes (grep charon)"

  test "$1" = "-html" && Echo "<br>"
  if test -n "`ps -ef | grep [c]haron`"
  then
    ps -ef | grep [c]haron | while read LINE
    do
      test "$1" = "-html" && Echo "<font style='font-family:monospace;'>"
      Echo "${LINE}"
      test "$1" = "-html" && Echo "</font><br>"
    done
  else
    Echo "No processes found for charon."
    test "$1" = "-html" && Echo "<br>"
  fi
  test "$1" = "-html" && Echo "</font>"

  Echo
  test "$1" = "-html" && Echo "<br>"

  Title $1 2 "Files list in ${CHARONDIR}/utils"

  test "$1" = "-html" && Echo "<br>"
  ls -lR ${CHARONDIR}/utils | while read LINE
  do
    test "$1" = "-html" && Echo "<font style='font-family:monospace;'>"
    Echo "${LINE}"
    test "$1" = "-html" && Echo "</font><br>"
  done
  test "$1" = "-html" && Echo "</font>"
}

function collect_collectlogscfg
{
  #-----------------------------------------------------------------------------
  # Collect logs and configuration files
  #-----------------------------------------------------------------------------
  Title $1 1 "Log and configuration files"

  if test ${OUTOFLTK} = 1
  then
    ${SGR0}
    test "$1" = "-html" && Echo "<br><font style='font-family:monospace;'>"
    Echo "Not applicable, kit not installed."
    test "$1" = "-html" && Echo "</font><br>"
    return
  fi

  test -n "${HL}" && (tput smacs;Echo ${HL};tput rmacs)

  Echo
  test "$1" = "-html" && Echo "<br>"
  if test -s ${CHARONDIR}/utils/charon_gstart.boot
  then
    typeset -i I=0
    if test "$1" = "-html"
    then
      Echo "<a name='LOGCFGLIST'><b>Configuration and log files:</a></b><br><br>"

      Echo "<table class=cfglog>"
      Echo "<tr>"
      Echo "<th class=cfglog rowspan=2 bgcolor=#0A71B4 align=left><font color=#FFFFFF>Configuration file</font></th>"
      Echo "<th class=cfglog colspan=3 bgcolor=#0A71B4 align=center><font color=#FFFFFF>Log file</font></th>"
      Echo "    <tr>"
      Echo "    <th class=cfglog bgcolor=#3AAADC align=center>Name</th>"
      Echo "    <th class=cfglog bgcolor=#3AAADC align=center>Size</th>"
      Echo "    <th class=cfglog bgcolor=#3AAADC align=center>Lines</th>"
      Echo "    </tr>"
      Echo "</tr>"

      cat ${CHARONDIR}/utils/charon_gstart.boot | while read L
      do
        LEXE=`echo ${L} | cut -f1 -d';'`
        LCFG=`echo ${L} | cut -f2 -d';'`
        get_logfile ${LCFG}
        I=I+1
        Echo "<tr>"
        Echo "<td class=cfglog align=left><a href='#LOGCFGC${I}'>${LCFG} (`basename ${LEXE}`)</a></td>"
        LOGFSIZ=`du -hL ${LOGF} 2>/dev/null | awk '{print $1}'`
        if test -z "${LOGFSIZ}"
        then
          LOGFSIZ="&rarr;File not found"
          LOGFLIN="NA"
        else
          LOGFLIN=`wc -l ${LOGF} 2>/dev/null | awk '{print $1}'`
        fi
        Echo "<td class=cfglog align=left><a href='#LOGCFGL${I}'>${LOGF}</a></td>"
        Echo "<td class=cfglog align=right>${LOGFSIZ}</td>"
        Echo "<td class=cfglog align=right>${LOGFLIN}</td>"
        Echo "</tr>"
      done
      Echo "</table><br>"      
    fi
    typeset -i I=0
    cat ${CHARONDIR}/utils/charon_gstart.boot | while read L
    do
      LEXE=`echo ${L} | cut -f1 -d';'`
      LCFG=`echo ${L} | cut -f2 -d';'`
      get_logfile ${LCFG}
      I=I+1
      if test "$1" = "-html"
      then
        Echo "<a name=LOGCFGC${I}></a>"
        Title $1 2 "${LCFG} (`basename ${LEXE}`)"

        Echo "<font style='font-family:monospace;'>"
        cat ${LCFG} 2>&1 | while read LINE
        do
          if test "`echo ${LINE} | cut -c1`" = "#"
          then
            if test "`echo ${LINE} | cut -c1-6`" = "#INFO:"
            then
              Echo "<font color='#0a71b4'>"
            else
              Echo "<font color='#a0a0a0'>"
            fi
          fi
          Echo "${LINE}"
          Echo "<br>"
          test "`echo ${LINE} | cut -c1`" = "#" && Echo "</font>"
        done
        Echo "</font>"

        Echo "<a name=LOGCFGL${I}></a>"
        LOGFSIZ=`du -hL ${LOGF} 2>/dev/null | awk '{print $1}'`
        if test -z "${LOGFSIZ}"
        then
          LOGFINF="File not found"
        else
          LOGFLIN=`wc -l ${LOGF} 2>/dev/null | awk '{print $1}'`
          LOGFINF="${LOGFSIZ} and ${LOGFLIN:-0} lines"
        fi

        Title $1 2 "${LOGF}" "${LOGFINF}"

        cat ${LOGF} 2>&1 | while read LINE
        do
          Echo "<font style='font-family:monospace;'>"
          case "`echo ${LINE} | cut -f4 -d:`"
          in
            0)
              case "`echo ${LINE} | cut -f5 -d:`"
              in
                0000009[789])
                  #--- HW configuration details
                  Echo "<font color='#0062ab'>"
                  ;;
                000003A5)
                  #--- session is loading built-in configuration
                  Echo "<font style='background-color:#00ff00'>"
                  ;;
                0000032B)
                  #--- Start request received
                  Echo "<font style='background-color:#00ff00'>"
                  ;;
                0000032E|0000032D)
                  #--- Stop request received
                  Echo "<font color=#ff2020>"
                  ;;
                00000350|00000408)
                  #--- Product version & license number
                  Echo "<font color=#ffffff style='background-color:#0000cc'>"
                  ;;
                00000351|0000002A)
                  #--- session: Detected removal of the license
                  Echo "<font color=#ffffff style='background-color:#990066'>"
                  ;;
                0000039D)
                  #--- License detected and online
                  Echo "<font color=#ffffff style='background-color:#00ff00'>"
                  ;;
                0000040B)
                  #--- License has changed (switch)
                  Echo "<font color=#ffffff style='background-color:#404040'>"
                  ;;
              esac
              ;;
            1)
              Echo "<font style='background-color:#00ffff'>"
              ;;
            2)
              Echo "<font color=#f0f0f0 style='background-color:#f70808'>"
              ;;
            *)
              if test "`echo ${LINE} | cut -c1-4`" = '. . '
              then
                Echo "<font color=#4b0082>"
              else
                if test "`echo ${LINE} | cut -c1-2`" = '. '
                then
                  Echo "<font color=#ffffff style='background-color:#404040'>"
                fi
              fi
              ;;
          esac
          Echo "${LINE}"
          Echo "</font></font><br>"
        done
        Echo "<table border='0' width='100%'>"
        Echo "<tr><td align=right>"
        Echo "<a href='#LOGCFGLIST'>Back to Configuration and log files list</a></td></tr></table>"
        Echo "<br>"
      else
        Echo "Executable:  ${LEXE}"
        Echo "Config file: ${LCFG}"
        ${BOLD}
        Echo "${LCFG}:"
        ${SGR0}
        cat ${LCFG} 2>&1 
        ${BOLD}
        Echo "${LOGF}:"
        ${SGR0}
        cat ${LOGF} 2>&1 
        Echo
      fi
    done
  else
    Echo "No guest service defined"
    test "$1" = "-html" && Echo "<br>"
  fi
}


if test `id -u` -ne 0
then
  Echo "Must be root !"
  Echo
  Echo "For help, run: $0 -h"
  Echo
  exit 1
fi

if test -e /etc/redhat-release
then
  OS_RELEASE=/etc/redhat-release
else
  OS_RELEASE=/etc/system-release
fi

BOLD="tput bold"
BOLDC="printf [1m[36m"
SGR0="tput sgr0"
HL="qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"

case "$1"
in
  -h)
    Usage
    exit 0
    ;;
  -lite)
    BOLD=""
    BOLDC=""
    SGR0=""
    HL="--------------------------------------------------------------------------"
    ;;
  -html)
    BOLD="Echo <b>"
    BOLDC="Echo <b><font color=#0000FF>"
    SGR0="Echo </b></font>"
    HL=""
    ;;
  -mail=*)
    tput bold
    printf "Name (empty one to exit): "
    tput sgr0
    read CS_NAME
    test -z "${CS_NAME}" && exit 0

    tput bold
    printf "Company (empty one to exit): "
    tput sgr0
    read CS_COMP
    test -z "${CS_COMP}" && exit 0

    $0 -html

    cat <<EOF >/tmp/charon_showhostinfo.htm.tmp
<b>Customer</b><br><hr>
Name: <font color="#2020FF">${CS_NAME}</font><br>
Company: <font color="#2020FF">${CS_COMP}</font><br>
<br>
`cat /tmp/charon_showhostinfo.htm`
EOF
    mv -f /tmp/charon_showhostinfo.htm.tmp /tmp/charon_showhostinfo.htm
    MAILTO=`echo $1 | cut -f2 -d'='`

    echo "Sending mail..."
    TMPMAIL=/tmp/charon_showhostinfo.htm.$$
    BOUNDARY="$$CHARONSENDMAIL`basename $0`$$"

    MAILFROM=`cat ${MAILFRFILE} 2>/dev/null`
    if test -z "${MAILFROM}"
    then
      echo "From: `whoami`@`hostname`" >${TMPMAIL}
    else
      if test -n "`echo ${MAILFROM} | grep '@'`"
      then
        echo "From: ${MAILFROM}" >${TMPMAIL}
      else
        echo "From: ${MAILFROM}<`whoami`@`hostname`>" >${TMPMAIL}
      fi
    fi

    echo "To: ${MAILTO}" >>${TMPMAIL}
    echo "Subject: [CHARON] Show Host Information from `hostname -s`" >>${TMPMAIL}
    echo "Mime-Version: 1.0;" >>${TMPMAIL}
    echo "Content-Type: multipart/mixed; boundary=${BOUNDARY}" >>${TMPMAIL}
    echo "" >>${TMPMAIL}
    echo "Please find attached show host information html file" >>${TMPMAIL}
    echo "" >>${TMPMAIL}

    echo "--${BOUNDARY}" >>${TMPMAIL}
    echo "Content-Type: text/html; charset=\"ISO-8859-1\";" >>${TMPMAIL}
    echo "Content-Disposition: inline;" >>${TMPMAIL}
    echo "" >>${TMPMAIL}
    echo "<html>" >>${TMPMAIL}
    if test "`cat ${OS_RELEASE} | awk '{print $1}'`" = "Red" -o "`cat ${OS_RELEASE} | awk '{print $1}'`" = "CentOS"
    then
      if test `du -k /tmp/charon_showhostinfo.htm | awk '{print $1}'` -le 500
      then
        #--- With Red Hat, sendmail allows log and config files in the message body, Fedora does not (v17)
        cat /tmp/charon_showhostinfo.htm >>${TMPMAIL}
      else
        echo "Please find attached the host information html file.<br>" >>${TMPMAIL}
      fi
    else
      echo "Please find attached the host information html file.<br>" >>${TMPMAIL}
    fi
    echo "</html>" >>${TMPMAIL}
    echo "" >>${TMPMAIL}

    echo "--${BOUNDARY}" >>${TMPMAIL}
    rm -f /tmp/charon_showhostinfo.htm.zip 2>/dev/null
    zip -j /tmp/charon_showhostinfo.htm.zip /tmp/charon_showhostinfo.htm >/dev/null 2>&1
    if test $? = 0
    then
      # Joins the /opt/charon/log folder in the zipped file
      zip /tmp/charon_showhostinfo.htm.zip /opt/charon/log/* >/dev/null 2>&1
      echo "Content-Type: application/octet-stream; name=Charon_ShowHostInfo_`hostname -s`.htm.zip;" >>${TMPMAIL}
      echo "Content-Transfer-Encoding: base64;" >>${TMPMAIL}
      echo "Content-Disposition: Attachment; filename=Charon_ShowHostInfo_`hostname -s`.htm.zip" >>${TMPMAIL}
      echo "" >>${TMPMAIL}
      base64 /tmp/charon_showhostinfo.htm.zip >>${TMPMAIL}
    else
      echo "Content-Type: application/octet-stream; name=Charon_ShowHostInfo_`hostname -s`.htm;" >>${TMPMAIL}
      echo "Content-Transfer-Encoding: base64;" >>${TMPMAIL}
      echo "Content-Disposition: Attachment; filename=Charon_ShowHostInfo_`hostname -s`.htm" >>${TMPMAIL}
      echo "" >>${TMPMAIL}
      base64 /tmp/charon_showhostinfo.htm >>${TMPMAIL}
    fi
    echo "" >>${TMPMAIL}

    echo "--${BOUNDARY}--" >>${TMPMAIL}

    sendmail -t -i <${TMPMAIL}
    if test $? = 0
    then
      echo "Done."
      rm -f ${TMPMAIL}
    else
      echo "Failed!"
    fi

    exit 0
    ;;
  -hostos)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
  -hardware)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
  -processes)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
  -storage)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
  -network)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
  -charonversion)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
  -charontoolkit)
    collect_`echo $1  | tr -d "-"`
    exit 0
    ;;
esac

if test "$1" = "-html"
then
  rm -f /tmp/charon_showhostinfo.htm 2>/dev/null
  tput smso
  echo "Generating output to /tmp/charon_showhostinfo.htm ..."
  tput rmso

  Echo "<style type='text/css'>"
  Echo "table.cfglog {"
  Echo "  border: 2px solid #0A71B4;"
  Echo "  width: 100%"
  Echo "}"
  Echo "th.cfglog, td.cfglog {"
  Echo "  padding: 5px;"
  Echo "  border: 1px solid #0062AB;"
  Echo "}"
  Echo "</style>"

  Echo "<html>"
  Echo "<a name='top'></a>"
  Echo "<table border='2' width='100%'>"
  Echo "<tr><td align=center bgcolor=#0A71B4><br>"
  if test ${OUTOFLTK} = 0
  then
    Echo "<strong><font color='#FFFFFF'>CHARON Linux Toolkit - Show Host Information</font></strong>"
  else
    Echo "<strong><font color='#FFFFFF'>CHARON - Show Host Information</font></strong>"
  fi
  Echo "<br><br></td></tr>"
  Echo "</table><br>"

  ${BOLDC}
  D=`date +"%d-%b-%Y %H:%M:%S"`
  Echo "Started on ${D}"
  Echo "<br>"
  ${SGR0}

  Title $1 1 "Table of contents"

  Echo "<br><font style='font-family:monospace;'>"
  grep 'Title \$1' $0 | grep -v grep | grep -v "Table of contents" | while read LINE
  do
    CLVL=`echo ${LINE} | awk '{print $3}'`
    CHAP=`echo ${LINE} | sed 's=\(^.*"\)\(.*\)\(".*$\)=\2=g'`
    if test "${CLVL}" = "1"
    then
      # Parent chapter
      Echo "</ul><a href='#${CHAP}'>${CHAP}</a><br><ul>"
    else
      # Child chapter
      if test -z "`echo ${CHAP} | grep '\${'`"
      then
        Echo "<li><a href='#${CHAP}'>${CHAP}</a></li>"
      fi
    fi
  done
  Echo "</ul></font><br><br>"
fi

for FUNC in `grep "^function collect_" $0 | awk '{print $2}'`
do
  ${FUNC} $1
  if test "$1" = "-html"
  then
    Echo "<br>"
    Echo "<table border='0' width='100%'>"
    Echo "<tr><td align=right>"
    Echo "<a href='#top'>Back to top &uarr;</a></td></tr></table>"
    Echo "<br>"
   fi
done

Echo 
test "$1" = "-html" && Echo "<br><hr>"
${BOLDC}
D=`date +"%d-%b-%Y %H:%M:%S"`
Echo "Completed on ${D}"
test "$1" = "-html" && Echo "<br></html>"
${SGR0}

if test "$1" = "-html"
then
  echo
  tput smso
  echo "Report completed. Output file is: /tmp/charon_showhostinfo.htm"
  tput rmso
fi

exit
