manctl.sh revision 50479
1#!/bin/sh 
2#
3# Copyright (c) 1994 Geoffrey M. Rehmet, Rhodes University
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. All advertising materials mentioning features or use of this software
15#    must display the following acknowledgement:
16#	This product includes software developed by Geoffrey M. Rehmet
17# 4. Neither the name of Geoffrey M. Rehmet nor that of Rhodes University
18#    may be used to endorse or promote products derived from this software
19#    without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24# IN NO EVENT SHALL GEOFFREY M. REHMET OR RHODES UNIVERSITY BE LIABLE
25# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31# SUCH DAMAGE.
32#
33# $FreeBSD: head/usr.sbin/manctl/manctl.sh 50479 1999-08-28 01:35:59Z peter $
34#
35# manctl: 
36#	a utility for manipulating manual pages
37# functions:
38#	compress uncompressed man pages (elliminating .so's)
39#		this is now two-pass.  If possible, .so's
40#		are replaced with hard links
41#	uncompress compressed man pages
42#	purge old formatted man pages (not implemented yet)
43# Things to watch out for:
44#	Hard links - careful with g(un)zipping!
45#	.so's - throw everything through soelim before gzip!
46#	symlinks - ignore these - eg: expn is its own man page:
47#			don't want to compress this!
48#
49PATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH
50
51#
52# purge cat? directories
53#
54do_purge()
55{
56	echo "purge $@" 2>&1
57	echo "not implemented yet\n" 2>&1
58}
59
60
61#
62# Uncompress one page
63#
64uncompress_page()
65{
66	local	pname
67	local	fname
68	local	sect
69	local	ext
70
71	# break up file name
72	pname=$1
73	IFS='.' ; set $pname
74	# less than 3 fields - don't know what to do with this
75	if [ $# -lt 3 ] ; then 
76		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
77	fi
78	# construct name and section
79	fname=$1 ; shift
80	while [ $# -gt 2 ] ; do
81		fname=$fname.$1
82		shift
83	done
84	sect=$1
85	ext=$2
86
87	IFS=" 	"
88	case "$ext" in
89	gz|Z) 	{ 
90		IFS=" 	" ; set `file $pname`
91		if [ $2 != "gzip" ] ; then 
92			echo moving hard link $pname 1>&2
93			mv $pname $fname.$ext	# link
94		else
95			if [ $2 != "symbolic" ] ; then
96				echo gunzipping page $pname 1>&2
97				gunzip -c $pname > /tmp/manager.$$
98				chmod u+w $pname
99				cp /tmp/manager.$$ $pname
100				chmod 444 $pname
101				mv $pname $fname.$sect
102				rm /tmp/manager.$$
103			else
104				# skip symlinks - this can be
105				# a program like expn, which is
106				# its own man page !
107				echo skipping symlink $pname 1>&2
108			fi
109		fi };;
110	*)	{
111		IFS=" 	"
112		echo skipping file $pname 1>&2
113		} ;;
114	esac
115	# reset IFS - this is important!
116	IFS=" 	"
117}
118
119
120#
121# Uncompress manpages in paths
122#
123do_uncompress()
124{
125	local	i
126	local	dir
127	local	workdir
128
129	workdir=`pwd`
130	while [ $# != 0 ] ; do
131		if [ -d $1 ] ; then
132			dir=$1
133			cd $dir
134			for i in * ; do
135				case $i in
136				*cat?)	;; # ignore cat directories
137				*)	{
138					if [ -d $i ] ; then 
139						do_uncompress $i
140					else
141						if [ -e $i ] ; then
142							uncompress_page $i
143						fi
144					fi } ;;
145				esac
146			done
147			cd $workdir
148		else
149			echo "directory $1 not found" 1>&2
150		fi
151		shift
152	done
153}
154
155#
156# Remove .so's from one file
157#
158so_purge_page()
159{
160 	local	so_entries
161	local	lines
162	local	fname
163
164	so_entries=`grep "^\.so" $1 | wc -l`
165	if [ $so_entries -eq 0 ] ; then return 0 ; fi
166
167	# we have a page with a .so in it
168	echo $1 contains a .so entry 2>&1
169	
170	# now check how many lines in the file
171	lines=`wc -l < $1`
172
173	# if the file is only one line long, we can replace it
174	# with a hard link!
175	if [ $lines -eq 1 ] ; then
176		fname=$1;
177		echo replacing $fname with a hard link
178		set `cat $fname`;
179		rm -f $fname
180		ln ../$2 $fname
181	else
182		echo inlining page $fname 1>&2
183		cat $fname | \
184		(cd .. ; soelim ) > /tmp/manager.$$
185		chmod u+w $fname
186		cp /tmp/manager.$$ $fname
187		chmod 444 $fname
188	fi
189}
190
191#
192# Remove .so entries from man pages
193#	If a page consists of just one line with a .so,
194#	replace it with a hard link
195#
196remove_so()
197{
198	local	pname
199	local	fname
200	local	sect
201
202	# break up file name
203	pname=$1
204	IFS='.' ; set $pname
205	if [ $# -lt 2 ] ; then 
206		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
207	fi
208	# construct name and section
209	fname=$1 ; shift
210	while [ $# -gt 1 ] ; do
211		fname=$fname.$1
212		shift
213	done
214	sect=$1
215
216	IFS=" 	"
217	case "$sect" in
218	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
219	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
220	[12345678ln]*){
221		IFS=" 	" ; set `file $pname`
222		if [ $2 = "gzip" ] ; then 
223			echo moving hard link $pname 1>&2
224			mv $pname $pname.gz	# link
225		else
226			if [ $2 != "symbolic" ] ; then
227				echo "removing .so's in  page $pname" 1>&2
228				so_purge_page $pname
229			else
230				# skip symlink - this can be
231				# a program like expn, which is
232				# its own man page !
233				echo skipping symlink $pname 1>&2
234			fi
235		fi };;
236	*)	{
237		IFS=" 	"
238		echo skipping file $pname 1>&2
239		} ;;
240	esac
241	# reset IFS - this is important!
242	IFS=" 	"
243}
244
245
246#
247# compress one page
248#	We need to watch out for hard links here.
249#
250compress_page()
251{
252	local	pname
253	local	fname
254	local	sect
255
256	# break up file name
257	pname=$1
258	IFS='.' ; set $pname
259	if [ $# -lt 2 ] ; then 
260		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
261	fi
262	# construct name and section
263	fname=$1 ; shift
264	while [ $# -gt 1 ] ; do
265		fname=$fname.$1
266		shift
267	done
268	sect=$1
269
270	IFS=" 	"
271	case "$sect" in
272	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
273	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
274	[12345678ln]*){
275		IFS=" 	" ; set `file $pname`
276		if [ $2 = "gzip" ] ; then 
277			echo moving hard link $pname 1>&2
278			mv $pname $pname.gz	# link
279		else
280			if [ $2 != "symbolic" ] ; then
281				echo gzipping page $pname 1>&2
282				cat $pname | \
283				(cd .. ; soelim )| gzip -c -- > /tmp/manager.$$
284				chmod u+w $pname
285				cp /tmp/manager.$$ $pname
286				chmod 444 $pname
287				mv $pname $pname.gz
288				rm /tmp/manager.$$
289			else
290				# skip symlink - this can be
291				# a program like expn, which is
292				# its own man page !
293				echo skipping symlink $pname 1>&2
294			fi
295		fi };;
296	*)	{
297		IFS=" 	"
298		echo skipping file $pname 1>&2
299		} ;;
300	esac
301	# reset IFS - this is important!
302	IFS=" 	"
303}
304
305#
306# Compress man pages in paths
307#
308do_compress_so()
309{
310	local	i
311	local	dir
312	local	workdir
313	local	what
314
315	what=$1
316	shift
317	workdir=`pwd`
318	while [ $# != 0 ] ; do
319		if [ -d $1 ] ; then
320			dir=$1
321			cd $dir
322			for i in * ; do
323				case $i in
324				*cat?)	;; # ignore cat directories
325				*)	{
326					if [ -d $i ] ; then 
327						do_compress_so $what $i
328					else 
329						if [ -e $i ] ; then
330							$what $i
331						fi
332					fi } ;;
333				esac
334			done
335			cd $workdir
336		else
337			echo "directory $1 not found" 1>&2
338		fi
339		shift
340	done
341}
342
343#
344# Display a usage message
345#
346ctl_usage()
347{
348	echo "usage: $1 -compress <path> ... " 1>&2
349	echo "       $1 -uncompress <path> ... " 1>&2
350	echo "       $1 -purge <days> <path> ... " 1>&2
351	echo "       $1 -purge expire <path> ... " 1>&2
352	exit 1
353}
354
355#
356# remove .so's and do compress
357#
358do_compress()
359{
360	# First remove all so's from the pages to be compressed
361	do_compress_so remove_so "$@"
362	# now do ahead and compress the pages
363	do_compress_so compress_page "$@"
364}
365
366#
367# dispatch options
368#
369if [ $# -lt 2 ] ; then ctl_usage $0 ; fi ;
370
371case "$1" in
372	-compress)	shift ; do_compress "$@" ;;
373	-uncompress)	shift ; do_uncompress "$@" ;;
374	-purge)		shift ; do_purge "$@" ;;
375	*)		ctl_usage $0 ;;
376esac
377