#! /bin/csh -f
#
# confluent_a - Script for controlling licenses for Confluent products.
#
# Version 1.3
#
# Post:
#    status - return status:
#      0 = success
#      30 = no arguments specified
#      31 = unable to create key directory
#      32 = key product not specified
#      33 = invalid key product
#      34 = bury client IDs not specified
#      35 = man topic not specified
#      36 = invalid man topic
#      37 = invalid option
#      cmd_init:
#        10 = system not recognized
#        11 = confluent_* command not available
#      customer:
#        20 = no read access for customer file
#        21 = customer file is empty
#        22 = customer file does not exist
#        23 = no write access to $key_dir
#        24 = error creating the customer file
#        25 = no write access for customer file
#
# This routine uses the following utility scripts:
#   cmd_init - determine system directories and check for command existence
#   customer - get/set customer info
#
# Copyright (c) 1994 by
#
#    Confluent, Inc.
#    400 Spear St., Suite 207
#    San Francisco, CA 94105
#
# Voice: 415-764-1000
# Fax: 415-764-1008
# Internet: info@confluent.com
#
# Created: 940609 - jag
# Modified: 950104 - jag : move to share/bin, use cmd_init, change "usage"
#                          to "use", add access to confluent_* and customer
# Modified: 950302 - jag : set CONFLUENT_ELMHOST for servers and users options
# Modified: 950423 - jag : output version with usage, replace version with info
# Modified: 950620 - jag : add arguments to kill option and use confluent_kill
# Modified: 970609 - jag : add suffix argument to start option

# Set the product installation directory.
# This line is configured during installation.
set top_dir = "./util-1.4"

# Set up some configuration information.
set start_time = 0
set max_log = "100k"
set use_time = 10
set cf_version_d = "1.4"

# Set up the remaining directories:
#   bin_dir : license manager commands (set up by cmd_init)
#   man_dir : license manager man files
#   key_dir : log and key files
#   share_dir : utility scripts
set man_dir = $top_dir/share/man
set key_dir = $top_dir:h/license
set share_dir = $top_dir/share/bin

# Get the local host name.
set host = `uname -n`

# Set up the paths for the log and resource files.
set log_path = $key_dir/$host.log
set res_path = $key_dir/resource

# Set up the command name used to invoke this script.
set cmd = $0
set cmd = $cmd:t

