# customer - Script for setting the Confluent customer info.
#
# Version 1.3
#
# Pre:
#    key_dir - license directory (e.g., ".../license")
#    customer_set - 1 => modify the contents of the customer file
# Post:
#    cmd_status - return status:
#      0 = success
#      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 is sourced from csh.
#
# 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: 940915 - wpt
# Modified: 940917 - jag
# Modified: 950107 - jag : move to share/bin, source by confluent_a

# Full path to customer file name.
set cu_file = $key_dir/customer.txt

# Set the default return value.
set cmd_status = 0

# Determine the state of the file.
# An empty file takes precedence over whether it is readable.
if (-e $cu_file) then
  if (! -r $cu_file) set cmd_status = 20
  if (-z $cu_file) set cmd_status = 21
else
  set cmd_status = 22
endif

echo ""

# Output messages if there are problems with access to the file.
if ($cmd_status) then

  if ($cmd_status == 20) then
    echo "You do not have read access for the customer file:"
    echo ""
    echo "   $cu_file"
  endif

  # The other conditions are errors if the contents are not being modified.
  if (! $customer_set) then

    if ($cmd_status == 21) then
      echo "The customer file is empty:"
      echo ""
      echo "   $cu_file"
    endif

    if ($cmd_status == 22) then
      echo "The customer file does not exist:"
      echo ""
      echo "   $cu_file"
    endif

  endif

# Otherwise, output the file.
else
  echo "The current contents of the customer file are:"
  echo ""
  cat $cu_file
endif

# Quit if the contents is not being modified.
if (! $customer_set) then
  echo ""
  exit
endif

# Reset the default return value.
set cmd_status = 0

# The license directory must be writable.
# It is guaranteed by confluent_a to exist and be a directory.
if (! -w $key_dir) then
  echo ""
  echo "You do not have write access to the license directory containing"
  echo "the customer file:"
  echo ""
  echo "   $key_dir"
  echo ""
  set cmd_status = 23
  exit
endif

# Create the customer file if it does not exist.
if (! -e $cu_file) then

  # Create an empty file.
  touch $cu_file

  # If it still does not exist, some error occurred other than a non-writable
  # directory.
  if (! -e $cu_file) then
    echo ""
    echo "An error occurred attempting to create a new customer file:"
    echo ""
    echo "   $cu_file"
    echo ""
    set cmd_status = 24
    exit
  endif
endif

# The customer file must be writable.
if (! -w $cu_file) then
  echo ""
  echo "You do not have write access for the customer file:"
  echo ""
  echo "   $cu_file"
  echo ""
  set cmd_status = 25
  exit
endif

# Warn that a non-empty file will be replaced.
if (! -z $cu_file) then
  echo ""
  echo "Continuing will overwrite the current contents of your customer file."
endif

start_mark:

# Output instructions.
echo ""
echo "Please enter your name, organization, and Customer ID below."
echo "To abort this script at any time, please type ctrl-C and press <return>."
echo ""

# Get the user name.
name_mark:
echo -n "Name?  "
set cu_name = $<
if ("$cu_name" == "") then
  echo ""
  echo "You entered an empty name\!  Please try again."
  echo ""
  goto name_mark
endif

# Get the organization.
org_mark:
echo -n "Organization?  "
set cu_org = $<
if ("$cu_org" == "") then
  echo ""
  echo "You entered an empty organization\!  Please try again."
  echo ""
  goto org_mark
endif

# Get the customer ID.
cust_mark:
echo -n "Customer ID (in the form C-123456)?  "
set cu_cid = $<
if ("$cu_cid" == "") then
  echo ""
  echo "You entered an empty Customer ID\!  Please try again.  If you have"
  echo "problems finding or entering your Customer ID, please contact"
  echo "Confluent at 415-764-1000 or by e-mail at support@confluent.com."
  echo ""
  goto cust_mark
endif

# The value must be in the form C-xxxxxx.
set cu_value = `echo $cu_cid | tr '[a-z]' '[A-Z]'`
set cu_result = `echo $cu_value | sed -n -e '/^C-[0-9]\{6\}$/p'`
if ("$cu_value" != "$cu_result") then
  echo ""
  echo "Your Customer ID must be in the form:"
  echo ""
  echo "   C-123456"
  echo ""
  echo "Please try again.  If you have problems finding or entering your"
  echo "Customer ID, please contact Confluent at 415-764-1000 or by e-mail"
  echo "at support@confluent.com."
  echo ""
  goto cust_mark
endif

# OK, we've got everything.  Print the info and allow the customer to
# correct any errors.
echo ""
echo "Your customer file will contain this information:"
echo ""
echo "$cu_name"
echo "$cu_org"
echo "$cu_value"
echo ""
echo "Is this correct?  If so, please press <return> to write the information"
echo "to your customer file, or anything else and <return> to try again."
echo -n "[y] -> "
set cu_input = $<
if ("$cu_input" != "") goto start_mark
echo "-> y"

# Save the old file.
mv $cu_file $cu_file.old

# Create the new one.
echo "$cu_name"   >! $cu_file
echo "$cu_org"   >>! $cu_file
echo "$cu_value" >>! $cu_file

# Output verification.
echo ""
echo "Information saved to $cu_file."

exit
