#! /bin/sh
#Tag 0x00010D13
# Copyright 1995,2003 Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# 
# UNPUBLISHED -- Rights reserved under the copyright laws of the United
# States.   Use of a copyright notice is precautionary only and does not
# imply publication or disclosure.
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
# in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
# in similar or successor clauses in the FAR, or the DOD or NASA FAR
# Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
# 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
# 
# THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
# INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
# DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
# PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
# GRAPHICS, INC.
#
# merge a group of logfiles, e.g. all those for today
#
# default case, w/out arguments uses the default pmlogger filename
# conventions for today's logs, namely `date +%Y%m%d` for both the
# input-basename and the output-name
#

# Get standard environment
. /etc/pcp.env


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

force=false
VERBOSE=false
SHOWME=false
RM=rm

_abandon()
{
    echo "$prog: These error(s) are fatal, no output archive has been created."
    status=1
    exit
}

_warning()
{
    echo "$prog: Trying to continue, although output archive may be corrupted."
    force=false
}

usage="Usage: $prog [-fNV] [input-basename ... output-name]"

# option parsing
#
while getopts fNV c
do
    case $c
    in

	f)	force=true
		;;

	N)	SHOWME=true
		RM="echo + rm"
		;;

	V)	VERBOSE=true
		;;

	\?)	echo "$usage"
		_abandon
		;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -eq 0 ]
then
    trylist=`date +%Y%m%d`
    output=$input
elif [ $# -ge 2 ]
then
    trylist=""
    while [ $# -ge 2 ]
    do
	trylist="$trylist $1"
	shift
    done
    output="$1"
else
    echo "$usage"
    status=1
    exit
fi

fail=false
mergelist=""

# handle dupicate-breaking name form of the base name
# i.e. YYYYMMDD.HH.MM-seq# and ensure no duplicates
#
rm -f $tmp.input
echo >$tmp.input
for try in $trylist
do
    grep "^$try\$" $tmp.input >/dev/null || echo "$try" >>$tmp.input
    for xxx in $try-*.index
    do
	[ "$xxx" = "$try-*.index" ] && continue
	tie=`basename $xxx .index`
	grep "^$tie\$" $tmp.input >/dev/null || echo "$tie" >>$tmp.input
    done
done

for input in `cat $tmp.input`
do
    for file in $input.index
    do
	file=`basename $file .index`
	empty=0
	if [ ! -f "$file.index" ]
	then
	    echo "$prog: Error: \"index\" file missing for archive \"$file\""
	    fail=true
	elif [ ! -s "$file.index" ]
	then
	    empty=`expr $empty + 1`
	fi
	if [ ! -f "$file.meta" ]
	then
	    echo "$prog: Error: \"meta\" file missing for archive \"$file\""
	    fail=true
	elif [ ! -s "$file.meta" ]
	then
	    empty=`expr $empty + 1`
	fi
	if [ ! -f "$file.0" ]
	then
	    echo "$prog: Error: \"volume 0\" file missing for archive \"$file\""
	    fail=true
	elif [ ! -s "$file.0" ]
	then
	    empty=`expr $empty + 1`
	fi
	if [ $empty -eq 3 ]
	then
	    echo "$prog: Warning: archive \"$file\" is empty and will be skipped"
	else
	    mergelist="$mergelist $file"
	fi
    done
done

if [ -f $output.index ]
then
    echo "$prog: Error: \"index\" file already exists for output archive \"$output\""
    fail=true
fi
if [ -f $output.meta ]
then
    echo "$prog: Error: \"meta\" file already exists for output archive \"$output\""
    fail=true
fi
if [ -f $output.0 ]
then
    echo "$prog: Error: \"volume 0\" file already exists for output archive \"$output\""
    fail=true
fi

$fail && _abandon

i=0
list=""
part=0
$VERBOSE && echo "Input archives to be merged:"
for input in $mergelist
do
    for file in $input.index
    do
	if [ $i -ge 35 ]
	then
	    # this limit requires of the order of 3 x 35 input + 3 x 1
	    # output = 108 file descriptors which should be well below any
	    # shell-imposed or system-imposed limits
	    #
	    $VERBOSE && echo "		-> partial merge to $tmp.$part"
	    cmd="pmlogextract $list $tmp.$part"
	    if $SHOWME
	    then
		echo "+ $cmd"
	    else
		if $cmd
		then
		    :
		else
		    $VERBOSE || echo "		-> partial merge to $tmp.$part"
		    echo "$prog: Directory: `pwd`"
		    echo "$prog: Failed: pmlogextract $list $tmp.$part"
		    _warning
		fi
	    fi
	    list=$tmp.$part
	    part=`expr $part + 1`
	    i=0
	fi
	file=`basename $file .index`
	list="$list $file"
	$VERBOSE && $PCP_ECHO_PROG $PCP_ECHO_N "	$file""$PCP_ECHO_C"
	numvol=`echo $file.[0-9]* | wc -w | sed -e 's/  *//g'`
	if [ $numvol -gt 1 ]
	then
	    $VERBOSE && echo " ($numvol volumes)"
	else
	    $VERBOSE && echo
	fi
	i=`expr $i + 1`
    done
done

cmd="pmlogextract $list $output"
if $SHOWME
then
    echo "+ $cmd"
else
    if $cmd
    then
	:
    else
	echo "$prog: Directory: `pwd`"
	echo "$prog: Failed: pmlogextract $list $output"
	_warning
    fi
    $VERBOSE && echo "Output archive files:"
    for file in $output.meta $output.index $output.0
    do
	if [ -f $file ]
	then
	    $VERBOSE && ls -l $file
	else
	    echo "$prog: Error: file \"$file\" not created"
	    force=false
	fi
    done
fi

if $force
then
    $VERBOSE && $PCP_ECHO_PROG $PCP_ECHO_N "Removing input archive files ...""$PCP_ECHO_C"
    for input in $mergelist
    do
	for file in $input.index
	do
	    file=`basename $file .index`
	    [ "$file" = "$output" ] && continue
	    eval $RM -f $file.index $file.meta $file.[0-9]*
	done
    done
    $VERBOSE && echo " done"
fi

exit
