#!/bin/sh
#
# Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston MA 02111-1307, USA.
# 
# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
# Mountain View, CA  94043, or:
# 
# http://www.sgi.com 
# 
# For further information regarding this notice, see: 
# 
# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
#
# $Id: pmnsadd,v 1.2 2001/06/27 06:25:38 kenmcd Exp $
#
# Add a subtree of new names into the namespace in the current directory
#
#Tag 0x00010D13


# source the PCP configuration environment variables
. /etc/pcp.env

exitsts=1
tmp=$PCP_TMP_DIR/$$
prog=`basename $0`
trap "rm -f $tmp.*; exit \$exitsts" 0 1 2 3 15

_usage()
{
    echo "Usage: pmnsadd [-d] [-n namespace] file"
}

namespace=${PMNS_DEFAULT-$PCP_VAR_DIR/pmns/root}
dupok=""
umask 22		# anything else is pretty silly

while getopts dn:\? c
do
    case $c
    in
	d)	dupok="-d"
		;;
	n)	namespace=$OPTARG
		;;
	\?)	_usage
		exitsts=0
		exit
		;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -ne 1 ]
then
    _usage
    exit
fi

if [ ! -f $namespace ]
then
    echo "$prog: cannot find PMNS file \"$root\""
    exit
fi

if [ ! -w $namespace ]
then
    echo "$prog: cannot open PMNS file \"$root\" for writing"
    exit
fi

if [ ! -f $1 ]
then
    echo "$prog: cannot find input file \"$1\""
    exit
fi

# Find PMNS pathname for base of new subtree (subroot), construct upper
# levels of PMNS as required and hand-off to pmnsmerge
#
subroot=`$PCP_AWK_PROG <$1 'NF >= 2 && $2 == "{" { print $1 ; exit }'`

echo 'root {' >$tmp.tmp
path=""
for name in `echo "$subroot" | tr '.' ' '`
do
    [ ! -z "$path" ] && echo "$path {" >>$tmp.tmp
    echo "	$name" >>$tmp.tmp
    echo "}" >>$tmp.tmp
    if [ -z "$path" ]
    then
	path="$name"
    else
	path="$path.$name"
    fi
done
cat $1 >>$tmp.tmp

# try to preserve mode, owner and group for the new output files
#
rm -f $namespace.new $namespace.new.bin
[ -f $namespace ] && cp -p $namespace $namespace.new
[ -f $namespace.bin ] && cp -p $namespace.bin $namespace.new.bin

$PCP_BINADM_DIR/pmnsmerge -f $dupok $namespace $tmp.tmp $namespace.new
exitsts=$?

# from here on, ignore SIGINT, SIGHUP and SIGTERM to protect
# the integrity of the new ouput files
#
trap "" 1 2 15

if [ $exitsts = 0 ]
then
    mv $namespace.new.bin $namespace.bin
    mv $namespace.new $namespace
else
    echo "$prog: No changes have been made to the PMNS file \"$namespace\""
    rm -f $namespace.new $namespace.new.bin
fi
