#!/bin/sh
#-------------------------------------------------------------------------------
# charon_menu_hostinfo
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2022 STROMASYS.
# All rights reserved.
#
#set -x
VERSION=1.7

. `dirname $0`/charon_common
. `dirname $0`/charon_common_menu

MAILTOFILE="/tmp/charon_menu_hostinfo.mailto"

get_preferences

case "$1"
in
  "")
    cd ${CHARONDIR}/utils
    ./menu_mod.pl -menu=charon_menuhostinfo.dat
    cd -
    ;;
  -enhanced)
    ${CHARONDIR}/utils/charon_showhostinfo | more
    ;;
  -lite)
    ${CHARONDIR}/utils/charon_showhostinfo -lite | more
    ;;
  -litemail)
    OUTP="/tmp/charon_showhostinfo.txt"
    echo "Generating TEXT file '${OUTP}'"   
    ${CHARONDIR}/utils/charon_showhostinfo -lite >${OUTP}
    echo
    select_mailto 
    if test -s /root/.charon.recipient
    then
      MAILTO=`cat /root/.charon.recipient`
      echo
      echo "Sending mail to ${MAILTO}..."
      (echo -e "Subject: [CHARON] Show host information\nTo: ${MAILTO}";cat ${OUTP}) | sendmail -t
      test $? -eq 0 && echo "Done."
      echo
    fi
    ;;
  -genhtml)
    OUTP="/tmp/charon_showhostinfo.htm"
    ${CHARONDIR}/utils/charon_showhostinfo -html
    echo
    if test -x /usr/bin/firefox -a -n "${DISPLAY}"
    then
      while test 1
      do
        printf "${FGBLUE}Do you want to view file with Firefox (y/n) ?[0m "
        read CH
        case "${CH}"
        in
          y)
            echo >>${CHARONDIR}/log/`basename $0`.log
            echo `date +"%d-%b-%Y %H:%M:%S"` "------------------------------" >>${CHARONDIR}/log/`basename $0`.log
            FIRLOG=${CHARONDIR}/log/`basename $0`.log

            /usr/bin/firefox ${OUTP} 2>${FIRLOG}

            if test $? != 0
            then
              if test -n "`grep 'Running Firefox as root' ${FIRLOG} | grep 'not supported'`"
              then
                #--- extracting the user in the error output file ("who am i" could also work)
                FIRUSR=`sed "s=\(^.* owned by \)\(.*\)\(\..$\)=\2=g" ${FIRLOG}`
                su ${FIRUSR} -c "/usr/bin/firefox ${OUTP}"
              fi
            else
              echo "Firefox failed to execute:"
              cat ${FIRLOG}
              echo
              Press_Enter
            fi
            break
            ;;
          n)
            break
            ;;
          *)
            tput bel
            tput cuu1;tput el
            ;;
        esac
      done
    else
      if test -z "${DISPLAY}"
      then
        echo "No DISPLAY, cannot run firefox."
      else
        echo "Firefox executable not found."
      fi
      echo
      Press_Enter
    fi
    ;;
  -htmlmail)
    echo
    >${MAILTOFILE}
    select_mailto
    if test -s /root/.charon.recipient
    then
      MAILTO=`cat /root/.charon.recipient`
      echo
      echo "______________________________________________________________________"
      echo
      ${CHARONDIR}/utils/charon_showhostinfo -mail=${MAILTO}
      echo
    fi
    ;;
esac
exit
