#!/bin/bash
#-------------------------------------------------------------------------------
# charon_menu_setup.sh for Charon-PAR
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2023 STROMASYS.
# All rights reserved.
# Products: PAR
#set -x
VERSION=2.1

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

if test `id -u` -ne 0
then
  echo "[41m[37m ERROR [0m must be root !"
  echo
  exit 1
fi

if test -x /usr/bin/dnf
then
  YUMC="/usr/bin/dnf --nogpgcheck"
  YUMM="dnf"
else
  YUMC="/usr/bin/yum --nogpgcheck"
  YUMM="yum"
fi

trap 'tput sgr0;echo;exit' 1 2 14 15

#=============================================================================
# Function: select_kit_location
#=============================================================================
function select_kit_location
{
  echo
  KIT=""
  while test 1
  do
    LASTLOC=`cat ${CHARONKITFOLDER} 2>/dev/null`
    LASTLOCDISP=""
    test -n "${LASTLOC}" && LASTLOCDISP="[${LASTLOC}] "
    echo "Enter kit location (folder), . for current folder, q to quit"
    echo -n "${LASTLOCDISP}: ${FGBLUE}"
    read KITLOC
    tput sgr0
    test -z "${KITLOC}" && KITLOC=${LASTLOC}
    test "${KITLOC}" = "." && KITLOC=${PWD}
    test "${KITLOC}" = "q" && break
    if test -n "${KITLOC}" -a -d "${KITLOC}"
    then
      >/tmp/charon_install.tmp

      #--- First pass: extract complete packs if there are
      echo
      echo -n "Searching for complete packs in ${KITLOC} ..."
      SAVKITLOC=${KITLOC}
      find ${KITLOC} -name "CHARON-*-completepack.tar.gz" | while read FILE
      do
        cd ${KITLOC}

        if test -s ${FILE}.md5sum
        then
          FILSUM=`cat ${FILE}.md5sum | awk '{print $1}'`
          DNLSUM=`md5sum ${FILE} | awk '{print $1}'`
          if test "${FILSUM}" = "${DNLSUM}"
          then
            #echo "Unzipping complete pack '${FILE}' (MD5SUM match)"
            PACKIT=`tar -tzf ${FILE} | grep InstallationKit/$`
            tar -xzf ${FILE}
            test $? = 0 && mv ${FILE} ${FILE}.save
          else
            echo
            echo "${BGORANGE} WARNING [0m Cannot unzip complete pack '${FILE}': MD5SUM does not match"
          fi
        else
          #echo "Unzipping complete pack '${FILE}'"
          tar -xzf ${FILE}
          if test $? = 0
          then
            mv ${FILE} ${FILE}.save
            mv ${PACKIT}/*.tar.gz* ${KITLOC} 
            echo "Files moved to '${KITLOC}' container" >${PACKIT}/readme.txt
          fi
        fi
      done
      echo " -> done."

      #--- 2nd pass: search for kits
      echo -n "Searching kits in ${KITLOC} ..."
      find ${KITLOC} -name "hpa-*-l*" -type d | while read FILE
      do
        echo -n "."
        echo ${FILE} >>/tmp/charon_install.tmp
      done
      find ${KITLOC} -name "charon-*.${INS_OS}*" -type d | while read FILE
      do
        echo -n "."
        echo ${FILE} >>/tmp/charon_install.tmp
      done
      find ${KITLOC} -name "charon-*.${INS_OS}*.tar" | while read FILE
      do
        echo -n "."
        echo ${FILE} >>/tmp/charon_install.tmp
      done
      find ${KITLOC} -name "charon-*.${INS_OS}*.tar.gz" | while read FILE
      do
        echo -n "."
        echo ${FILE} >>/tmp/charon_install.tmp
      done
      echo " -> done."

      #--- 3rd pass: search for license server packages (VE)
      echo -n "Searching license server package in ${KITLOC} ..."
      find ${KITLOC} -name "license-server-*.rpm" | while read FILE
      do
        echo -n "."
        echo ${FILE} >>/tmp/charon_install.tmp
      done
      echo " -> done."

      tput ed
      if test -s /tmp/charon_install.tmp
      then
        sort /tmp/charon_install.tmp >/tmp/charon_install.sort.tmp
        mv -f /tmp/charon_install.sort.tmp /tmp/charon_install.tmp
        while test 1
        do
          typeset -i I=0
          echo
          tput bold
          echo "No  Product              Version Build  Installed Container  md5sum"
          tput sgr0
          if test "${SEMIGRAPH}" = "enabled"
          then
            tput smacs
            echo "qqq qqqqqqqqqqqqqqqqqqqq qqqqqqq qqqqqq qqqqqqqqq qqqqqqqqqq qqqqqqq"
            tput rmacs
          else
            echo "--- -------------------- ------- ------ --------- ---------- -------"
          fi
          LAST_BLD=""

          ( grep -v ^$ /tmp/charon_install.tmp | while read LINE
          do
            if test -z "$(echo ${LINE} | grep 'license-server')"
            then
              if test -n "`echo ${LINE} | grep '\.${VENDOR_ID}\.'`"
              then
                #--- Vendor number is present in kit name
                echo ${LINE} | sed "s=\(^.*-\)\([0-9][0-9][0-9][0-9][0-9]\)\(\.${VENDOR_ID}\..*$\)=\2 \0=g"
              else
                #--- Vendor number not is present in kit name
                if test "`basename ${LINE} | cut -c1-3`" = "hpa"
                then
                  LINH=`ls ${LINE}/hpa-*.rpm`
                  if test -n "${LINH}"
                  then
                    LINH=`echo ${LINH} | sed "s=^${LINE}/==g" | sed "s=^hpa-.*-\([0-9][0-9][0-9][0-9][0-9]\)\..*$=\1=g"`
                    echo ${LINH} ${LINE}
                  fi
                else
                  echo ${LINE} | sed "s=\(^.*-\)\([0-9][0-9][0-9][0-9][0-9]\)\(\..*$\)=\2 \0=g"
                fi
              fi
            else
              echo "XXXXX ${LINE}"
            fi
          done ) | sort -n >/tmp/charon_install.tmp.sorted

          cat /tmp/charon_install.tmp.sorted | awk '{print $2}' >/tmp/charon_install.tmp

          COL=""
          cat /tmp/charon_install.tmp | while read LINE
          do
            I=I+1
            LKIT=`basename ${LINE}`

            if test -z "$(echo ${LINE} | grep 'license-server')"
            then
              if test "`echo ${LKIT} | cut -c1-3`" = "hpa"
              then
                LPRD="hpa"
                LVER=`echo ${LINE} | cut -f2 -d '-' | sed "s=\([0-9]\)\([0-9]\)=\1\.\2\.0=g"`
                LBLD="00000"
                LINH=`ls ${LINE}/hpa-*.rpm`
                if test -n "${LINH}"
                then
                  LBLD=`echo ${LINH} | sed "s=^${LINE}/==g" | sed "s=^hpa-.*-\([0-9][0-9][0-9][0-9][0-9]\)\..*$=\1=g"`
                fi
              else
                LK3=`echo ${LKIT} | cut -f3 -d'-'`
                if test -z "`echo ${LK3} | tr -dc '.'`"
                then
                  LPRD=`echo ${LKIT} | cut -f1 -d'-'`-`echo ${LKIT} | cut -f2 -d'-'`-`echo ${LKIT} | cut -f3 -d'-'`
                  LVER=`echo ${LKIT} | cut -f4 -d'-'`
                  LBLD=`echo ${LKIT} | cut -f5 -d'-' | cut -f1 -d'.'`
                else
                  LPRD=`echo ${LKIT} | cut -f1 -d'-'`-`echo ${LKIT} | cut -f2 -d'-'`
                  LVER=`echo ${LKIT} | cut -f3 -d'-'`
                  LBLD=`echo ${LKIT} | cut -f4 -d'-' | cut -f1 -d'.'`
                fi
              fi
            else
              LPRD="license-server"
              LVER=$(echo ${LKIT} | cut -f3 -d'-' | sed "s=\.rpm==g")
              LBLD=""
            fi
            LMD5="-"
            LINS="no"
            if test -d ${LINE}
            then
              LCNT="folder"
              if test "`echo ${LKIT} | cut -c1-3`" = "hpa"
              then
                LPK=`ls ${LINE}/hpa-*.rpm 2>/dev/null`
              else
                LPK=`ls ${LINE}/charon-par-*.rpm 2>/dev/null | grep -v debuginfo`
              fi
              if test -n "${LPK}"
              then
                LPK=`rpm -q -p ${LPK}`
                if test -z "`rpm -q ${LPK} 2>/dev/null | grep 'is not installed'`"
                then
                  LINS="yes"
                fi
              fi
            else
              if test -n "$(echo ${LINE} | grep 'license-server')"
              then
                LCNT="rpm"
                test -z "`rpm -q license-server 2>/dev/null | grep 'is not installed'`" && LINS="yes"
              else
                LCNT=`echo ${LINE} | sed "s=\(^.*\.\)\(tar.*$\)=\2=g"`
                if test "${LCNT}" = "tar.gz"
                then
                  LINS="-"
                  if test -e ${LINE}.md5sum
                  then
                    FILSUM=`cat ${LINE}.md5sum | awk '{print $1}'`
                    DNLSUM=`md5sum ${LINE} | awk '{print $1}'`
                    if test "${FILSUM}" = "${DNLSUM}"
                    then
                      LMD5="[30m[42m OK [0m"
                    else
                      LMD5="[37m[41m FAILED [0m"
                    fi
                  else
                    LMD5="not found"
                  fi
                fi
              fi
            fi
            if test "${LAST_BLD}" != "${LBLD}"
            then
              if test -z "${COL}"
              then
                COL=${FGBLUE}
              else
                COL=""
              fi
            fi
            test -n "$(echo ${LINE} | grep 'license-server')" && COL="[38;5;39m"
            test "${LINS}" = "yes" && COL="[32m[1m"
            echo -n "${COL}"
            printf "%3d %-20s %7s %6s " $I ${LPRD} ${LVER} ${LBLD}
            printf "%-9s " ${LINS}
            printf "%-10s " ${LCNT}
            echo "${LMD5}"
            tput sgr0
            LAST_BLD=${LBLD}
          done
          echo
          printf "Select the kit you want to use (q to quit): "
          read ANS
          test "${ANS}" = "q" && break 2
          echo
          if test -n "${ANS}"
          then
            if test -z "`echo ${ANS} | tr -d [0123456789]`"
            then
              if test ${ANS} -lt 1 -o ${ANS} -gt `cat /tmp/charon_install.tmp | wc -l`
              then
                tput bold
                echo "Incorrect value, out of range."
                tput sgr0
              else
                KIT=`cat /tmp/charon_install.tmp | head -${ANS} | tail -1`
                KITLOC=`dirname ${KIT}`
                KIT=`basename ${KIT}`
                echo "${SAVKITLOC}" >${CHARONKITFOLDER}
                if test -n "`echo ${KIT} | grep '\.tar.gz$'`"
                then
                  cd ${KITLOC}
                  if test -s ${KIT}.md5sum
                  then
                    echo "Checking ${KIT} file (md5sum) ..."
                    FILSUM=`cat ${KIT}.md5sum | awk '{print $1}'`
                    DNLSUM=`md5sum ${KIT} | awk '{print $1}'`
                    if test "${FILSUM}" = "${DNLSUM}"
                    then
                      echo "Success."
                    else
                      echo "[41m[37mChecksum failure![0m Please remove the kit and download again."
                      KIT=""
                      Press_Enter
                      break 2
                    fi
                  fi
                  echo "Unzipping file..."
                  gunzip ${KIT}
                  KIT=`echo ${KIT} | sed "s=\.gz$==g"`
                  cd - >/dev/null 2>&1
                fi
                if test -n "`echo ${KIT} | grep '\.tar$'`"
                then
                  echo "Extracting tar file..."
                  cd ${KITLOC}
                  tar -xf ${KIT}
                  if test $? = 0
                  then
                    echo "Done."
                    KIT0=`tar -tf ${KIT} | head -1`
                    echo "Removing tar file..."
                    rm -f ${KIT}
                    KIT=`basename ${KIT0}`
                  else
                    tput bold
                    echo "Failed!"
                    tput sgr0
                    KIT=""
                  fi
                  cd - >/dev/null 2>&1
                fi
                break 2
              fi
            else
              tput bold
              echo "Incorrect value '${ANS}', must be numeric."
              tput sgr0
            fi
          fi
        done
        break
      else
        echo "${FGORANGE}No kit found in this folder!"
        tput sgr0
      fi
    else
      echo "${BGORANGE} WARNING [0m Invalid answer or '${KITLOC}' is not a folder"
      echo
    fi
  done
  echo
}


#=============================================================================
# Function: charon_install
#=============================================================================
function charon_install
{
  YDAT=`date +%Y%m%d_%H%M%S`
  test ! -e ${CHARONDIR}/log && mkdir -p ${CHARONDIR}/log

  if test -d $1
  then
    cd $1
  else
    cd $(dirname $1)
  fi

  #--- license server (VE) as a separate kit case 
  if test -n "$(echo $1 | grep 'license-server')"
  then
    (${YUMC} -y install $1 2>&1;echo $? >/tmp/charon_setup.ret) | tee ${CHARONDIR}/log/${YUMM}.install.${YDAT}
    RET=`cat /tmp/charon_setup.ret 2>/dev/null`
    if test ${RET} = 0
    then
      #--- For case where install is OK but "Failed to" messages are found
      if test -n "`grep 'Failed to' ${CHARONDIR}/log/${YUMM}.install.${YDAT}`"
      then
        echo "${BGORANGE} Installation completed with failures [0m"
      else
        echo "[42m[30m Installation completed [0m"
      fi
    fi  
    cd - >/dev/null 2>&1
    echo

    if test "${GLOBALLOGMON}" = "enabled"
    then
      if test -x /opt/license-server/license_viewer
      then
        if test ! -s /etc/systemd/system/charon_monvesvrlog.service
        then
          echo "Installing VE license server log monitoring service..."
          cp -f ${CHARONDIR}/utils/templates/charon_monvesvrlog.service /etc/systemd/system/charon_monvesvrlog.service
          systemctl daemon-reload
          systemctl enable charon_monvesvrlog.service
          systemctl start  charon_monvesvrlog.service
          sleep 3
        fi
      fi
    fi

    return
  fi

  #-- Try to resolve conflict is aksusbd packages for .i386 and .x86_64 are present (TO CONFIRM)
  if test `uname -m` = "x86_64"
  then
    if test -n "`ls aksusbd-*.x86_64.rpm 2>/dev/null`"
    then
      if test -n "`ls aksusbd-*.i386.rpm 2>/dev/null`"
      then
        ls aksusbd-*.i386.rpm 2>/dev/null | xargs -i mv {} {}.not_installed 2>/dev/null
      fi
    fi
  else
    ls aksusbd-*.x86_64.rpm 2>/dev/null | xargs -i mv {} {}.not_installed 2>/dev/null
  fi

  #-- Do not install the debuginfo. Only manual install
  if test -n "`ls charon-par-debuginfo*.rpm 2>/dev/null`"
  then
    ls charon-par-debuginfo*.rpm 2>/dev/null | xargs -i mv {} {}.not_installed 2>/dev/null
  fi

#HERE: if build >21000, ask if install aksusbd or if license server will be used or both
#HERE  ==> need testing with a functional VE license server !!

  if test "`basename $1 | cut -c1-3`" = "hpa"
  then
    (${YUMC} -y install hpa-*.rpm *.${INS_OS}.*.rpm aksusbd*.rpm 2>&1;echo $? >/tmp/charon_setup.ret) | tee ${CHARONDIR}/log/${YUMM}.install.${YDAT}
  else
    (${YUMC} -y install *.${INS_OS}.*.rpm aksusbd*.rpm 2>&1;echo $? >/tmp/charon_setup.ret) | tee ${CHARONDIR}/log/${YUMM}.install.${YDAT}
  fi
  RET=`cat /tmp/charon_setup.ret 2>/dev/null`
  if test ${RET} = 0
  then
    #--- For case where install is OK but "Failed to" messages are found
    if test -n "`grep 'Failed to' ${CHARONDIR}/log/${YUMM}.install.${YDAT}`"
    then
      echo "${BGORANGE} Installation completed with failures [0m"
    else
      echo "[42m[30m Installation completed [0m"
      rpm -q aksusbd >/dev/null
      if test $? -eq 0
      then
        echo "Restarting aksusbd service..."
        systemctl restart aksusbd
      fi
    fi

    #--- If the /etc/profile.d/charon.sh file does not exist, create it
    if test ! -e /etc/profile.d/charon.sh
    then
      echo "PATH=\$PATH:/opt/charon/bin:/opt/charon/cfg" >/etc/profile.d/charon.sh
      echo "export PATH" >>/etc/profile.d/charon.sh
    fi
    if test -z "`echo $PATH | grep /opt/charon/bin`"
    then
      cat <<EOP
Execute the following command or disconnect/reconnect for PATH to be defined:

# . /etc/profile.d/charon.sh

EOP
    fi
  else
    echo "[41m[30m Installation failed (error code ${RET}) [0m"
  fi
  tput sgr0
  echo
  echo "${FGBLUE}Output saved to[0m [1m${CHARONDIR}/log/${YUMM}.install.${YDAT}[0m"

  cd - >/dev/null 2>&1

  if test "${GLOBALLOGMON}" = "enabled"
  then
    if test -x /usr/sbin/aksusbd
    then
      if test ! -s /etc/systemd/system/charon_monusb.service
      then
        echo "Installing aksusbd monitoring service..."
        cp -f ${CHARONDIR}/utils/templates/charon_monusb.service /etc/systemd/system/charon_monusb.service
        systemctl daemon-reload
        systemctl enable charon_monusb.service
        systemctl start  charon_monusb.service
        sleep 3
      fi
    fi
  fi
  echo
}

#=============================================================================
# Function: charon_uninstall
#=============================================================================
function charon_uninstall
{
  VMRUN=`ps -eo command|grep -w -e [c]haron-par -e [h]ppa`
  if test -n "${VMRUN}"
  then
    echo
    tput bold
    echo "Cannot uninstall Charon, there is at least one virtual machine running:"
    tput sgr0
    echo ${VMRUN}
    echo
    return
  fi

  #--- License server removal
  echo
  KIT="license-server"
  KITNAM=${KIT}
  KITRPM=${KIT}
  P="V$(rpm -q ${KITRPM} | grep -v 'not installed' | cut -f3 -d'-')"
  if test "$P" != "V"
  then
    while test 1
    do
      echo -n "Do you want to uninstall ${FGBLUE}${KITNAM} ${P}[0m (y/n) ? "
      read ANS
      case "${ANS}"
      in
        y|Y)
          echo
          echo "[44m[37m Removing ${KITRPM} ... [0m"
          sleep 1
          ${YUMC} -y remove ${KITRPM}

          if test -s /etc/systemd/system/charon_monvesvrlog.service
          then
            echo "Uninstalling VE license server log monitoring service..."
            systemctl stop  charon_monvesvrlog.service
            sleep 2
            systemctl disable charon_monvesvrlog.service
            sleep 1
            rm -f /etc/systemd/system/charon_monvesvrlog.service 2>/dev/null
            systemctl daemon-reload
          fi

          break
          ;;
        n|N)
          break
          ;;
        *)
          tput cuu1;tput ed
          ;;
      esac
    done
  fi

  #--- Charon-PAR removal
  KITBLD=0
  echo
  ANS="n"
  for KITNAM in "hpa" "charon-par" "charon-par-debuginfo" "charon-mtd" "charon-license" "aksusbd"
  do
    P=`rpm -q ${KITNAM} 2>/dev/null | grep "is not installed"`
    if test -z "${P}"
    then
      KITBLD=$(echo ${P} | cut -f4 -d'-')
      while test 1
      do
        if test "${KITNAM}" = "charon-par" -o "${KITNAM}" = "hpa"
        then
          echo -n "Do you want to uninstall ${FGBLUE}${KITNAM}[0m (y/n) ? "
          read ANS
        fi
        case "${ANS}"
        in
          y|Y)
            echo
            echo "[44m[37m Removing ${KITNAM} package ... [0m"
            sleep 1
            ${YUMC} -y remove ${KITNAM}
            break
            ;;
          n|N)
            break
            ;;
          *)
            tput cuu1;tput el
            ;;
        esac
      done
    fi
  done

  #--- aksusbd (HASP license) removal only (V3.0.7 and above)
  if test ${KITBLD} -ge 22100 -o ${KITBLD} -eq 0
  then
    echo
    KIT="aksusbd"
    KITNAM=${KIT}
    KITRPM=${KIT}
    P="V$(rpm -q ${KITRPM} | grep -v 'not installed' | cut -f2 -d'-')"
    if test "$P" != "V"
    then
      while test 1
      do
        echo -n "Do you want to uninstall ${FGBLUE}${KITNAM} ${P}[0m (y/n) ? "
        read ANS
        case "${ANS}"
        in
          y|Y)
            echo
            echo "[44m[37m Removing ${KITRPM} ... [0m"
            sleep 1
            ${YUMC} -y remove ${KITRPM}
            break
            ;;
          n|N)
            break
            ;;
          *)
            tput cuu1;tput ed
            ;;
        esac
      done
    fi
  fi

  if test ! -x /usr/sbin/aksusbd
  then
    if test -s /etc/systemd/system/charon_monusb.service
    then
      echo "Uninstalling aksusbd monitoring service..."
      systemctl stop  charon_monusb.service
      sleep 2
      systemctl disable charon_monusb.service
      sleep 1
      rm -f /etc/systemd/system/charon_monusb.service 2>/dev/null
      systemctl daemon-reload
    fi
  fi
}


#-----------------------------------------------------------------------------
# Main menu
#-----------------------------------------------------------------------------
while test 1
do
  get_preferences
  . ${CHARONDIR}/utils/charon_setlicmenu
  tput clear
  display_header "Install/Upgrade/Remove CHARON"
  echo -n "$(tput bold)Hostname$(tput sgr0):        "
  hostname
  tput sgr0
  echo -n "$(tput bold)Linux version$(tput sgr0):   "
  if test -e /etc/redhat-release
  then
    OS_RELEASE=/etc/redhat-release
  else
    OS_RELEASE=/etc/system-release
  fi
  cat ${OS_RELEASE}
  echo    "                 `uname -s` `uname -r`"
  tput sgr0
  echo

  if test "$1" = "el"
  then
    INS_OS="$1"
  else
    case "`cat ${OS_RELEASE} | awk '{print $1}'`"
    in
      "Red")
         INS_MAJOR=`cat ${OS_RELEASE} | sed "s=\(^.* release \)\(.*\)\(\..*$\)=\2=g"`
         INS_OS="el${INS_MAJOR}"
         ;;
      "CentOS")
         INS_MAJOR=`cat ${OS_RELEASE} | sed "s=\(^.* release \)\(.\)\(.*$\)=\2=g"`
         INS_OS="el${INS_MAJOR}"
         ;;
      "Rocky")
         INS_MAJOR=`cat ${OS_RELEASE} | sed "s=\(^.* release \)\(.\)\(.*$\)=\2=g"`
         INS_OS="el${INS_MAJOR}"
         ;;
      *)
         echo "Cannot determine which kit to use between Red Hat and Fedora. Please contact support!"
         sleep 2
         break
         ;;
    esac
  fi

  echo "$(tput bold)Installed packages$(tput sgr0)"

  typeset -i I=0
  for EMU in $(rpm -q -a | grep -e ^hpa -e ^charon -e ^aksusbd -e ^license-server | sort)
  do
    P=$(echo ${EMU} | sed -e "s=\.i386==g" -e "s=\.x86_64==g")
    case "$(echo $P | cut -f1 -d'-')"
    in
      aksusbd)
        echo "- [38;5;39m${P}[0m (HASP license)"
        ;;
      license)
        echo "- [38;5;39m${P}[0m (VE license - PAR 3.0.7 minimum)"
        ;;
      charon)
        case "$(echo $P | cut -f2 -d'-')"
        in
          par)
            case "$(echo $P | cut -f3 -d'-')"
            in
              debuginfo)
                echo "- ${FGORANGE}${P}[0m (Charon-PAR debugging tools)"
                ;;
              *)
                echo "- ${FGBLUE}${P}[0m (Charon-PAR emulators)"
                ;;
            esac
            ;;
          mtd)
            echo "- ${FGBLUE}${P}[0m (Charon Magnetic Tape Dump & Restore utility)"
            ;;
          *)
            echo "- ${FGBLUE}${P}[0m"
            ;;
        esac
        ;;
      xhpterm)
        echo "- ${P}[0m (Terminal emulation for block mode applications)"
        ;;
      *)
        echo "- ${P}[0m"
        ;;
    esac
    I=I+1
  done
  if test ${I} -eq 0
  then
    echo "${BGORANGE} WARNING [0m No one installed."
  fi

  tput sgr0
  echo

  CNT=${I}
  while test 1
  do
    echo -n "Enter your choice ($(tput bold)i$(tput sgr0) to install"
    test ${CNT} -gt 0 && echo -n ",$(tput bold)u$(tput sgr0) to uninstall"
    echo -n ",$(tput bold)r$(tput sgr0) to refresh,$(tput bold)q$(tput sgr0) to quit): "
    read ANS
    case "${ANS}"
    in
      i|I)
        select_kit_location

        if test -n "${KIT}"
        then
          if test "`echo ${KIT} | cut -c1-3`" = "hpa"
          then
            KITRPM=`ls ${KITLOC}/${KIT}/hpa-*.rpm 2>/dev/null`
          else
            KITRPM=`ls ${KITLOC}/${KIT}/charon-par-*.rpm 2>/dev/null`
          fi
          if test -z "${KITRPM}" -a -z "$(echo ${KIT} | grep 'license-server')"
          then
            echo "[41m[37m ERROR [0m"
            echo "Corrupted kit, no 'charon-par-*.rpm' file found."
          else
            charon_install ${KITLOC}/${KIT}

            if test $? = 0 -a -z "`echo ${PATH} | grep '/opt/charon'`"
            then
              if test "${SEMIGRAPH}" = "enabled"
              then
                X="`tput smacs`x`tput rmacs`"
                tput smacs
                echo "lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
                tput rmacs
              else
                X="|"
                echo "+--------------------------------------------------------------------------+"
              fi
              cat <<EOF
$X [31mPATH not set[0m, it is recommended to logoff and login before using Charon  $X
$X or to leave the menu then run:                                           $X
$X                                                                          $X
$X # [1m. /etc/profile.d/charon.sh[0m     (for CHARON-VAX & CHARON-AXP > V4.6)    $X
EOF
              if test "${SEMIGRAPH}" = "enabled"
              then
                tput smacs
                echo "mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
                tput rmacs
              else
                echo "+--------------------------------------------------------------------------+"
              fi
            fi
          fi
          Press_Enter
        fi
        break
        ;;
      u|U)
        charon_uninstall
        Press_Enter
        break
        ;;
      r|R)
        break
        ;;
      q|Q)
        break 2
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done
done

exit
