#!/bin/sh
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)active.sh	1.4 12/06/99 Sun Microsystems, Inc.
#

PATH="/usr/bin"
export PATH

SS_LIBDIR=/opt/SUNWicg/SunScreen/lib
SS_ETCDIR=/etc/opt/SUNWicg/SunScreen
CONFIGS=$SS_ETCDIR/configs
ACTIVE=$SS_ETCDIR/.active
read SCREENNAME <$SS_ETCDIR/name

set -e

domain=default
extract=false
force=false
unset policy
unset args

not()
{
	if "$@"
	then return 1
	else return 0
	fi
}

set_num=31	# SI18N_ADMIN_SCRIPTS
LS()		# localize a string
{
	msg_num=$1; shift
	if [ -x ${SS_LIBDIR}/catgets ]
	then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
	else echo "$*"
	fi
}

while getopts ZfxD:d: c
do case $c in
   Z) args=-Z;;
   f) force=true;;
   x) extract=true;;
   D|d) domain=$OPTARG;;
   *) echo `LS 50 "Usage: ssadm active [-x policy]"`; exit 1;;
   esac
done
[ $OPTIND -eq 1 ] || shift `expr $OPTIND - 1`
policy=$1

# things that shouldn't be extracted
policypattern='^@origin |^[A-Z_]*_VERSION '
pattern='^[A-Z_]*_VERSION '

if $extract
then if [ -n "$policy" ]
     then if [ ${SSADM_ACCESS_LEVEL:-WRITE} != "WRITE" ]
	  then echo>&2 `LS 51 "ssadm active: Access denied"`; exit 1
	  fi
	  if [ -f "$CONFIGS/$domain/$policy" ] && not $force
	  then f=`LS 52 "ssadm active: Error: Policy \"%s\" already exists."`
	       printf>&2 "$f\n" "$policy"
	       exit 1
	  fi
	  egrep -v "$policypattern" $ACTIVE/Configuration > "$CONFIGS/$domain/$policy"
	  $SS_LIBDIR/logmsg -A active -F NOTICE -P active "reason:utf8=\"extracted $SCREENNAME $domain $policy\"; user:utf8=\"$user\""
     else exec egrep -v "$pattern" $ACTIVE/Configuration
     fi
else exec ${SS_LIBDIR}/ss_active_config $args get
fi
