#!/bin/sh

#Functions
clrscreen()
{
   clear
}
processdongleidinfo()
{
   countvaliddongleids=0
   getvaliddongleids
   if [ $countvaliddongleids -eq 1 ]; then
      echo 
      echo
      echo "  A FLEXID9 dongle with ID ${singlevaliddongleiddisplaytext} was detected on this machine."
      echo
      while :
      do
         inquire ' Do you want to use its ID as your FLEXlm (FlexNet) hostid? (yes/no) '
         read response 
         case $response in
            Y | y | YES | yes )
               fidtype="F"
               idtouse=$validdongleidlist
               break
               ;;
            N | n | NO | no )
               break
               ;;
            * )
               ;; 
        esac
      done
   elif [ $countvaliddongleids -gt 1 ]; then
      echo 
      echo '  Multiple FLEXID9 dongles were detected on this machine.'
      while :
      do
         inquire ' Do you want to use one of their IDs as your FLEXlm (FlexNet) hostid? (yes/no) '
         read response
         case $response in
            Y | y | YES | yes )
               fidtype="F"
               echo
               echo "  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
               echo
               echo "   THE FOLLOWING FLEXID9 DONGLES WERE DETECTED ON THIS MACHINE: "
               echo "    (All hyphens were removed from the IDs in the list below.)"
               echo -e "$validdongleiddisplaylist"
               echo
               echo
               while :
               do
                  inquire2 "  Enter the list number of the ID to use as your FLEXlm (FlexNet) hostid " "(1-${countvaliddongleids}) "
                  read selecteddongleidlistitemnum
                  if [ "$selecteddongleidlistitemnum" != "" ] &&
                     [ -z "`echo \"$selecteddongleidlistitemnum\" | tr -d \"[0-9\.]\"`" ]; then
                     if [ $selecteddongleidlistitemnum -ge 1 ] && 
                        [ $selecteddongleidlistitemnum -le $countvaliddongleids ]; then
                        break
                     fi
                  fi
               done
               tmpcnt=0
               for dongleid in $validdongleidlist
               do
                  tmpcnt=`expr ${tmpcnt} + 1`
                  if [ $tmpcnt -eq $selecteddongleidlistitemnum ]; then
                     break
                  fi
               done
               idtouse=$dongleid
               echo
               echo
               echo "  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
               break
               ;;
            N | n | NO | no )
               break
               ;;
            * )
               ;;
         esac
      done
      echo 
      echo
   elif [ "$debugVal" = "1" ]; then
      prtc
      echo
   fi
}
ifDebugShowDongleIssuesText()
{
   if [ "$debugVal" = "1" ]; then
      echo "DEBUG: If you feel that a FLEXID9 dongle ID should have been found,"
      echo "       some possible causes of failure are as follows:"
      echo "       -- The required dongle driver is not installed on the machine."
      echo "       -- The existing dongle driver needs to be upgraded."
      echo "       -- The dongle is not attached to the machine."
      echo "       -- An incorrect version of the FlexNet lmutil utility was used."
      echo
   fi
}
formatOutputFromCommand()
{
echo 
echo "     _____________________________________________________________________________________"
echo
#echo -e "$fulldongleidtext" | awk '{ print "        ", $0 }'
echo -e "$1" | awk '{ print "        ", $0 }'

echo "     _____________________________________________________________________________________"
echo
}
getvaliddongleids()
{
   rawdongleids=""
   getdongleidinfocmd="./lmutil lmhostid -flexid -n"
   if [ "$debugVal" = "1" ]; then 
      echo
      echo
      echo "DEBUG: NOTE: The ANSYS, Inc. License Manager supports FlexNet ID USB port"
      echo "       (FLEXID9) dongles. For these dongles, the FlexNet utilities return"
      echo "       IDs that begin with 'FLEXID=9-'."
      echo
      echo
      echo "DEBUG: Command issued to retrieve FLEXID9 dongle IDs: $getdongleidinfocmd"
      prtc
      echo
      echo
   fi
   fulldongleidtext=`$getdongleidinfocmd`
   validdonglecheck=`echo $fulldongleidtext | grep -c "FLEXID=9-"`	
   if [ "$validdonglecheck" != "0" ]; then
      rawdongleids=`echo $fulldongleidtext | cut -d \" -f2`
      countvaliddongleids=0
      validdongleidlist=""
      for rawdongleid in `echo $rawdongleids`
      do
         fixeddongleid=`echo $rawdongleid | sed 's/FLEXID=9-/9/'`
         # If the strings are the same in the check below, the ID doesn't have
         # 'FLEXID=9-' in it and is, therefore, not a valid FLEXID9 dongle ID.
         if [ "$rawdongleid" != "$fixeddongleid" ]; then
            countvaliddongleids=`expr ${countvaliddongleids} + 1`
            validdongleidlist="${validdongleidlist} ${fixeddongleid}"
            validdongleiddisplaylist="${validdongleiddisplaylist}\n       ${countvaliddongleids}. ${fixeddongleid}"
            # Next string is only used if there is only one valid dongle ID.
            curdongleiddisplaytext="${rawdongleid} (${fixeddongleid})"
         fi
      done
      if [ "$countvaliddongleids" = "1" ]; then
         singlevaliddongleiddisplaytext=$curdongleiddisplaytext
      fi
      if [ "$debugVal" = "1" ]; then 
         echo "DEBUG: A FLEXID9 dongle was found. The dongle information retrieved was as follows:"
         echo "          $fulldongleidtext"
         echo
         if [ "$sysdir" = "linx64" ]; then
            echo "     _____________________________________________________________________"
            echo 
            echo "       NOTE: Due to a dongle limitation, the 64-bit Linux version of"
            echo "       the FLEXlm license manager does not support multiple FLEXID9"
            echo "       dongles. Therefore, the FlexNet lmutil utility that was used to"
            echo "       retrieve the dongle information cannot retrieve ID information"
            echo "       for multiple dongles."
            echo
            echo "       *** CAUTION ***"
            echo 
            echo "       If you have multiple FLEXID9 dongles attached to the machine"
            echo "       and the ID of the dongle that you wish to use was not retrieved"
            echo "       by the utility, you will need to remove all but the desired"
            echo "       dongle from the machine and run this utility again. HOWEVER,"
            echo "       removing a dongle could cause serious disruption to any software"
            echo "       that is tied to it.  Therefore, do not remove other dongles until"
            echo "       it has been determined that it is safe to do so."
            echo
            echo
            echo "       Please note that you could encounter similar issues when running"
            echo "       the FLEXlm component of the ANSYS, Inc. License Manager with a"
            echo "       license file tied to a FLEXID9 dongle."
            echo "     _____________________________________________________________________"
            echo
         fi
      fi
   elif [ "$debugVal" = "1" ]; then
      echo "DEBUG: A FLEXID9 dongle ID was not found. The following information was"
      echo "       returned from the command issued to retrieve FLEXID9 dongle IDs:"
      formatOutputFromCommand "$fulldongleidtext"
      echo
      ifDebugShowDongleIssuesText
      echo
   fi
}
processsystemidinfo()
{
   fidtype="U"
   if [ $usesetherid -eq 0 ]; then
      idtouse=`./lmutil lmhostid -n | cut -d\" -f2 | cut -d' ' -f1`
   else
      processetheridinfo 
   fi
}
processetheridinfo()
{
   countvalidetherids=0
   getvalidetherids
   if [ $countvalidetherids -eq 1 ]; then
      idtouse=$validetherids
   elif [ $countvalidetherids -gt 1 ]; then
      echo
      echo
      echo "  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      echo
      echo "   THE FOLLOWING PHYSICAL ETHERNET ADDRESSES WERE DETECTED ON THIS MACHINE:"
      echo -e "$etheriddisplaylist"
      echo
      while :
      do
         echo "  Enter the above list number (1-${countvalidetherids}) of the ID to use as your FLEXlm (FlexNet) hostid..."
         inquire2 "  (Please contact your company's IT Department if you need assistance.) (1-${countvalidetherids}) " 
         read selectedetheridlistitemnum
         if [ "$selectedetheridlistitemnum" != "" ] &&
            [ -z "`echo \"$selectedetheridlistitemnum\" | tr -d \"[0-9\.]\"`" ]; then
            if [ $selectedetheridlistitemnum -ge 1 ] && 
               [ $selectedetheridlistitemnum -le $countvalidetherids ]; then
               break
            fi
         fi
      done
      tmpcnt=0
      for etherid in $etheridlist
      do
         tmpcnt=`expr ${tmpcnt} + 1`
         if [ $tmpcnt -eq $selectedetheridlistitemnum ]; then
            break
         fi
      done
      idtouse=$etherid
      echo
      echo
      echo "  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      echo
      echo
   elif [ $debugVal -eq 1 ]; then
      prtc
      echo
   fi 
}
getvalidetherids()
{
   validetherids=""
   getetheridinfocmd="./lmutil lmhostid -n"
   if [ "$debugVal" = "1" ]; then
      echo
      echo "DEBUG: Command issued to retrieve the machine's physical Ethernet addresses: $getetheridinfocmd"
      echo
      prtc
      echo
   fi
   fulletheridtext=`$getetheridinfocmd`
   texttotest=`echo $fulletheridtext | sed 's!/[a-fA-F0-9\" ]*$!!'`
   if [ "$fulletheridtext" = "$texttotest" ]; then
      validetherids=`echo $fulletheridtext | cut -d \" -f2`
      countvalidetherids=0
      etheridlist=""
      for etherid in `echo $validetherids`
      do
         countvalidetherids=`expr ${countvalidetherids} + 1`
         etheridlist="${etheridlist} ${etherid}"
         etheriddisplaylist="${etheriddisplaylist}\n       ${countvalidetherids}. ${etherid}"
      done
   elif [ "$debugVal" = "1" ]; then
      echo "DEBUG: A physical Ethernet address was not found on the machine. The following"
      echo "       information was returned from the command issued to retrieve the physical"
      echo "       Ethernet address ID(s):"
      formatOutputFromCommand "$fulletheridtext"
      echo
   fi
}
echo_multiline()
{
   if [ $echoflag -eq 1 ]; then
      echo -e "$1 \c"
   else 
      echo -en "$1 "
   fi
}
inquire()
{
   if [ $echoflag -eq 1 ]; then
      echo " $1: \c"
   else
      echo -n " $1: "
   fi
}
inquired()
{
   if [ $echoflag -eq 1 ]; then
      echo " $1 [$2] : \c"
   else
      echo -n " $1 [$2] : "
   fi
}

