#!/bin/sh
#
# Install man pages, but fixing up paths as we go.  All of the man pages
# are written to use the Transarc paths, and this script fixes those paths to
# be correct for the chosen configure options as the man pages are installed.

set -e

manpage="$1"
dest="$2"

afsbackupdir=/var/lib/openafs/backup
afsbosconfigdir=/etc/openafs
afsconfdir=/etc/openafs/server
afsdbdir=/var/lib/openafs/db
afslocaldir=/etc/openafs/server-local
afslogsdir=/var/log/openafs
afssrvlibexecdir=/usr/lib/openafs
viceetcdir=/etc/openafs

# Build a particular man page.  Takes the section title, the section number,
# the filename of the POD page, and the output file.
buildpage () {
    pod2man -c "$1" -r OpenAFS -s "$2" "$3" | \
        sed -e "s%/usr/afs/local/BosConfig%${afsbosconfigdir}/BosConfig%g" \
            -e "s%/usr/afs/etc%${afsconfdir}%g" \
            -e "s%/usr/afs/backup%${afsbackupdir}%g" \
            -e "s%/usr/afs/bin%${afssrvlibexecdir}%g" \
            -e "s%/usr/afs/db%${afsdbdir}%g" \
            -e "s%/usr/afs/local%${afslocaldir}%g" \
            -e "s%/usr/afs/logs%${afslogsdir}%g" \
            -e "s%/usr/vice/etc%${viceetcdir}%g" > "$4"
}

# Create the output directories.
mkdir -p man1 man5 man8

# Do the work with lots of calls to buildpage.
cd pod1
for f in *.pod ; do
    buildpage 'AFS Command Reference' 1 "$f" \
        ../man1/`echo "$f" | sed 's/\.pod$//'`.1
done
cd ..
cd pod5
for f in *.pod ; do
    buildpage 'AFS File Reference' 5 "$f" \
        ../man5/`echo "$f" | sed 's/\.pod$//'`.5
done
cd ..
cd pod8
for f in *.pod ; do
    buildpage 'AFS Command Reference' 8 "$f" \
        ../man8/`echo "$f" | sed 's/\.pod$//'`.8
done
cd ..
