compressdoc patch

git-svn-id: svn://svn.linuxfromscratch.org/BLFS/trunk/BOOK@1134 af4574ff-66df-0310-9fd7-8a98e5e911e0
This commit is contained in:
Larry Lawrence 2003-09-20 20:03:47 +00:00
parent 61cc8962a3
commit b94cd5161b

View File

@ -10,45 +10,45 @@ ideas like recursively calling gzip on them. A better way to go is to use the
script below. script below.
</para> </para>
<screen><userinput><command>cat &gt; /usr/bin/comprdoc &lt;&lt; "EOF"</command> <screen><userinput><command>cat &gt; /usr/bin/compressdoc &lt;&lt; "EOF"</command>
#!/bin/sh #!/bin/sh
function changefileext function changefileext
{ {
# prints the given filename with the new extension instead of # prints the given filename with the new extension instead of
# the old one. ! - always prints an absolute filename even if # the old one. ! - always prints an absolute filename even if
# the caller provides a relative one. # the caller provides a relative one.
# parameters : 1 - file name # parameters : 1 - file name
# 2 - old extension # 2 - old extension
# 3 - new extension (may be empty) # 3 - new extension (may be empty)
echo `dirname $1`\/`basename $1 $2`$3 echo `dirname $1`\/`basename $1 $2`$3
} }
# check that the command line is right, if not print a relevant message. # check that the command line is right, if not print a relevant message.
if [ ! -d $1 -o -z $1 ] || [ "$2" != "gz" -a "$2" != "bz2" ] if [ ! -d $1 -o -z $1 ] || [ "$2" != "gz" -a "$2" != "bz2" ]
then then
echo "Usage : $0 /path/to/doc/dir gz/bz2" echo "Usage : $0 /path/to/doc/dir gz/bz2"
echo "e.g. $0 /usr/info gz to compress info pages in gzip format" echo "e.g. $0 /usr/info gz to compress info pages in gzip format"
echo "or $0 /usr/X11R6/man bz2 to compact X man pages using bzip2." echo "or $0 /usr/X11R6/man bz2 to compact X man pages using bzip2."
exit 1 exit 1
fi fi
# set up a few variables. # set up a few variables.
NEWEXT=.$2 # NEWEXT = extension of newly compressed files NEWEXT=.$2 # NEWEXT = extension of newly compressed files
if [ "$NEWEXT" == ".bz2" ] if [ "$NEWEXT" == ".bz2" ]
then then
OLDEXT=".gz" # OLDEXT = extensions of files to recompress OLDEXT=".gz" # OLDEXT = extensions of files to recompress
DECOMPRESS="gunzip -f" # DECOMPRESS = command to decompress a file DECOMPRESS="gunzip -f" # DECOMPRESS = command to decompress a file
COMPRESS="bzip2 -f9" # COMPRESS = command to compress a file COMPRESS="bzip2 -f9" # COMPRESS = command to compress a file
else else
OLDEXT=".bz2" OLDEXT=".bz2"
DECOMPRESS="bunzip2 -f" DECOMPRESS="bunzip2 -f"
COMPRESS="gzip -f9" COMPRESS="gzip -f9"
fi fi
# process all files not in the target format under the provided root directory. # process all files not in the target format under the provided root directory.
@ -59,89 +59,87 @@ cd $1
for f in `find . \! -name "*$NEWEXT"` for f in `find . \! -name "*$NEWEXT"`
do do
# the following test is needed because we have to update links ahead of # the following test is needed because we have to update links ahead of
# ourselves, so $f is sometimes a nonexistent file or a link to one. # ourselves, so $f is sometimes a nonexistent file or a link to one.
if [ -f $f -o -L $f ] if [ -f $f -o -L $f ]
then then
FILE=$f # the file being processed FILE=$f # the file being processed
BASEFILE=`basename $FILE` # its basename (see HLINKS) BASEFILE=`basename $FILE` # its basename (see HLINKS)
INODE=`find $FILE -printf %i` # its inode number (see HLINKS) INODE=`find $FILE -printf %i` # its inode number (see HLINKS)
NEWFILE=`changefileext $FILE $OLDEXT $NEWEXT` # new file name NEWFILE=`changefileext $FILE $OLDEXT $NEWEXT` # new file name
# HLINKS is the list of all hard links to the current file. # HLINKS is the list of all hard links to the current file.
HLINKS=`find . \! -name $BASEFILE -inum $INODE` HLINKS=`find . \! -name $BASEFILE -inum $INODE`
if [ -L $FILE ] if [ -L $FILE ]
then then
# the current file is a symbolic link, so we change # the current file is a symbolic link, so we change
# its name and the name of its target. # its name and the name of its target.
TARGET=`readlink $FILE` TARGET=`readlink $FILE`
rm -f $FILE rm -f $FILE
ln -sf `changefileext $TARGET $OLDEXT $NEWEXT` $NEWFILE ln -sf `changefileext $TARGET $OLDEXT $NEWEXT` $NEWFILE
elif [ -f $FILE ] elif [ -f $FILE ]
then then
# the current file is a regular file. # the current file is a regular file.
TEMPFILE=`changefileext $FILE $OLDEXT` TEMPFILE=`changefileext $FILE $OLDEXT`
# if there are several versions of a page (at worst, there can be # if there are several versions of a page (at worst, there can be
# one uncompressed, one old-compressed and one new-compressed), then # one uncompressed, one old-compressed and one new-compressed), then
# we have to make sure that only the most recent file is kept, because # we have to make sure that only the most recent file is kept, because
# it most likely means the user installed several versions of a package. # it most likely means the user installed several versions of a package.
# first, if we are dealing with an old-compressed file, # first, if we are dealing with an old-compressed file,
# expand it if it is more recent than the uncompressed # expand it if it is more recent than the uncompressed
# file *and* the new-compressed file, else delete it. # file *and* the new-compressed file, else delete it.
# (works even if TEMPFILE and/or NEWFILE do not exist) # (works even if TEMPFILE and/or NEWFILE do not exist)
if [ "$FILE" != "$TEMPFILE" ] if [ "$FILE" != "$TEMPFILE" ]
then then
if [ $FILE -nt $TEMPFILE -a \ if [ $FILE -nt $TEMPFILE -a $FILE -nt $NEWFILE ]
$FILE -nt $NEWFILE ] then
then $DECOMPRESS $FILE
$DECOMPRESS $FILE else
else rm -f $FILE
rm -f $FILE fi
fi FILE=$TEMPFILE
FILE=$TEMPFILE fi
fi
# now we are dealing with an uncompressed file that may # now we are dealing with an uncompressed file that may
# exist or not (because of the above). If it is newer # exist or not (because of the above). If it is newer
# than both the new-compressed and the old-compressed # than both the new-compressed and the old-compressed
# files then it is compressed, else it is deleted. # files then it is compressed, else it is deleted.
if [ -f $FILE ] if [ -f $FILE ]
then then
if [ $FILE -nt $NEWFILE -a \ if [ $FILE -nt $NEWFILE -a $FILE -nt $FILE$OLDEXT ]
$FILE -nt $FILE$OLDEXT ] then
then $COMPRESS $FILE
$COMPRESS $FILE else
else rm -f $FILE
rm -f $FILE fi
fi fi
fi fi
fi
# update the hard links to the current files, # update the hard links to the current files,
# as the new inode number is now known. # as the new inode number is now known.
for g in $HLINKS for g in $HLINKS
do do
rm -f $g rm -f $g
ln -f $NEWFILE `changefileext $g $OLDEXT $NEWEXT` ln -f $NEWFILE `changefileext $g $OLDEXT $NEWEXT`
done done
fi fi
done done
<command>EOF <command>EOF
chmod 755 /usr/bin/comprdoc</command></userinput></screen> chmod 755 /usr/bin/comprdoc</command></userinput></screen>
<para>Now, as root, you can issue a <para>Now, as root, you can issue a
<userinput><command>/usr/bin/comprdoc /usr/man bz2</command></userinput> <userinput><command>/usr/bin/compressdoc /usr/man bz2</command></userinput>
to compress your system man pages. Similarly, you can run it on the to compress your system man pages. Similarly, you can run it on the
<filename class="directory">/usr/info</filename> directory. Don't forget <filename class="directory">/usr/info</filename> directory. Don't forget
<filename class="directory">/usr/X11R6/man</filename> if you install the <filename class="directory">/usr/X11R6/man</filename> if you install the
@ -149,12 +147,12 @@ to compress your system man pages. Similarly, you can run it on the
<application>XEmacs</application>, also install their documentation in <application>XEmacs</application>, also install their documentation in
nonstandard places.</para> nonstandard places.</para>
<para>Generally, package installation systems do not compress the man/info <para>Generally, package installation systems do not compress man/info pages,
pages, which means you will need to run the script again if you want to keep which means you will need to run the script again if you want to keep the size
the size of your documentation as small as possible. Also, note that upgrading of your documentation as small as possible. Also, note that running the script
a package is safe: when you have several versions of a page (for example, a after upgrading a package is safe: when you have several versions of a page
compressed and an uncompressed file), the script always keeps the most recent (for example, one compressed and one uncompressed), the most recent one is kept
and deletes the others.</para> and the others deleted.</para>
</sect1> </sect1>