# Indicate available usage options.
if ($#argv < 1) then
  echo ""
  echo "$cmd ($cf_version_d)"
  echo ""
  echo "usage: $cmd <option>"
  echo ""
  echo "<option> is one of the following:"
  echo ""
  echo "  Servers"
  echo "  -------"
  echo "  start [<time>] : start a license server on $host"
  echo "         <time> : startup time in seconds (default: $start_time)"
  echo "                  use 180 to reconnect clients on a network"
  echo "         use '$cmd man lm' for more information"
  echo "  servers : list the status of all license servers"
  echo "  kill [all | <name> ...] : kill the license server(s)"
  echo "         all    : kill on all available hosts"
  echo "         <name> : kill on host <name> (default: $host)"
  echo "         use '$cmd servers' to get a list of license servers"
  echo ""
  echo "  Codes and Keys"
  echo "  --------------"
  echo "  code : output the server code for $host"
  echo "         needed by Confluent to issue a license key"
  echo "  key <product> : install a license key for $host"
  echo "         install in $key_dir"
  echo "         <product> is one of:"
  echo "             vthought : Visual Thought (alias: vt)"
  echo "  keys [<mode>] : list installed key information"
  echo "         <mode> is one of:"
  echo "             detail : include key values"
  echo "             values : output only key values"
  echo ""
  echo "  Clients"
  echo "  -------"
  echo "  users : list all current users"
  echo "  bury <cid> ... : kill client(s) <cid>... on $host"
  echo "         use '$cmd users' to get a list of clients"
  echo "         use '$cmd man admin' for more information"
  echo ""
  echo "  Licenses"
  echo "  --------"
  echo "  tokens : list available licenses and expiration dates"
  echo "  status : list current license status on $host"
  echo "         use '$cmd man alert' for more information"
  echo "  use [<time>] : summarize current license activity on $host"
  echo "         <time> : summary interval in seconds (default: $use_time)"
  echo "         use '$cmd man usage' for more information"
  echo "  history [<unit>] [<detail>] : summarize historical license activity"
  echo "         <unit> : report interval - one of: month, hour, day"
  echo "         <detail> : user detail - one or more of: user, host"
  echo "         use '$cmd man rpt' for more information"
  echo ""
  echo "  Documentation and Advanced Use"
  echo "  ------------------------------"
  echo "  man <topic> : output the manual page for a license utility"
  echo "  <topic> : execute a license utility for advanced control"
  echo "         <topic> is one of:"
  echo "             admin    : control servers, install keys, list users"
  echo "             alert    : alert on license status"
  echo "             lm       : license server"
  echo "             rpt      : report historical license activity"
  echo "             usage    : report current license activity"
  echo "             ver      : extract license manager version from a file"
  echo "             resource : resource file format (only valid with 'man')"
  echo ""
  echo "  Miscellaneous"
  echo "  -------------"
  echo "  customer [set] : list the current customer information"
  echo "         set : prompt for replacement of the customer information"
  echo "  info : output version and directory information for the $cmd command"
  echo ""
  echo "arguments in [] are optional"
  echo ""

  exit (30)
endif

# If the key directory does not exist:
#  1. Remove it in case it is a regular file or a link to a nonexistent path.
#  2. Create it, giving full access to all users.
if (! -d $key_dir) then
  if (-w $key_dir:h) then
    echo "Creating the key directory $key_dir"
    rm -f $key_dir
    (umask 000; mkdir $key_dir)
  else
    echo "Cannot create the key directory $key_dir"
    exit (31)
  endif
endif

# Go through the available options.
switch ("$1")

  case start:
    # Get the start time, if specified.
    if ($#argv > 1) then
      set start_time = "$2"
    endif

    # Specify the resource file argument only if the file exists.
    if (-e $res_path) then
      set res = "-r $res_path"
    else
      set res = ""
    endif

    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_lm$3"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_lm$3 -e $key_dir -l $log_path -m $max_log \
                            $res -s $start_time
  breaksw

  case servers:
    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_admin"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    # Set CONFLUENT_ELMHOST so servers are found on other subnets.
    set servers = `cat $key_dir/*.lic |& sed -n -e '/^1 /s/^1 //p' | sort | uniq`
    set servers = `echo $servers | sed -e 's/ /:/g'`
    setenv CONFLUENT_ELMHOST "$servers"

    $bin_dir/confluent_admin -s
  breaksw

  case kill:
    if ($#argv < 2) then

      # Set up the system directory and check for existence of the command.
      set cmd_exec = "confluent_admin"
      source $share_dir/cmd_init
      if ($cmd_status) exit ($cmd_status)

      $bin_dir/confluent_admin -k

      # Get the port address.
      set port=`$bin_dir/confluent_admin -q|awk '/port.*default:/ {print $NF}'`

      # Remove the status file - left by an apparent bug in confluent_lm.
      rm -f /tmp/.elmd.$port

    else

      # Set up the system directory and check for existence of the command.
      set cmd_exec = "confluent_kill"
      source $share_dir/cmd_init
      if ($cmd_status) exit ($cmd_status)

      $bin_dir/confluent_kill $argv[2-]:q

    endif
  breaksw

  case code:
    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_admin"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_admin -c -e $key_dir -r -1
  breaksw

  case key:
    # The product must be specified.
    if ($#argv < 2) then
      echo "The product was not specified"
      exit (32)
    endif

    # Get the product and verify that it is valid.
    switch ("$2")

      case vt:
      case vthought:
        set product = vthought
      breaksw

      default:
        echo "Invalid product: $2"
        exit (33)
      breaksw

    endsw

    # Set the access for the new key file to ug=rw, o=r.
    # This does not seem to work.
    #umask 002

    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_admin"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_admin -c -e $key_dir -r 1 $product
  breaksw

  case keys:
    # Get the mode.
    switch ("$2")

      case detail:
      case values:
        set mode = "$2"
      breaksw

      default:
        set mode = ""
      breaksw

    endsw

    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_keys"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_keys $key_dir $mode
  breaksw

  case users:
    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_admin"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    # Set CONFLUENT_ELMHOST so servers are found on other subnets.
    set servers = `cat $key_dir/*.lic |& sed -n -e '/^1 /s/^1 //p' | sort | uniq`
    set servers = `echo $servers | sed -e 's/ /:/g'`
    setenv CONFLUENT_ELMHOST "$servers"

    $bin_dir/confluent_admin -l
  breaksw

  case bury:
    # At least one client must be specified.
    if ($#argv < 2) then
      echo "No client IDs were specified"
      exit (34)
    endif

    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_admin"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    # Go through each specified client.
    foreach id ($argv[2-])
      $bin_dir/confluent_admin -b $id
    end
  breaksw

  case tokens:
    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_rpt"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_rpt -l $key_dir
  breaksw

  case status:
    cd $key_dir

    # Determine whether there are any potential key files present.
    ls *.lic >& /dev/null
    set flag = $status

    # Get the list of keys.
    # Set the flag if there are no valid key files.
    if (! $flag) then
      set keys = `grep -l $host *.lic | sed -n -e 's/^\([0-9]\{2,\}\)\.lic$/\1/p'`
      if ("$keys" == "") set flag = 1
    endif

    if ($flag) then
      echo "No key files for $host in $key_dir"
    else
      # Set up the system directory and check for existence of the command.
      set cmd_exec = "confluent_alert"
      source $share_dir/cmd_init
      if ($cmd_status) exit ($cmd_status)

      $bin_dir/confluent_alert -v $keys
    endif
  breaksw

  case use:
    # Get the use time, if specified.
    if ($#argv > 1) then
      set use_time = "$2"
    endif

    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_usage"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_usage -b $use_time
  breaksw

  case history:
    # Determine the level of output detail.
    set unit = ""
    set detail = ""
    set detailu = 0
    set detailh = 0
    foreach arg ($argv[2-])
      if (("$arg" == "month") || ("$arg" == "day") || ("$arg" == "hour")) then
        set unit = "-t $arg"
      endif
      if ("$arg" == "user") set detailu = 1
      if ("$arg" == "host") set detailh = 1
    end
    if ($detailu || $detailh) then
      set detail = "-u "
      if ($detailu) set detail = "${detail}u"
      if ($detailh) set detail = "${detail}h"
    endif

    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_rpt"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_rpt $unit $detail $key_dir
  breaksw

  case man:
    # The topic must be specified.
    if ($#argv < 2) then
      echo "The topic was not specified"
      exit (35)
    endif

    # Get the topic and verify that it is valid.
    switch ("$2")

      case admin:
      case alert:
      case lm:
      case rpt:
      case usage:
      case ver:
        set topic = "$2"
        set section = "1"
      breaksw

      case resource:
        set topic = "$2"
        set section = "5"
      breaksw

      default:
        echo "Invalid topic: $2"
        exit (36)
      breaksw

    endsw

    echo ""
    more $man_dir/confluent_$topic.$section
  breaksw

  case admin:
  case alert:
  case lm:
  case rpt:
  case usage:
  case ver:
    # Set up the system directory and check for existence of the command.
    set cmd_exec = "confluent_$1"
    source $share_dir/cmd_init
    if ($cmd_status) exit ($cmd_status)

    $bin_dir/confluent_$1 $argv[2-]:q
  breaksw

  case customer:
    # Determine whether customer information is modified.
    @ customer_set = "$2" == "set"

    source $share_dir/customer
    if ($cmd_status) exit ($cmd_status)
  breaksw

  case info:
    echo ""
    echo "Version: $cf_version_d"
    echo "Installation directory: $top_dir"
    echo "License directory: $key_dir"
    echo ""
  breaksw

  default:
    echo "Invalid option: $1"
    exit (37)
  breaksw

endsw

exit (0)
