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    /^\.SH NAME/{
64#                               Read next line
65	n
66#                               Remove all commas ...
67	s/,//g
68#                               ... and backslash-escaped spaces.
69	s/\\\ //g
70#                               Delete from \- to the end of line
71	s/ \\\-.*//
72#                               Convert all non-space non-alphanum sequences
73#                               to single underscores.
74	s/[^ A-Za-z0-9][^ A-Za-z0-9]*/_/g
75#                               print the result and exit
76	p;q
77    }' $ManPage`
78
79if test -z "$Names" ; then
80    echo "warning: no target names found in $ManPage"
81fi
82
83########################################################################
84### Remaining Set Up
85###
86
87case $ManPage in
88    *.1) Section=1 ;;
89    *.3) Section=3 ;;
90    *.n) Section=n ;;
91    *)	echo "unknown section for $ManPage"
92	exit 2 ;;
93esac
94
95Name=`basename $ManPage .$Section`
96SrcDir=`dirname $ManPage`
97
98########################################################################
99### Process Page to Create Target Pages
100###
101
102Specials="DString Thread Notifier RegExp library packagens pkgMkIndex safesock FindPhoto FontId MeasureChar"
103for n in $Specials; do
104    if [ "$Name" = "$n" ] ; then
105	Names="$n $Names"
106    fi
107done
108
109First=""
110for Target in $Names; do
111    Target=$Target$Suffix.$Section
112    rm -f "$Dir/$Target" "$Dir/$Target.*"
113    if test -z "$First" ; then
114	First=$Target
115	sed -e "/man\.macros/r $SrcDir/man.macros" -e "/man\.macros/d" \
116	    $ManPage > "$Dir/$First"
117	chmod 644 "$Dir/$First"
118	$Gzip "$Dir/$First"
119    else
120	ln $SymOrLoc"$First$Gz" "$Dir/$Target$Gz"
121    fi
122done
123
124########################################################################
125exit 0
126