inquire2()
{
   if [ $echoflag -eq 1 ]; then
      echo " ${1}${2}: \c"
   else
      echo -n " ${1}${2}: "
   fi
}
nosupport()
{
   echo
   echo " ANSYS is not supported on this system.  Please contact your ANSYS"
   echo " sales representative for a list of supported hardware platforms."
   echo
   exit
}
prtc() 
{
   echo; echo
   inquire ' <Press RETURN to Continue> '
   read rtrn
   rtrn=""
}

scriptName="getFLEXid"
debugVal=0
for mopt in $*
do
   case $mopt in
      -debug )
         debugVal=1
         ;;
      *)
         echo
         echo "     ERROR: Usage:"
         echo "        $scriptName -debug"
         echo "     Command line was as follows:"
         echo "        $scriptName $@"   
         echo "The $scriptName utility is exiting due to one or more invalid command line options being specified on the command line."
         echo
         exit 1
         ;;
   esac
done
if [ -d ./lmutil ]
then
   echo 
   echo " Required file lmutil is a directory rather than an"
   echo " executable as is required.  This is a fatal error."
   echo
   exit
fi
if [ ! -x ./lmutil ]
then
   echo
   echo " Required file lmutil either does not exist or is not"
   echo " an executable as is required.  This is a fatal error."
   echo
   exit
