manctl.sh revision 29847
11366Scsgr#!/bin/sh 
21366Scsgr#
31366Scsgr# Copyright (c) 1994 Geoffrey M. Rehmet, Rhodes University
41366Scsgr# All rights reserved.
51366Scsgr#
61366Scsgr# Redistribution and use in source and binary forms, with or without
71366Scsgr# modification, are permitted provided that the following conditions
81366Scsgr# are met:
91366Scsgr# 1. Redistributions of source code must retain the above copyright
101366Scsgr#    notice, this list of conditions and the following disclaimer.
111366Scsgr# 2. Redistributions in binary form must reproduce the above copyright
121366Scsgr#    notice, this list of conditions and the following disclaimer in the
131366Scsgr#    documentation and/or other materials provided with the distribution.
141366Scsgr# 3. All advertising materials mentioning features or use of this software
151366Scsgr#    must display the following acknowledgement:
161366Scsgr#	This product includes software developed by Geoffrey M. Rehmet
171366Scsgr# 4. Neither the name of Geoffrey M. Rehmet nor that of Rhodes University
181366Scsgr#    may be used to endorse or promote products derived from this software
191366Scsgr#    without specific prior written permission.
201366Scsgr#
211366Scsgr# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
221366Scsgr# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
231366Scsgr# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
241366Scsgr# IN NO EVENT SHALL GEOFFREY M. REHMET OR RHODES UNIVERSITY BE LIABLE
251366Scsgr# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261366Scsgr# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271366Scsgr# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281366Scsgr# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291366Scsgr# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301366Scsgr# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311366Scsgr# SUCH DAMAGE.
321366Scsgr#
3329847Scharnier# $Id: manctl.sh,v 1.9 1997/02/22 16:06:29 peter Exp $
341366Scsgr#
351366Scsgr# manctl: 
361366Scsgr#	a utility for manipulating manual pages
371366Scsgr# functions:
381366Scsgr#	compress uncompressed man pages (elliminating .so's)
391376Scsgr#		this is now two-pass.  If possible, .so's
401376Scsgr#		are replaced with hard links
411366Scsgr#	uncompress compressed man pages
421366Scsgr#	purge old formatted man pages (not implemented yet)
431366Scsgr# Things to watch out for:
441366Scsgr#	Hard links - careful with g(un)zipping!
451366Scsgr#	.so's - throw everything through soelim before gzip!
461366Scsgr#	symlinks - ignore these - eg: expn is its own man page:
471366Scsgr#			don't want to compress this!
481366Scsgr#
4919411SwoschPATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH
501366Scsgr
511366Scsgr#
521366Scsgr# purge cat? directories
531366Scsgr#
541366Scsgrdo_purge()
551366Scsgr{
561366Scsgr	echo "purge $@" 2>&1
571366Scsgr	echo "not implemented yet\n" 2>&1
581366Scsgr}
591366Scsgr
601366Scsgr
611366Scsgr#
621366Scsgr# Uncompress one page
631366Scsgr#
641366Scsgruncompress_page()
651366Scsgr{
661366Scsgr	local	pname
671366Scsgr	local	fname
681366Scsgr	local	sect
691366Scsgr	local	ext
701366Scsgr
711366Scsgr	# break up file name
721366Scsgr	pname=$1
731366Scsgr	IFS='.' ; set $pname
741366Scsgr	# less than 3 fields - don't know what to do with this
751366Scsgr	if [ $# -lt 3 ] ; then 
761366Scsgr		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
771366Scsgr	fi
781366Scsgr	# construct name and section
791366Scsgr	fname=$1 ; shift
801366Scsgr	while [ $# -gt 2 ] ; do
811366Scsgr		fname=$fname.$1
821366Scsgr		shift
831366Scsgr	done
841366Scsgr	sect=$1
851366Scsgr	ext=$2
861366Scsgr
871366Scsgr	IFS=" 	"
881366Scsgr	case "$ext" in
891366Scsgr	gz|Z) 	{ 
901366Scsgr		IFS=" 	" ; set `file $pname`
911366Scsgr		if [ $2 != "gzip" ] ; then 
921366Scsgr			echo moving hard link $pname 1>&2
931366Scsgr			mv $pname $fname.$ext	# link
941366Scsgr		else
951366Scsgr			if [ $2 != "symbolic" ] ; then
961366Scsgr				echo gunzipping page $pname 1>&2
971366Scsgr				gunzip -c $pname > /tmp/manager.$$
981366Scsgr				chmod u+w $pname
991366Scsgr				cp /tmp/manager.$$ $pname
1001366Scsgr				chmod 444 $pname
1011366Scsgr				mv $pname $fname.$sect
1021366Scsgr				rm /tmp/manager.$$
1031366Scsgr			else
1041366Scsgr				# skip symlinks - this can be
1051366Scsgr				# a program like expn, which is
1061366Scsgr				# its own man page !
1071366Scsgr				echo skipping symlink $pname 1>&2
1081366Scsgr			fi
1091366Scsgr		fi };;
1101366Scsgr	*)	{
1111366Scsgr		IFS=" 	"
1121366Scsgr		echo skipping file $pname 1>&2
1131366Scsgr		} ;;
1141366Scsgr	esac
1151366Scsgr	# reset IFS - this is important!
1161366Scsgr	IFS=" 	"
1171366Scsgr}
1181366Scsgr
1191366Scsgr
1201366Scsgr#
1211366Scsgr# Uncompress manpages in paths
1221366Scsgr#
1231366Scsgrdo_uncompress()
1241366Scsgr{
1251366Scsgr	local	i
1261366Scsgr	local	dir
1271369Scsgr	local	workdir
1281366Scsgr
1291369Scsgr	workdir=`pwd`
1301366Scsgr	while [ $# != 0 ] ; do
1311366Scsgr		if [ -d $1 ] ; then
1321366Scsgr			dir=$1
1331369Scsgr			cd $dir
1341370Scsgr			for i in * ; do
1351366Scsgr				case $i in
1361366Scsgr				*cat?)	;; # ignore cat directories
1371366Scsgr				*)	{
1381366Scsgr					if [ -d $i ] ; then 
1391366Scsgr						do_uncompress $i
1401366Scsgr					else
1411366Scsgr						if [ -e $i ] ; then
1421366Scsgr							uncompress_page $i
1431366Scsgr						fi
1441366Scsgr					fi } ;;
1451366Scsgr				esac
1461366Scsgr			done
1471369Scsgr			cd $workdir
1481366Scsgr		else
1491366Scsgr			echo "directory $1 not found" 1>&2
1501366Scsgr		fi
1511366Scsgr		shift
1521366Scsgr	done
1531366Scsgr}
1541366Scsgr
1551366Scsgr#
1561376Scsgr# Remove .so's from one file
1571376Scsgr#
1581376Scsgrso_purge_page()
1591376Scsgr{
1601376Scsgr 	local	so_entries
1611376Scsgr	local	lines
1621376Scsgr	local	fname
1631376Scsgr
1641376Scsgr	so_entries=`grep "^\.so" $1 | wc -l`
1656403Sjkh	if [ $so_entries -eq 0 ] ; then return 0 ; fi
1661376Scsgr
1671376Scsgr	# we have a page with a .so in it
1681376Scsgr	echo $1 contains a .so entry 2>&1
1691376Scsgr	
1701376Scsgr	# now check how many lines in the file
1711376Scsgr	lines=`wc -l < $1`
1721376Scsgr
1731376Scsgr	# if the file is only one line long, we can replace it
1741376Scsgr	# with a hard link!
1751376Scsgr	if [ $lines -eq 1 ] ; then
1761376Scsgr		fname=$1;
1771376Scsgr		echo replacing $fname with a hard link
1781376Scsgr		set `cat $fname`;
1791376Scsgr		rm -f $fname
1801376Scsgr		ln ../$2 $fname
1811376Scsgr	else
1821376Scsgr		echo inlining page $fname 1>&2
1831376Scsgr		cat $fname | \
1841376Scsgr		(cd .. ; soelim ) > /tmp/manager.$$
1851376Scsgr		chmod u+w $fname
1861376Scsgr		cp /tmp/manager.$$ $fname
1871376Scsgr		chmod 444 $fname
1881376Scsgr	fi
1891376Scsgr}
1901376Scsgr
1911376Scsgr#
1921376Scsgr# Remove .so entries from man pages
1931376Scsgr#	If a page consists of just one line with a .so,
1941376Scsgr#	replace it with a hard link
1951376Scsgr#
1961376Scsgrremove_so()
1971376Scsgr{
1981376Scsgr	local	pname
1991376Scsgr	local	fname
2001376Scsgr	local	sect
2011376Scsgr
2021376Scsgr	# break up file name
2031376Scsgr	pname=$1
2041376Scsgr	IFS='.' ; set $pname
2051376Scsgr	if [ $# -lt 2 ] ; then 
2061376Scsgr		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
2071376Scsgr	fi
2081376Scsgr	# construct name and section
2091376Scsgr	fname=$1 ; shift
2101376Scsgr	while [ $# -gt 1 ] ; do
2111376Scsgr		fname=$fname.$1
2121376Scsgr		shift
2131376Scsgr	done
2141376Scsgr	sect=$1
2151376Scsgr
2161376Scsgr	IFS=" 	"
2171376Scsgr	case "$sect" in
2181376Scsgr	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
2191376Scsgr	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
2201376Scsgr	[12345678ln]*){
2211376Scsgr		IFS=" 	" ; set `file $pname`
2221376Scsgr		if [ $2 = "gzip" ] ; then 
2231376Scsgr			echo moving hard link $pname 1>&2
2241376Scsgr			mv $pname $pname.gz	# link
2251376Scsgr		else
2261376Scsgr			if [ $2 != "symbolic" ] ; then
2271376Scsgr				echo "removing .so's in  page $pname" 1>&2
2281376Scsgr				so_purge_page $pname
2291376Scsgr			else
2301376Scsgr				# skip symlink - this can be
2311376Scsgr				# a program like expn, which is
2321376Scsgr				# its own man page !
2331376Scsgr				echo skipping symlink $pname 1>&2
2341376Scsgr			fi
2351376Scsgr		fi };;
2361376Scsgr	*)	{
2371376Scsgr		IFS=" 	"
2381376Scsgr		echo skipping file $pname 1>&2
2391376Scsgr		} ;;
2401376Scsgr	esac
2411376Scsgr	# reset IFS - this is important!
2421376Scsgr	IFS=" 	"
2431376Scsgr}
2441376Scsgr
2451376Scsgr
2461376Scsgr#
2471366Scsgr# compress one page
2481366Scsgr#	We need to watch out for hard links here.
2491366Scsgr#
2501366Scsgrcompress_page()
2511366Scsgr{
2521366Scsgr	local	pname
2531366Scsgr	local	fname
2541366Scsgr	local	sect
2551366Scsgr
2561366Scsgr	# break up file name
2571366Scsgr	pname=$1
2581366Scsgr	IFS='.' ; set $pname
2591366Scsgr	if [ $# -lt 2 ] ; then 
2601366Scsgr		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
2611366Scsgr	fi
2621366Scsgr	# construct name and section
2631366Scsgr	fname=$1 ; shift
2641366Scsgr	while [ $# -gt 1 ] ; do
2651366Scsgr		fname=$fname.$1
2661366Scsgr		shift
2671366Scsgr	done
2681366Scsgr	sect=$1
2691366Scsgr
2701366Scsgr	IFS=" 	"
2711366Scsgr	case "$sect" in
2721366Scsgr	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
2731366Scsgr	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
2741366Scsgr	[12345678ln]*){
2751366Scsgr		IFS=" 	" ; set `file $pname`
2761366Scsgr		if [ $2 = "gzip" ] ; then 
2771366Scsgr			echo moving hard link $pname 1>&2
2781366Scsgr			mv $pname $pname.gz	# link
2791366Scsgr		else
2801366Scsgr			if [ $2 != "symbolic" ] ; then
2811366Scsgr				echo gzipping page $pname 1>&2
2821370Scsgr				cat $pname | \
2831370Scsgr				(cd .. ; soelim )| gzip -c -- > /tmp/manager.$$
2841366Scsgr				chmod u+w $pname
2851366Scsgr				cp /tmp/manager.$$ $pname
2861366Scsgr				chmod 444 $pname
2871366Scsgr				mv $pname $pname.gz
2881366Scsgr				rm /tmp/manager.$$
2891366Scsgr			else
2901366Scsgr				# skip symlink - this can be
2911366Scsgr				# a program like expn, which is
2921366Scsgr				# its own man page !
2931366Scsgr				echo skipping symlink $pname 1>&2
2941366Scsgr			fi
2951366Scsgr		fi };;
2961366Scsgr	*)	{
2971366Scsgr		IFS=" 	"
2981366Scsgr		echo skipping file $pname 1>&2
2991366Scsgr		} ;;
3001366Scsgr	esac
3011366Scsgr	# reset IFS - this is important!
3021366Scsgr	IFS=" 	"
3031366Scsgr}
3041366Scsgr
3051366Scsgr#
3061366Scsgr# Compress man pages in paths
3071366Scsgr#
3081376Scsgrdo_compress_so()
3091366Scsgr{
3101366Scsgr	local	i
3111366Scsgr	local	dir
3121369Scsgr	local	workdir
3131376Scsgr	local	what
3141366Scsgr
3151376Scsgr	what=$1
3161376Scsgr	shift
3171369Scsgr	workdir=`pwd`
3181366Scsgr	while [ $# != 0 ] ; do
3191366Scsgr		if [ -d $1 ] ; then
3201366Scsgr			dir=$1
3211369Scsgr			cd $dir
3221369Scsgr			for i in * ; do
3231366Scsgr				case $i in
3241366Scsgr				*cat?)	;; # ignore cat directories
3251366Scsgr				*)	{
3261366Scsgr					if [ -d $i ] ; then 
3271376Scsgr						do_compress_so $what $i
3281366Scsgr					else 
3291366Scsgr						if [ -e $i ] ; then
3301376Scsgr							$what $i
3311366Scsgr						fi
3321366Scsgr					fi } ;;
3331366Scsgr				esac
3341366Scsgr			done
3351369Scsgr			cd $workdir
3361366Scsgr		else
3371366Scsgr			echo "directory $1 not found" 1>&2
3381366Scsgr		fi
3391366Scsgr		shift
3401366Scsgr	done
3411366Scsgr}
3421366Scsgr
3431366Scsgr#
3441366Scsgr# Display a usage message
3451366Scsgr#
3461366Scsgrctl_usage()
3471366Scsgr{
34829847Scharnier	echo "usage: $1 -compress <path> ... " 1>&2
34929847Scharnier	echo "       $1 -uncompress <path> ... " 1>&2
35029847Scharnier	echo "       $1 -purge <days> <path> ... " 1>&2
35129847Scharnier	echo "       $1 -purge expire <path> ... " 1>&2
3521366Scsgr	exit 1
3531366Scsgr}
3541366Scsgr
3551376Scsgr#
3561376Scsgr# remove .so's and do compress
3571376Scsgr#
3581376Scsgrdo_compress()
3591376Scsgr{
3601376Scsgr	# First remove all so's from the pages to be compressed
3611376Scsgr	do_compress_so remove_so "$@"
3621376Scsgr	# now do ahead and compress the pages
3631376Scsgr	do_compress_so compress_page "$@"
3641376Scsgr}
3651366Scsgr
3661366Scsgr#
3671366Scsgr# dispatch options
3681366Scsgr#
36913601Swoschif [ $# -lt 2 ] ; then ctl_usage $0 ; fi ;
3701366Scsgr
3711366Scsgrcase "$1" in
3721366Scsgr	-compress)	shift ; do_compress "$@" ;;
3731366Scsgr	-uncompress)	shift ; do_uncompress "$@" ;;
3741366Scsgr	-purge)		shift ; do_purge "$@" ;;
3751366Scsgr	*)		ctl_usage $0 ;;
3761366Scsgresac
377