#!/bin/sh
#
# create a forest of archives suitable for using with pmlogger_daily
#
# only arg from the command line is the root of the forest, which must
# already exist
#
# -c config specifies an alternate recipe with one archive per line
# and the fields
# hostname source-archive-base-name destination-archive-base-name
#

_usage()
{
    echo >&2 "Usage: mk.logfarm [-c config] rootdir"
    exit 1
}

tmp=/var/tmp/mk.logfarm.$$
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

# need to be able to find pmlogrewrite
#
if [ ! -f /etc/pcp.conf ]
then
    echo "mk.logfarm: Error: cannot find /etc/pcp.conf"
    exit
fi
eval `grep PCP_BINADM_DIR= /etc/pcp.conf`
if [ -z "$PCP_BINADM_DIR" ]
then
    echo "mk.logfarm: Error: cannot find PCP_BINADM_DIR from /etc/pcp.conf"
    exit
fi
export PATH=$PCP_BINADM_DIR:$PATH
if which pmlogrewrite >/dev/null 2>&1
then
    :
else
    echo "mk.logfarm: Error: cannot find pmlogrewrite"
    exit
fi

sts=1
config=''
while getopts 'c:?' p
do
    case "$p"
    in
	c)	config="$OPTARG"
		if [ ! -f "$config" ]
		then
		    echo "mk.logfarm: Error: config ($config) does not exist"
		    exit
		fi
		;;

	?)	_usage
		# NOTREACHED
    esac
done
shift `expr $OPTIND - 1`

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

rootdir="$1"

if [ ! -d "$rootdir" ]
then
    echo "mk.logfarm: Error: rootdir ($rootdir) does not exist"
    exit
fi

rm -f $tmp.abort
if [ -z "$config" ]
then
    cat <<End-of-File
thishost	archives/foo+	20011002
thishost	archives/foo+	20011003
thishost	archives/foo+	20011004.01.02
thishost	archives/foo+	20011004.03.04
thishost	archives/foo+	20011004.05.06
thishost	archives/foo+	20011004.05.06-00
thishost	archives/foo+	20011004.07.08
thishost	archives/foo+	20011005
thishost	archives/foo+	20011006.00.10
thishost	archives/foo+	20011007
otherhost	archives/ok-foo	20011002.00.10
otherhost	archives/ok-foo	20011002.00.10-00
otherhost	archives/ok-foo	20011002.02.03
otherhost	archives/ok-foo	20011002.03.04
otherhost	archives/ok-foo	20011003.00.10
otherhost	archives/ok-foo	20011004
otherhost	archives/ok-foo	20011005.00.10
otherhost	archives/ok-foo	20011006
End-of-File
else
    cat "$config"
fi \
| sed \
    -e 's/#.*//' \
    -e '/^[ 	]*$/d' \
| while read host src dst
do
    if [ ! -d "$rootdir"/"$host" ]
    then
	if mkdir "$rootdir"/"$host"
	then
	    :
	else
	    echo "mk.logfarm: Error: mkdir failed for $rootdir/$host"
	    touch $tmp.abort
	    break
	fi
    fi

    for file in "$src".*
    do
	if [ "$file" = "$src.*" ]
	then
	    echo "mk.logfarm: Error: no files match source ($src.*)"
	    touch $tmp.abort
	    break
	fi
	cp "$file" "$rootdir"/"$host"
    done
    [ -f $tmp.abort ] && break
    srcbase="`basename $src`"

    # rename if needed ...
    #
    if [ "$srcbase" != "$dst" ]
    then
	if pmlogmv "$rootdir"/"$host"/"$srcbase" "$rootdir"/"$host"/"$dst"
	then
	    :
	else
	    echo "mk.logfarm: Error: pmlogmv $host/$srcbase -> $host/$dst failed"
	    touch $tmp.abort
	    break
	fi
    fi

    # if the destination archive name is of the YYYYMMDD... format, then
    # align timestamps, unless this is a bad archive (maybe we're testing
    # with one of the badarchives ones) ...
    #
    rm -f $tmp.dostamp
    case "$dst"
    in
	2[0-9][0-9][0-9][01][0-9][0-3][0-9]*)
		touch $tmp.dostamp
		;;
    esac
    if [ -f $tmp.dostamp ]
    then
	if pmlogcheck -w "$rootdir"/"$host"/"$dst">/dev/null 2>&1
	then
	    # now align the timestamps of the archive with the date-and-time
	    # of the destination archive name
	    #
	    delta=`src/timeshift -z -a "$rootdir"/"$host"/"$dst" "$dst"`
	    cat <<End-of-File >$tmp.config
GLOBAL {
    time -> $delta
}
End-of-File
	    if pmlogrewrite -c $tmp.config -i "$rootdir"/"$host"/"$dst"
	    then
		:
	    else
		echo "mk.logfarm: Error: pmlogrewrite $host/$dst failed"
		touch $tmp.abort
		break
	    fi
	fi

	# And finally change the access and modification times.
	#
	# Need to use the *bsd verson of touch(1) syntax for -t, namely
	# "YYYYMMDDhhmm.SS" which also works for the Linux variants
	# of touch(1).
	#
	stamp="`echo "$dst" | sed \
				-e 's/-[0-9][0-9]*$//' \
				-e 's/^\(....\)\(..\)\(..\)\.\(..\)\.\(..\)$/\1\2\3\4\5.00/' \
				-e 's/^\(....\)\(..\)\(..\)$/\1\2\30000.00/' \
	      `"
	#debug# echo "dst=$dst stamp=$stamp"
	for file in "$rootdir"/"$host"/"$dst".*
	do
	    touch -t "$stamp" "$file"
	done
    else
	# skip timestamp for $dst"
	#
	:
    fi

done

[ ! -f $tmp.abort ] && sts=0

exit $sts