fi

# Get platform type
SYS=`uname -s`
case $SYS in
   SunOS )
      case `uname -i` in
         FJSV* )
            SYS="SOLUS64"
            ;;
         i86pc* )
            nosupport
            ;;
         * )
            whatsun=`isalist |grep sparcv9`
            if [ $? -eq 0 ]; then
               SYS="SOLUS64"
            else
               nosupport
            fi
            ;;
      esac
      ;;
   HP-UX )
      nosupport
      ;;
   AIX )
      nosupport
      ;;
   Linux )
      i=`uname -m`
      case $i in
         ia64)
            nosupport
            ;;
         x86_64)
            SYS="LINX64"
            ;;
         *)
            SYS="LIN32"
            ;;
      esac
      ;;
esac

#
# SET SYSTEM DEPENDENT VARIABLES 
sysdir=""
echoflag=1
hname=`uname -n`
supportsdongles=0
usesetherid=0
case "$SYS" in
   SOLUS64 )
      sysdir=solus64
      MACH="Sun SPARC 64"
      ;;
   LIN32 )
      echoflag=0
      sysdir=lin32
      MACH="Linux 32"
      supportsdongles=1
      usesetherid=1
      ;;
   LINX64 )
      echoflag=0
      sysdir=linx64
      MACH="Linux x64"
      supportsdongles=1
      usesetherid=1
      ;;
