1#!/bin/sh
2
3########################################################################
4### Parse Options
5###
6
7Gzip=:
8SymOrLoc=""
9Gz=""
10Suffix=""
11
12while true; do
13    case $1 in
14        -s | --symlinks  ) SymOrLoc="-s "      ;;
15        -z | --compress  )     Gzip=$2;  shift ;;
16	-e | --extension )       Gz=$2;  shift ;;
17	-x | --suffix    )   Suffix=$2;  shift ;;
18	-*) cat <<EOF
19Unknown option "$1". Supported options:
20    -s         Use symbolic links for manpages with multiple names.
21    -z PROG    Use PROG to compress manual pages.
22    -e EXT     Defines the extension added by -z PROG when compressing.
23    -x SUFF    Defines an extra extension suffix to use.
24Option names may not be combined getopt-style.
25EOF
26	    exit 1 ;;
27	*)  break ;;
28    esac
29    shift
30done
31if test "$#" != 2; then
32    echo "Usage: installManPages <options> file dir"
33    exit 1
34fi
35
36########################################################################
37### Parse Required Arguments
38###
39
40ManPage=$1
41Dir=$2
42if test -f $ManPage ; then : ; else
43    echo "source manual page file must exist"
44    exit 1
45fi
46if test -d $Dir ; then : ; else
47    echo "target directory must exist"
48    exit 1
49fi
50test -z "$SymOrLoc" && SymOrLoc="$Dir/"
51
52########################################################################
53### Extract Target Names from Manual Page
54###
55
56# A sed script to parse the alternative names out of a man page.
57#
58# Backslashes are trippled in the sed script, because it is in
59# backticks which doesn't pass backslashes literally.
60#
61Names=`sed -n '
62#                               Look for a line, that starts with .SH NAME
63#                               optionally allow NAME to be surrounded
64#                               by quotes.
65    /^\.SH NAME/{
66#                               Read next line
67	n
68#                               Remove all commas ...
69	s/,//g
70#                               ... and backslash-escaped spaces.
71	s/\\\ //g
72#                               Delete from \- to the end of line
73	s/ \\\-.*//
74#                               print the result and exit
75	p;q
76    }' $ManPage`
77
78if test -z "$Names" ; then
79    echo "warning: no target names found in $ManPage"
80fi
81
82########################################################################
83### Remaining Set Up
84###
85
86case $ManPage in
87    *.1) Section=1 ;;
88    *.3) Section=3 ;;
89    *.n) Section=n ;;
90    *)	echo "unknown section for $ManPage"
91	exit 2 ;;
92esac
93
94SrcDir=`dirname $ManPage`
95
96########################################################################
97### Process Page to Create Target Pages
98###
99
100First=""
101for Target in $Names; do
102    Target=$Target.$Section$Suffix
103    rm -f $Dir/$Target $Dir/$Target.*
104    if test -z "$First" ; then
105	First=$Target
106	sed -e "/man\.macros/r $SrcDir/man.macros" -e "/man\.macros/d" \
107	    $ManPage > $Dir/$First
108	chmod 444 $Dir/$First
109	$Gzip $Dir/$First
110    else
111	ln $SymOrLoc$First$Gz $Dir/$Target$Gz
112    fi
113done
114
115########################################################################
116exit 0
117