xref: /dragonfly/usr.sbin/manctl/manctl.sh (revision 73610d44)
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: src/usr.sbin/manctl/manctl.sh,v 1.11.2.1 2001/02/05 13:53:03 jedgar Exp $
34# $DragonFly: src/usr.sbin/manctl/manctl.sh,v 1.2 2003/06/17 04:29:57 dillon Exp $
35#
36# manctl:
37#	a utility for manipulating manual pages
38# functions:
39#	compress uncompressed man pages (elliminating .so's)
40#		this is now two-pass.  If possible, .so's
41#		are replaced with hard links
42#	uncompress compressed man pages
43#	purge old formatted man pages (not implemented yet)
44# Things to watch out for:
45#	Hard links - careful with g(un)zipping!
46#	.so's - throw everything through soelim before gzip!
47#	symlinks - ignore these - eg: expn is its own man page:
48#			don't want to compress this!
49#
50PATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH
51
52#
53# purge cat? directories
54#
55do_purge()
56{
57	echo "purge $@" 2>&1
58	echo "not implemented yet\n" 2>&1
59}
60
61
62#
63# Uncompress one page
64#
65uncompress_page()
66{
67	local	pname
68	local	fname
69	local	sect
70	local	ext
71
72	# break up file name
73	pname=$1
74	IFS='.' ; set $pname
75	# less than 3 fields - don't know what to do with this
76	if [ $# -lt 3 ] ; then
77		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ;
78	fi
79	# construct name and section
80	fname=$1 ; shift
81	while [ $# -gt 2 ] ; do
82		fname=$fname.$1
83		shift
84	done
85	sect=$1
86	ext=$2
87
88	IFS=" 	"
89	case "$ext" in
90	gz|Z) 	{
91		IFS=" 	" ; set `file $pname`
92		if [ $2 != "gzip" ] ; then
93			echo moving hard link $pname 1>&2
94			mv $pname $fname.$ext	# link
95		else
96			if [ $2 != "symbolic" ] ; then
97				echo gunzipping page $pname 1>&2
98				temp=`mktemp -t manager` || exit 1
99				gunzip -c $pname > $temp
100				chmod u+w $pname
101				cp $temp $pname
102				chmod 444 $pname
103				mv $pname $fname.$sect
104				rm -f $temp
105			else
106				# skip symlinks - this can be
107				# a program like expn, which is
108				# its own man page !
109				echo skipping symlink $pname 1>&2
110			fi
111		fi };;
112	*)	{
113		IFS=" 	"
114		echo skipping file $pname 1>&2
115		} ;;
116	esac
117	# reset IFS - this is important!
118	IFS=" 	"
119}
120
121
122#
123# Uncompress manpages in paths
124#
125do_uncompress()
126{
127	local	i
128	local	dir
129	local	workdir
130
131	workdir=`pwd`
132	while [ $# != 0 ] ; do
133		if [ -d $1 ] ; then
134			dir=$1
135			cd $dir
136			for i in * ; do
137				case $i in
138				*cat?)	;; # ignore cat directories
139				*)	{
140					if [ -d $i ] ; then
141						do_uncompress $i
142					else
143						if [ -e $i ] ; then
144							uncompress_page $i
145						fi
146					fi } ;;
147				esac
148			done
149			cd $workdir
150		else
151			echo "directory $1 not found" 1>&2
152		fi
153		shift
154	done
155}
156
157#
158# Remove .so's from one file
159#
160so_purge_page()
161{
162 	local	so_entries
163	local	lines
164	local	fname
165
166	so_entries=`grep "^\.so" $1 | wc -l`
167	if [ $so_entries -eq 0 ] ; then return 0 ; fi
168
169	# we have a page with a .so in it
170	echo $1 contains a .so entry 2>&1
171
172	# now check how many lines in the file
173	lines=`wc -l < $1`
174
175	# if the file is only one line long, we can replace it
176	# with a hard link!
177	if [ $lines -eq 1 ] ; then
178		fname=$1;
179		echo replacing $fname with a hard link
180		set `cat $fname`;
181		rm -f $fname
182		ln ../$2 $fname
183	else
184		echo inlining page $fname 1>&2
185		temp=`mktemp -t manager` || exit 1
186		cat $fname | \
187		(cd .. ; soelim ) > $temp
188		chmod u+w $fname
189		cp $temp $fname
190		chmod 444 $fname
191		rm -f $temp
192	fi
193}
194
195#
196# Remove .so entries from man pages
197#	If a page consists of just one line with a .so,
198#	replace it with a hard link
199#
200remove_so()
201{
202	local	pname
203	local	fname
204	local	sect
205
206	# break up file name
207	pname=$1
208	IFS='.' ; set $pname
209	if [ $# -lt 2 ] ; then
210		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ;
211	fi
212	# construct name and section
213	fname=$1 ; shift
214	while [ $# -gt 1 ] ; do
215		fname=$fname.$1
216		shift
217	done
218	sect=$1
219
220	IFS=" 	"
221	case "$sect" in
222	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
223	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
224	[12345678ln]*){
225		IFS=" 	" ; set `file $pname`
226		if [ $2 = "gzip" ] ; then
227			echo moving hard link $pname 1>&2
228			mv $pname $pname.gz	# link
229		else
230			if [ $2 != "symbolic" ] ; then
231				echo "removing .so's in  page $pname" 1>&2
232				so_purge_page $pname
233			else
234				# skip symlink - this can be
235				# a program like expn, which is
236				# its own man page !
237				echo skipping symlink $pname 1>&2
238			fi
239		fi };;
240	*)	{
241		IFS=" 	"
242		echo skipping file $pname 1>&2
243		} ;;
244	esac
245	# reset IFS - this is important!
246	IFS=" 	"
247}
248
249
250#
251# compress one page
252#	We need to watch out for hard links here.
253#
254compress_page()
255{
256	local	pname
257	local	fname
258	local	sect
259
260	# break up file name
261	pname=$1
262	IFS='.' ; set $pname
263	if [ $# -lt 2 ] ; then
264		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ;
265	fi
266	# construct name and section
267	fname=$1 ; shift
268	while [ $# -gt 1 ] ; do
269		fname=$fname.$1
270		shift
271	done
272	sect=$1
273
274	IFS=" 	"
275	case "$sect" in
276	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
277	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
278	[12345678ln]*){
279		IFS=" 	" ; set `file $pname`
280		if [ $2 = "gzip" ] ; then
281			echo moving hard link $pname 1>&2
282			mv $pname $pname.gz	# link
283		else
284			if [ $2 != "symbolic" ] ; then
285				echo gzipping page $pname 1>&2
286				temp=`mktemp -t manager` || exit 1
287				cat $pname | \
288				(cd .. ; soelim )| gzip -c -- > $temp
289				chmod u+w $pname
290				cp $temp $pname
291				chmod 444 $pname
292				mv $pname $pname.gz
293				rm -f $temp
294			else
295				# skip symlink - this can be
296				# a program like expn, which is
297				# its own man page !
298				echo skipping symlink $pname 1>&2
299			fi
300		fi };;
301	*)	{
302		IFS=" 	"
303		echo skipping file $pname 1>&2
304		} ;;
305	esac
306	# reset IFS - this is important!
307	IFS=" 	"
308}
309
310#
311# Compress man pages in paths
312#
313do_compress_so()
314{
315	local	i
316	local	dir
317	local	workdir
318	local	what
319
320	what=$1
321	shift
322	workdir=`pwd`
323	while [ $# != 0 ] ; do
324		if [ -d $1 ] ; then
325			dir=$1
326			cd $dir
327			for i in * ; do
328				case $i in
329				*cat?)	;; # ignore cat directories
330				*)	{
331					if [ -d $i ] ; then
332						do_compress_so $what $i
333					else
334						if [ -e $i ] ; then
335							$what $i
336						fi
337					fi } ;;
338				esac
339			done
340			cd $workdir
341		else
342			echo "directory $1 not found" 1>&2
343		fi
344		shift
345	done
346}
347
348#
349# Display a usage message
350#
351ctl_usage()
352{
353	echo "usage: $1 -compress <path> ... " 1>&2
354	echo "       $1 -uncompress <path> ... " 1>&2
355	echo "       $1 -purge <days> <path> ... " 1>&2
356	echo "       $1 -purge expire <path> ... " 1>&2
357	exit 1
358}
359
360#
361# remove .so's and do compress
362#
363do_compress()
364{
365	# First remove all so's from the pages to be compressed
366	do_compress_so remove_so "$@"
367	# now do ahead and compress the pages
368	do_compress_so compress_page "$@"
369}
370
371#
372# dispatch options
373#
374if [ $# -lt 2 ] ; then ctl_usage $0 ; fi ;
375
376case "$1" in
377	-compress)	shift ; do_compress "$@" ;;
378	-uncompress)	shift ; do_uncompress "$@" ;;
379	-purge)		shift ; do_purge "$@" ;;
380	*)		ctl_usage $0 ;;
381esac
382