esac

clrscreen
echo
echo " >>>---> Register license server information:"
echo
inquire ' Enter your customer number '
read custnum
while :
do
   inquire ' Enter your company name '
   read coname
   if [ ! -z "$coname" ]; then
      break
   fi
done
while :
do
   inquire ' Enter your name '
   read yourname
   if [ ! -z "$yourname" ]; then
      break
   fi
done
while :
do
   inquire ' Enter your telephone number '
   read phonenum
   if [ ! -z "$phonenum" ]; then
      break
   fi
done
inquire ' Enter your E-mail address '
read emailaddr 
inquire ' Enter your FAX number '
read faxnum
while :
do
   inquire ' Is this system a single server or a member of a three server setup? (1/3) '
   read oneorthree
   case $oneorthree in
      1 | 3 )
         break
         ;;
      * )
         ;;
   esac
done 
while :
do
   if [ $oneorthree -eq 3 ]; then
      while :
      do
         inquire ' Enter the three hostnames (separate with spaces) '
         read thehosts
         counthosts=0
         for eachhost in $thehosts
         do
            counthosts=`expr $counthosts + 1`
         done
         if [ "$counthosts" -eq 3 ]; then
            break
         fi 
      done
   else
      tmphosts=`uname -n`
      inquired ' Enter the hostname' ${tmphosts}
      read thehosts
      if [ -z "$thehosts" ]; then
         thehosts="$tmphosts"
      fi
   fi
   if [ ! -z "$thehosts" ]; then
      break
   fi
done
if [ "$sysdir" = "lin32" ] || [ "$sysdir" = "linx64" ]; then
   echo 
   echo "  Select hardware type for this license server machine:"
   echo
   echo "       1. Compaq"
   echo "       2. Dell"
   echo "       3. HP"
   echo "       4. IBM"
   echo "       5. SGI"
   echo "       6. Sun"
   echo "       7. Other"
   echo
   while :
   do
      inquire ' Enter the hardware type (1-7) '
      read hwnum
      case $hwnum in
         1 )
            linuxhw="$MACH - Compaq"
            break
            ;;
         2 )
            linuxhw="$MACH - Dell"
            break
            ;;
         3 )
            linuxhw="$MACH - HP"
            break
            ;;
         4 )
            linuxhw="$MACH - IBM"
            break
            ;;
         5 )
            linuxhw="$MACH - SGI"
            break
            ;;
         6 )
            linuxhw="$MACH - Sun"
            break
            ;;
         7 )
            linuxhw="$MACH - Other"
            break
            ;;
         * )
            ;;
      esac
   done
fi
if [ $supportsdongles -eq 1 ]; then
   processdongleidinfo
fi
if [ "$idtouse" = "" ]; then
   processsystemidinfo
fi
osrev=`uname -sr`
todaysdate=`date`
echo "CUSTOMER: $custnum" > ./licserver.info
echo "COMPANY: $coname" >> ./licserver.info
echo "NAME: $yourname" >> ./licserver.info
echo "PHONE: $phonenum" >> ./licserver.info
echo "EMAIL: $emailaddr" >> ./licserver.info
echo "FAX: $faxnum" >> ./licserver.info
echo "NUMSERVERS: $oneorthree" >> ./licserver.info
echo "QUORUM: $thehosts" >> ./licserver.info
echo "HOSTNAME: $hname" >> ./licserver.info
if [ "$sysdir" = "lin32" ] || [ "$sysdir" = "linx64" ]; then
   echo "PLATFORM: $linuxhw" >> ./licserver.info
else
   echo "PLATFORM: $MACH" >> ./licserver.info
fi
echo "OS: $osrev" >> ./licserver.info
echo "FLEXID: $idtouse" >> ./licserver.info
echo "IDTYPE: $fidtype" >> ./licserver.info
echo "DATE: $todaysdate" >> ./licserver.info 
echo
echo " ... Writing ./licserver.info "
sleep 5
clrscreen
cat ./licserver.info
