#!/bin/sh
#
# Build new man page for libpcp|libpcp_archive routine $1
#

if [ $# != 1 ]
then
    echo "Usage: new routinename"
    exit 1
fi

if [ ! -f /etc/pcp.env ]
then
    echo "No /etc/pcp.env ... you lose"
    exit 1
fi

. /etc/pcp.env

tmp=/var/tmp/new.$$
trap "rm -f $tmp.*; exit 1" 0 1 2 3 15

lib=''
for try in libpcp libpcp_archive
do
    if grep -C 1 -r "^$1(" ../../src/$try/src 2>/dev/null >$tmp.proto
    then
	lib=$try
	break
    fi
done

if [ -z "$lib" ]
then
    echo "Cannot find prototype declaration for $1()"
    exit 1
fi

fnbase=`echo "$1" | tr '[A-Z]' '[a-z]'`
TH=`echo "$1" | tr '[a-z]' '[A-Z]' | sed -e 's/_//g'`

#echo fnbase=$fnbase TH=$TH

case $fnbase
in
    pma*)	archive=true
    		;;
    *)		archive=false
    		;;
esac

#
# Debian packaging
#
if $archive
then
    deb=../../debian/libpcp-archive1-dev.install
else
    deb=../../debian/libpcp3-dev.install
fi

if [ ! -f $deb ]
then
    echo "Error: $deb: not found"
    exit 1
fi

if grep "/$1.3.gz" $deb >/dev/null 2>&1
then
    echo "Warning: $1.3.gz already in $deb"
else
    ( echo "usr/share/man/man3/$1.3.gz" ; cat $deb ) \
    | LC_COLLATE=POSIX sort >$tmp.install
    cp $tmp.install $deb
    echo "$deb updated."
fi

if [ -f $fnbase.3 ]
then
    echo "Error: $fnbase.3 already exists!"
    exit 1
fi

#
# Skeletal man page
#
sed <$tmp.proto \
    -e '1s/.*-\([_a-zA-Z][_a-zA-Z0-9]*\)$/\1/' \
    -e '2s/^[^:]*://' \
    -e 3d \
| (tr '\012' ' '; echo ) \
| sed >$tmp.synopsis \
    -e 's/\([_a-zA-Z][_a-zA-Z0-9]*\)\([,)]\)/\\fI\1\\fP\2/g' \
    -e 's/[ 	]*$//' \
# end

cat <<End-of-File >$fnbase.3
'\\"macro stdmacro
.\\"
.\\" Copyright (c) `date "+%Y"` TODO.  All Rights Reserved.
.\\"
.\\" This program is free software; you can redistribute it and/or modify it
.\\" under the terms of the GNU General Public License as published by the
.\\" Free Software Foundation; either version 2 of the License, or (at your
.\\" option) any later version.
.\\"
.\\" This program is distributed in the hope that it will be useful, but
.\\" WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
.\\" or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
.\\" for more details.
.\\"
.\\"
.TH $TH 3 "PCP" "Performance Co-Pilot"
.SH NAME
\\f3$1\\f1,
\\f3__pmSomeRelatedRoutine\f1 \- TODO
.SH "C SYNOPSIS"
.ft 3
#include <pcp/pmapi.h>
.br
#include <pcp/libpcp.h>
End-of-File

$archive && cat <<End-of-File >>$fnbase.3
.br
#include <pcp/archive.h>
End-of-File

cat <<End-of-File >>$fnbase.3
.sp
`cat $tmp.synopsis`
End-of-File

if $archive
then
    cat <<End-of-File >>$fnbase.3
.sp
cc ... \\-lpcp_archive \\-lpcp
End-of-File
else
    cat <<End-of-File >>$fnbase.3
.sp
cc ... \\-lpcp
End-of-File
fi

cat <<End-of-File >>$fnbase.3
.ft 1
.SH CAVEAT
This documentation is intended for internal Performance Co-Pilot
(PCP) developer use.
.PP
These interfaces are not part of the PCP APIs that are guaranteed to
remain fixed across releases, and at some point in the future
they may not work or may provide different semantics.
.SH DESCRIPTION
.de CR
.ie t \\f(CR\\\\\$1\\fR\\\\\$2
.el \\fI\\\\\$1\\fR\\\\\$2
..
.B $1
TODO
.SH DIAGNOSTICS AND RETURN VALUES
The return value is 0 if all is well, else
some error code less than zero that can be turned into an error
message by calling
.BR pmErrStr (3).
.SH SEE ALSO
.BR TODO (3)
and
.BR PMAPI (3).
End-of-File

git add $fnbase.3

echo "$fnbase.3 created and added to git."
