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

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

MAILTOFILE="/tmp/charon_menu_showlic.mailto"
MAILFRFILE="${CHARONDIR}/utils/charon_check.mailfrom"
DONGLESFILE="${CHARONDIR}/utils/charon_licenses.list"

get_preferences

display_header "HASP license details"
display_licfound

OPTHASP=""
HASPEXE=`which hasp_srm_view 2>/dev/null`
if test -z "${HASPEXE}"
then
  echo "Cannot find 'hasp_srm_view' executable file."
  echo "Please verify CHARON is installed and PATH is correctly set."
  Press_Enter
  exit 1
else
  test -n "`strings ${HASPEXE} | grep ^-all`" && OPTHASP="-all"
fi

#--- Check if 'atd' service is enabled
ATDX=0
systemctl -q is-enabled atd 2>/dev/null
if test $? -ne 0
then
  tput bold
  echo "'atd' service is not enabled. Please read the installation guide and enable it."
  tput sgr0
  ATDX=1
fi

#--- Check if 'atd' service is enabled
systemctl -q is-active atd 2>/dev/null
if test $? -ne 0
then
  tput bold
  echo "'atd' service is not running. Please start it."
  tput sgr0
  ATDX=1
fi

if test ${ATDX} -eq 1
then
  Press_Enter
  exit 2
fi

OUTVIEW="/tmp/`basename $0`_`date +%Y%m%d_%H%M%S`.view"
echo -n "Please wait, generating output..."
touch ${OUTVIEW}

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
  echo -n "."
  test -z "`at -l | grep -w ^${ATNUM}`" && break
  if test $N -gt 30
  then
    >${OUTVIEW}
    break
  fi
done

echo
tput cuu1;tput el

LICNUM=`grep "^The License Number" ${OUTVIEW} | sed "s=\(^.*Number: \)\(.*\)=\2=g" | tr "\r\n" " "`
test -z "${LICNUM}" && LICNUM="Unknown_License"

OUTP="/tmp/`echo ${LICNUM} | tr ' ' '_'`.txt"
cat ${OUTVIEW} | sed "s=\r==g" >${OUTP}
rm -f ${OUTVIEW} 2>/dev/null

if test -s ${OUTP}
then
  # if test -z "`grep 'key not found' ${OUTP}`"
  if test -n "`grep 'License type:' ${OUTP}`"
  then
    tput bold
    echo "Licenses found (local and remote):"
    tput sgr0
    echo
    echo "License number Type Key ID"
    if test "${SEMIGRAPH}" = "enabled"
    then
      tput smacs
      echo "qqqqqqqqqqqqqq qqqq qqqqqqqqqqqqqqqqqqqq"
      tput rmacs
    else
      echo "-------------- ---- --------------------"
    fi
    if test -s ${DONGLESFILE}
    then
      tput cuu1
      tput cuu1
      tput hpa 41
      tput bold
      echo "Description"
      tput sgr0
      tput hpa 41
      if test "${SEMIGRAPH}" = "enabled"
      then
        tput smacs
        echo "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
        tput rmacs
      else
        echo "--------------------------------------"
      fi
    fi

    grep -e "^The License" -e "^The Sentinel SL" -e "^The Master KeyId" -e "^License type:" ${OUTP} | while read LINE
    do
      case "`echo ${LINE} | awk '{print $1,$2}'`"
      in
        "The License")
          case "`echo ${LINE} | awk '{print $3}'`"
          in
            "Number:")
              LICNUM=`echo ${LINE} | sed "s=\(^.*Number: \)\(.*\)=\2=g" | tr -d "\r\n"`
              LICDESC=`grep "^${LICNUM};" ${DONGLESFILE} 2>/dev/null | cut -f2 -d';'`
              COLDESC=`grep "^${LICNUM};" ${DONGLESFILE} 2>/dev/null | cut -f3 -d';'`
            ;;
            "KeyId:")
              KEYID=`echo ${LINE} | sed "s=\(^.*KeyId: \)\(.*\)=\2=g" | tr "\r\n" " "`
              ;;
          esac
          ;;
        "The Sentinel")
          if test "`echo ${LINE} | awk '{print $3,$4}'`" = "SL KeyId:"
          then
            KEYID=`echo ${LINE} | sed "s=\(^.*KeyId: \)\(.*\)=\2=g" | tr "\r\n" " "`
          fi
          ;;
        "License type:")
          if test "`echo ${LINE} | awk '{print $3}'`" = "Software"
          then
            LICHASPTYP="Soft"
          else
            LICHASPTYP="USB "
          fi
          ;;
        "The Master")
          if test "`echo ${LINE} | awk '{print $3}'`" = "KeyId:"
          then
            if test "${TERM}" != "dumb"
            then
              printf "%-14s %-4s %-20s " ${LICNUM} ${LICHASPTYP} ${KEYID}
              test -n "${LICDESC}" && echo "[3${COLDESC:-0}m${LICDESC}[0m" || echo
            fi
          fi
          ;;
      esac
    done
    echo

    ask_editor
    if test -n "${EDI}"
    then
      if test -z "`echo ${EDI}|grep vim`"
      then
        ${EDI} ${OUTP} 2>/dev/null
      else
        ${EDI} -u ${CHARONDIR}/utils/charonlic.vimrc ${OUTP} 2>/dev/null
      fi
    fi
    echo
    while test 1
    do
      printf "Do you want to send file via mail (y/n) ? "
      read CH
      case "${CH}"
      in
        y)
          >${MAILTOFILE}
          select_mailto
          if test -s /root/.charon.recipient
          then
            MAILTO=`cat /root/.charon.recipient`
            echo
            MAILFROM=`cat ${MAILFRFILE} 2>/dev/null`
            if test -z "${MAILFROM}"
            then
              echo "Sending mail to ${MAILTO}..."
              (echo -e "Subject: [CHARON] View license HASP_SRM_VIEW\nTo: ${MAILTO}";cat ${OUTP}) | sendmail -t 
              test $? -eq 0 && echo "Done."
            else
              echo "Sending mail to ${MAILTO} as '${MAILFROM}'..."
              if test -n "`echo ${MAILFROM} | grep '@'`"
              then
                (echo -e "Subject: [CHARON] View license HASP_SRM_VIEW\nTo: ${MAILTO}\nFrom: ${MAILFROM}";cat ${OUTP}) | sendmail -t 
              else
                (echo -e "Subject: [CHARON] View license HASP_SRM_VIEW\nTo: ${MAILTO}\nFrom: ${MAILFROM}<`whoami`@`hostname`>";cat ${OUTP}) | sendmail -t 
              fi
              test $? -eq 0 && echo "Done."
            fi
            echo       
          fi
          break
          ;;
        n)
          break
          ;;
        *)
          tput cuu1;tput el
          ;;
      esac
    done
  else
    echo
    tput bold
    cat ${OUTP}
    tput sgr0
    echo
    Press_Enter
    echo "[44m[37m USB dongle driver status (aksusbd) [0m"
    systemctl -l --lines=6 status aksusbd
    Press_Enter
  fi
else
  tput bold
  echo "Output file empty, please check your dongle is connected and retry."
  tput sgr0
  Press_Enter
fi

exit
