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#
3350479Speter# $FreeBSD$
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
9771293Sjedgar				temp=`mktemp -t manager` || exit 1
9871293Sjedgar				gunzip -c $pname > $temp
991366Scsgr				chmod u+w $pname
10071293Sjedgar				cp $temp $pname
1011366Scsgr				chmod 444 $pname
1021366Scsgr				mv $pname $fname.$sect
10371293Sjedgar				rm -f $temp
1041366Scsgr			else
1051366Scsgr				# skip symlinks - this can be
1061366Scsgr				# a program like expn, which is
1071366Scsgr				# its own man page !
1081366Scsgr				echo skipping symlink $pname 1>&2
1091366Scsgr			fi
1101366Scsgr		fi };;
1111366Scsgr	*)	{
1121366Scsgr		IFS=" 	"
1131366Scsgr		echo skipping file $pname 1>&2
1141366Scsgr		} ;;
1151366Scsgr	esac
1161366Scsgr	# reset IFS - this is important!
1171366Scsgr	IFS=" 	"
1181366Scsgr}
1191366Scsgr
1201366Scsgr
1211366Scsgr#
1221366Scsgr# Uncompress manpages in paths
1231366Scsgr#
1241366Scsgrdo_uncompress()
1251366Scsgr{
1261366Scsgr	local	i
1271366Scsgr	local	dir
1281369Scsgr	local	workdir
1291366Scsgr
1301369Scsgr	workdir=`pwd`
1311366Scsgr	while [ $# != 0 ] ; do
1321366Scsgr		if [ -d $1 ] ; then
1331366Scsgr			dir=$1
1341369Scsgr			cd $dir
1351370Scsgr			for i in * ; do
1361366Scsgr				case $i in
1371366Scsgr				*cat?)	;; # ignore cat directories
1381366Scsgr				*)	{
1391366Scsgr					if [ -d $i ] ; then 
1401366Scsgr						do_uncompress $i
1411366Scsgr					else
1421366Scsgr						if [ -e $i ] ; then
1431366Scsgr							uncompress_page $i
1441366Scsgr						fi
1451366Scsgr					fi } ;;
1461366Scsgr				esac
1471366Scsgr			done
1481369Scsgr			cd $workdir
1491366Scsgr		else
1501366Scsgr			echo "directory $1 not found" 1>&2
1511366Scsgr		fi
1521366Scsgr		shift
1531366Scsgr	done
1541366Scsgr}
1551366Scsgr
1561366Scsgr#
1571376Scsgr# Remove .so's from one file
1581376Scsgr#
1591376Scsgrso_purge_page()
1601376Scsgr{
1611376Scsgr 	local	so_entries
1621376Scsgr	local	lines
1631376Scsgr	local	fname
1641376Scsgr
1651376Scsgr	so_entries=`grep "^\.so" $1 | wc -l`
1666403Sjkh	if [ $so_entries -eq 0 ] ; then return 0 ; fi
1671376Scsgr
1681376Scsgr	# we have a page with a .so in it
1691376Scsgr	echo $1 contains a .so entry 2>&1
1701376Scsgr	
1711376Scsgr	# now check how many lines in the file
1721376Scsgr	lines=`wc -l < $1`
1731376Scsgr
1741376Scsgr	# if the file is only one line long, we can replace it
1751376Scsgr	# with a hard link!
1761376Scsgr	if [ $lines -eq 1 ] ; then
1771376Scsgr		fname=$1;
1781376Scsgr		echo replacing $fname with a hard link
1791376Scsgr		set `cat $fname`;
1801376Scsgr		rm -f $fname
1811376Scsgr		ln ../$2 $fname
1821376Scsgr	else
1831376Scsgr		echo inlining page $fname 1>&2
18471293Sjedgar		temp=`mktemp -t manager` || exit 1
1851376Scsgr		cat $fname | \
18671293Sjedgar		(cd .. ; soelim ) > $temp
1871376Scsgr		chmod u+w $fname
18871293Sjedgar		cp $temp $fname
1891376Scsgr		chmod 444 $fname
19071293Sjedgar		rm -f $temp
1911376Scsgr	fi
1921376Scsgr}
1931376Scsgr
1941376Scsgr#
1951376Scsgr# Remove .so entries from man pages
1961376Scsgr#	If a page consists of just one line with a .so,
1971376Scsgr#	replace it with a hard link
1981376Scsgr#
1991376Scsgrremove_so()
2001376Scsgr{
2011376Scsgr	local	pname
2021376Scsgr	local	fname
2031376Scsgr	local	sect
2041376Scsgr
2051376Scsgr	# break up file name
2061376Scsgr	pname=$1
2071376Scsgr	IFS='.' ; set $pname
2081376Scsgr	if [ $# -lt 2 ] ; then 
2091376Scsgr		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
2101376Scsgr	fi
2111376Scsgr	# construct name and section
2121376Scsgr	fname=$1 ; shift
2131376Scsgr	while [ $# -gt 1 ] ; do
2141376Scsgr		fname=$fname.$1
2151376Scsgr		shift
2161376Scsgr	done
2171376Scsgr	sect=$1
2181376Scsgr
2191376Scsgr	IFS=" 	"
2201376Scsgr	case "$sect" in
2211376Scsgr	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
2221376Scsgr	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
2231376Scsgr	[12345678ln]*){
2241376Scsgr		IFS=" 	" ; set `file $pname`
2251376Scsgr		if [ $2 = "gzip" ] ; then 
2261376Scsgr			echo moving hard link $pname 1>&2
2271376Scsgr			mv $pname $pname.gz	# link
2281376Scsgr		else
2291376Scsgr			if [ $2 != "symbolic" ] ; then
2301376Scsgr				echo "removing .so's in  page $pname" 1>&2
2311376Scsgr				so_purge_page $pname
2321376Scsgr			else
2331376Scsgr				# skip symlink - this can be
2341376Scsgr				# a program like expn, which is
2351376Scsgr				# its own man page !
2361376Scsgr				echo skipping symlink $pname 1>&2
2371376Scsgr			fi
2381376Scsgr		fi };;
2391376Scsgr	*)	{
2401376Scsgr		IFS=" 	"
2411376Scsgr		echo skipping file $pname 1>&2
2421376Scsgr		} ;;
2431376Scsgr	esac
2441376Scsgr	# reset IFS - this is important!
2451376Scsgr	IFS=" 	"
2461376Scsgr}
2471376Scsgr
2481376Scsgr
2491376Scsgr#
2501366Scsgr# compress one page
2511366Scsgr#	We need to watch out for hard links here.
2521366Scsgr#
2531366Scsgrcompress_page()
2541366Scsgr{
2551366Scsgr	local	pname
2561366Scsgr	local	fname
2571366Scsgr	local	sect
2581366Scsgr
2591366Scsgr	# break up file name
2601366Scsgr	pname=$1
2611366Scsgr	IFS='.' ; set $pname
2621366Scsgr	if [ $# -lt 2 ] ; then 
2631366Scsgr		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ; 
2641366Scsgr	fi
2651366Scsgr	# construct name and section
2661366Scsgr	fname=$1 ; shift
2671366Scsgr	while [ $# -gt 1 ] ; do
2681366Scsgr		fname=$fname.$1
2691366Scsgr		shift
2701366Scsgr	done
2711366Scsgr	sect=$1
2721366Scsgr
2731366Scsgr	IFS=" 	"
2741366Scsgr	case "$sect" in
2751366Scsgr	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
2761366Scsgr	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
2771366Scsgr	[12345678ln]*){
2781366Scsgr		IFS=" 	" ; set `file $pname`
2791366Scsgr		if [ $2 = "gzip" ] ; then 
2801366Scsgr			echo moving hard link $pname 1>&2
2811366Scsgr			mv $pname $pname.gz	# link
2821366Scsgr		else
2831366Scsgr			if [ $2 != "symbolic" ] ; then
2841366Scsgr				echo gzipping page $pname 1>&2
28571293Sjedgar				temp=`mktemp -t manager` || exit 1
2861370Scsgr				cat $pname | \
28771293Sjedgar				(cd .. ; soelim )| gzip -c -- > $temp
2881366Scsgr				chmod u+w $pname
28971293Sjedgar				cp $temp $pname
2901366Scsgr				chmod 444 $pname
2911366Scsgr				mv $pname $pname.gz
29271293Sjedgar				rm -f $temp
2931366Scsgr			else
2941366Scsgr				# skip symlink - this can be
2951366Scsgr				# a program like expn, which is
2961366Scsgr				# its own man page !
2971366Scsgr				echo skipping symlink $pname 1>&2
2981366Scsgr			fi
2991366Scsgr		fi };;
3001366Scsgr	*)	{
3011366Scsgr		IFS=" 	"
3021366Scsgr		echo skipping file $pname 1>&2
3031366Scsgr		} ;;
3041366Scsgr	esac
3051366Scsgr	# reset IFS - this is important!
3061366Scsgr	IFS=" 	"
3071366Scsgr}
3081366Scsgr
3091366Scsgr#
3101366Scsgr# Compress man pages in paths
3111366Scsgr#
3121376Scsgrdo_compress_so()
3131366Scsgr{
3141366Scsgr	local	i
3151366Scsgr	local	dir
3161369Scsgr	local	workdir
3171376Scsgr	local	what
3181366Scsgr
3191376Scsgr	what=$1
3201376Scsgr	shift
3211369Scsgr	workdir=`pwd`
3221366Scsgr	while [ $# != 0 ] ; do
3231366Scsgr		if [ -d $1 ] ; then
3241366Scsgr			dir=$1
3251369Scsgr			cd $dir
3261369Scsgr			for i in * ; do
3271366Scsgr				case $i in
3281366Scsgr				*cat?)	;; # ignore cat directories
3291366Scsgr				*)	{
3301366Scsgr					if [ -d $i ] ; then 
3311376Scsgr						do_compress_so $what $i
3321366Scsgr					else 
3331366Scsgr						if [ -e $i ] ; then
3341376Scsgr							$what $i
3351366Scsgr						fi
3361366Scsgr					fi } ;;
3371366Scsgr				esac
3381366Scsgr			done
3391369Scsgr			cd $workdir
3401366Scsgr		else
3411366Scsgr			echo "directory $1 not found" 1>&2
3421366Scsgr		fi
3431366Scsgr		shift
3441366Scsgr	done
3451366Scsgr}
3461366Scsgr
3471366Scsgr#
3481366Scsgr# Display a usage message
3491366Scsgr#
3501366Scsgrctl_usage()
3511366Scsgr{
35229847Scharnier	echo "usage: $1 -compress <path> ... " 1>&2
35329847Scharnier	echo "       $1 -uncompress <path> ... " 1>&2
35429847Scharnier	echo "       $1 -purge <days> <path> ... " 1>&2
35529847Scharnier	echo "       $1 -purge expire <path> ... " 1>&2
3561366Scsgr	exit 1
3571366Scsgr}
3581366Scsgr
3591376Scsgr#
3601376Scsgr# remove .so's and do compress
3611376Scsgr#
3621376Scsgrdo_compress()
3631376Scsgr{
3641376Scsgr	# First remove all so's from the pages to be compressed
3651376Scsgr	do_compress_so remove_so "$@"
3661376Scsgr	# now do ahead and compress the pages
3671376Scsgr	do_compress_so compress_page "$@"
3681376Scsgr}
3691366Scsgr
3701366Scsgr#
3711366Scsgr# dispatch options
3721366Scsgr#
37313601Swoschif [ $# -lt 2 ] ; then ctl_usage $0 ; fi ;
3741366Scsgr
3751366Scsgrcase "$1" in
3761366Scsgr	-compress)	shift ; do_compress "$@" ;;
3771366Scsgr	-uncompress)	shift ; do_uncompress "$@" ;;
3781366Scsgr	-purge)		shift ; do_purge "$@" ;;
3791366Scsgr	*)		ctl_usage $0 ;;
3801366Scsgresac
381