#!/bin/false # This is a ksh script library, to be sourced by utconfig, etc.
#
# ident "@(#)config_lib.ksh	1.5	10/12/20 Oracle"
#
# Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
## Helper functions for input validation
#

# An alphanumeric identifier (C-style) of up to 24 characters
IsValidAlphanumeric()
{
   (($# == 1 && ${#1} <= 24)) || return 1

   param=$(echo $1 | tr ' ' '\000')
   [[ $param == [a-zA-Z0-9]+([_a-zA-Z0-9-]) && $1 != *- ]]
}

# A valid positive integer (no sign includede)
IsValidInteger()
{
   (($# == 1 && ${#1} <= 24)) || return 1

   param=$(echo $1 | tr ' ' '\000')
   [[ $param == +([0-9]) ]]
}

# A valid unix group name on both Linux and Solaris
IsValidGroup()
{
   (($# == 1)) || return 1

   param=$(echo $1 | tr ' ' '\000')
   #
   ## Rules/restrictions for group names on Solaris and Linux differ.
   ## The following is a lowest common denominator compromise
   #
   [[ $param == [a-z]*([a-z0-9]) ]]
}


#
## GroupExists
#
## Checks whether a name designates an existing unix group
#
GroupExists()
{
   (($# == 1)) || return 1

   getent group "$1" > /dev/null 2>&1 
}

# ReplyYesOrNo
# $1 - reply key
# $2 - default
# Returns 0 on yes, 1 on no
ReplyYesOrNo() {
  key=$1
  default=$2
  shift 2

  $UTLIBDIR/utdialog $UTDIALOG_OPTS -H YesOrNo:$default utconfig.$key $* | read
  case "$REPLY" in
    N) return 1;;
    Y) return 0;;
  esac
  return 2
}

UTLIBDIR=/etc/opt/SUNWut/basedir/lib
