#!/bin/ksh -p
#
# ident "@(#)utsync_lib.ksh	1.4 04/01/20 SMI"
#
# Copyright 2002-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
###
#
# Description
#       This script library is shared (sourced) by:
#               utfwsync
#               utdssync
#
###


umask 022
PATH="/usr/sbin:/usr/bin:/bin"

CleanupAndExit() {
  [[ -n $TMP_FILE ]] && /bin/rm -f $TMP_FILE
  exit $1
}

Usage() {
  print -u2 "Usage: $PROGRAM_ID $PROGRAM_OPTS"
  CleanupAndExit 1
}

Error() {
  print -u2 "$PROGRAM_ID: error, $1"
  return 0
}

Fatal() {
  print -u2 "$PROGRAM_ID: fatal, $1"
  exit 1
}

CheckUidIsZero() {
  case "$(id)" in
    'uid=0('*) return 0;;  # uid is zero
    *)         Fatal "must be run as UID 0 (root)";;
  esac
}

GetGroup() {
  $TRACING

  $UTO_BASEDIR/sbin/utgstatus | sed -ne 's/\([^ ]*\)[ ][ ]*T.*/\1/p'

  return 0
}

StopGroup() {
  $TRACING
  
  typeset OUTPUT_CNTL="1>/dev/null 2>&1" # output supression

  print "\nStopping Authentication Managers on $GROUP ..."

  for HOST in $GROUP; do
    if $VERBOSE; then
      print "\nStopping host '$HOST'"
      OUTPUT_CNTL="" # no output supression
    fi
    eval $UTRCMD -n $HOST $UTO_BASEDIR/sbin/utfwadm -A -a -n all $OUTPUT_CNTL
    eval $UTRCMD -n $HOST $UTO_BASEDIR/lib/utauthd -e
  done

  return 0
}

RestartGroup() {
  $TRACING
  
  typeset OUTPUT_CNTL="1>/dev/null 2>&1" # output supression

  print "\nRestarting Authentication Managers ..."

  for HOST in $GROUP; do
    if $VERBOSE; then
      print "\nRestarting host '$HOST'"
      OUTPUT_CNTL="" # no output supression
    fi
    eval $UTRCMD -n $HOST /etc/init.d/utsvc restart $OUTPUT_CNTL
  done

  return 0
}

WaitPeriod() {
  typeset -i PERIOD_LEN=$2
  typeset -i SECONDS=$(expr $1 \* $PERIOD_LEN)

  print ""

  while (( $SECONDS > 0 )); do
    print -n "Will restart Authentication Managers in $SECONDS seconds  \r"
    sleep $PERIOD_LEN
    SECONDS=$( expr $SECONDS - $PERIOD_LEN )
  done

  print ""

  return 0
}
