applied compressdoc patch

git-svn-id: svn://svn.linuxfromscratch.org/BLFS/trunk/BOOK@1295 af4574ff-66df-0310-9fd7-8a98e5e911e0
This commit is contained in:
Larry Lawrence 2003-10-04 01:47:13 +00:00
parent b7d0bb4a52
commit 2fa79e3bf1

View File

@ -13,21 +13,20 @@ to go is to use the script below.
<screen><userinput><command>cat &gt; /usr/bin/compressdoc &lt;&lt; "EOF"</command> <screen><userinput><command>cat &gt; /usr/bin/compressdoc &lt;&lt; "EOF"</command>
#!/bin/bash #!/bin/bash
# VERSION: 20031004.0245
# #
# Compress (with bzip2 or gzip) all man pages in a hierarchy and # Compress (with bzip2 or gzip) all man pages in a hierarchy and
# update symlinks - By Marc Heerdink &lt;marc@koelkast.net&gt;. # update symlinks - By Marc Heerdink &lt;marc @ koelkast.net&gt;
# Modified to be able to gzip or bzip2 files as an option and to deal # Modified to be able to gzip or bzip2 files as an option and to deal
# with all symlinks properly by Mark Hymers # &lt;markh@linuxfromscratch.org&gt; # with all symlinks properly by Mark Hymers &lt;markh @ linuxfromscratch.org&gt;
# #
# Modified 20030925 by Yann E. Morin &lt;yann.morin.1998 @ # anciens.enib.fr&gt; # Modified 20030930 by Yann E. Morin &lt;yann.morin.1998 @ anciens.enib.fr&gt;
# to accept compression/decompression, to correctly handle hard-links, # to accept compression/decompression, to correctly handle hard-links,
# to allow for changing hard-links into soft- ones, to specify the # to allow for changing hard-links into soft- ones, to specify the
# compression level, to parse the man.conf for all occurrences of MANPATH, # compression level, to parse the man.conf for all occurrences of MANPATH,
# to allow for a backup, to allow to keep the newest version of a page. # to allow for a backup, to allow to keep the newest version of a page.
# #
# TODO: # TODO:
# - inverse the quiet option into a verbose one, so as to be silent
# by default;
# - choose a default compress method to be based on the available # - choose a default compress method to be based on the available
# tool : gzip or bzip2; # tool : gzip or bzip2;
# - when a MANPATH env var exists, use this instead of /etc/man.conf # - when a MANPATH env var exists, use this instead of /etc/man.conf
@ -41,7 +40,7 @@ function help ()
if [ -n "$1" ]; then if [ -n "$1" ]; then
echo "Unknown option : $1" echo "Unknown option : $1"
fi fi
echo "Usage: $0 &lt;comp_method&gt; [options] [dirs]" ( echo "Usage: $0 &lt;comp_method&gt; [options] [dirs]" &amp;&amp; \
cat &lt;&lt; EOT cat &lt;&lt; EOT
Where comp_method is one of : Where comp_method is one of :
--gzip, --gz, -g --gzip, --gz, -g
@ -69,10 +68,12 @@ And where options are :
a page will not be re-compressed if it ends with the same suffix a page will not be re-compressed if it ends with the same suffix
as the method adds (.bz2 for bzip2, .gz for gzip). as the method adds (.bz2 for bzip2, .gz for gzip).
-s Change hard-links into soft-links. Use with _caution_ as the --soft, -S Change hard-links into soft-links. Use with _caution_ as the
first encountered file will be used as a reference. Not used first encountered file will be used as a reference. Not used
when in backup mode. when in backup mode.
--hard, -H Change soft-links into hard-links. Not used when in backup mode.
--conf=dir, --conf dir --conf=dir, --conf dir
Specify the location of man.conf. Defaults to /etc. Specify the location of man.conf. Defaults to /etc.
@ -107,6 +108,7 @@ Note about compression
gain space at the expense of a slight overhead in time. Well, my P4-2.5GHz gain space at the expense of a slight overhead in time. Well, my P4-2.5GHz
does not even let me notice this... :-) does not even let me notice this... :-)
EOT EOT
) | less
} }
# This function checks that the man page is unique amongst bzip2'd, gzip'd and # This function checks that the man page is unique amongst bzip2'd, gzip'd and
@ -144,12 +146,12 @@ function check_unique ()
} }
# OK, parse the command-line for arguments, and initialize to some sensible # OK, parse the command-line for arguments, and initialize to some sensible
# state, that is keep hardlinks, parse /etc/man.conf, be most silent, search # state, that is : don't change links state, parse /etc/man.conf, be most
# man.conf in /etc, and don't force (re-)compression. # silent, search man.conf in /etc, and don't force (re-)compression.
COMP_METHOD= COMP_METHOD=
COMP_SUF= COMP_SUF=
COMP_LVL= COMP_LVL=
FORCE_COMP=no FORCE_OPT=
LN_OPT= LN_OPT=
MAN_DIR= MAN_DIR=
VERBOSE_LVL=0 VERBOSE_LVL=0
@ -179,11 +181,15 @@ while [ -n "$1" ]; do
shift shift
;; ;;
--force|-F) --force|-F)
FORCE_COMP=yes FORCE_OPT=-F
shift shift
;; ;;
--soft|-s) --soft|-S)
LN_OPT=-s LN_OPT=-S
shift
;;
--hard|-H)
LN_OPT=-H
shift shift
;; ;;
--conf=*) --conf=*)
@ -275,15 +281,17 @@ if [ "$FAKE" != "no" ]; then
echo " ($COMP_METHOD)" echo " ($COMP_METHOD)"
echo "Compression level.: $COMP_LVL" echo "Compression level.: $COMP_LVL"
echo "Compression suffix: $COMP_SUF" echo "Compression suffix: $COMP_SUF"
echo "Force compression.: $FORCE_COMP" echo -n "Force compression.: "
echo "man.conf is.......: ${MAN_CONF}/man.conf ($MAN_CONF)" [ "foo$FORCE_OPT" = "foo-F" ] &amp;&amp; echo "yes" || echo "no"
echo -n "Hard links........: " echo "man.conf is.......: ${MAN_CONF}/man.conf"
[ "$LN_OPT" = "-s" -o "$LN_OPT" = "--soft" ] &amp;&amp; echo -n "Convert to symlinks" || echo -n "Keep hardlinks" echo -n "Hard-links........: "
echo " ($LN_OPT)" [ "foo$LN_OPT" = "foo-S" ] &amp;&amp; echo "convert to soft-links" || echo "leave as is"
echo -n "Soft-links........: "
[ "foo$LN_OPT" = "foo-H" ] &amp;&amp; echo "convert to hard-links" || echo "leave as is"
echo "Backup............: $BACKUP" echo "Backup............: $BACKUP"
echo "Faking (yes!).....: $FAKE" echo "Faking (yes!).....: $FAKE"
echo "Directories.......: $MAN_DIR" echo "Directories.......: $MAN_DIR"
echo "Silence level.....: $VERBOSE_LVL ($VERBOSE_OPT)" echo "Verbosity level...: $VERBOSE_LVL"
exit 0 exit 0
fi fi
@ -309,6 +317,7 @@ fi
# I know MAN_DIR has only absolute path names # I know MAN_DIR has only absolute path names
# I need to take into account the localized man, so I'm going recursive # I need to take into account the localized man, so I'm going recursive
for DIR in $MAN_DIR; do for DIR in $MAN_DIR; do
MEM_DIR=`pwd`
cd "$DIR" cd "$DIR"
for FILE in *; do for FILE in *; do
# Fixes the case were the directory is empty # Fixes the case were the directory is empty
@ -321,19 +330,21 @@ for DIR in $MAN_DIR; do
if [ ! -L "$FILE" -a ! -e "$FILE" ]; then continue; fi if [ ! -L "$FILE" -a ! -e "$FILE" ]; then continue; fi
if [ -d "$FILE" ]; then if [ -d "$FILE" ]; then
cd "${MEM_DIR}" # Go back to where we ran "$0", in case "$0"=="./compressdoc" ...
# We are going recursive to that directory # We are going recursive to that directory
echo "-&gt; Entering ${DIR}/${FILE}..." &gt; $DEST_FD0 echo "-&gt; Entering ${DIR}/${FILE}..." &gt; $DEST_FD0
# I need not pass --conf, as I specify the directory to work on # I need not pass --conf, as I specify the directory to work on
# But I need exit in case of error # But I need exit in case of error
"$0" ${COMP_METHOD} ${COMP_LVL} ${LN_OPT} ${VERBOSE_OPT} "${DIR}/${FILE}" || exit 1 "$0" ${COMP_METHOD} ${COMP_LVL} ${LN_OPT} ${VERBOSE_OPT} ${FORCE_OPT} "${DIR}/${FILE}" || exit 1
echo "&lt;- Leaving ${DIR}/${FILE}." &gt; $DEST_FD1 echo "&lt;- Leaving ${DIR}/${FILE}." &gt; $DEST_FD1
cd "$DIR" # Needed for the next iteration of the loop
else # !dir else # !dir
if ! check_unique "$DIR" "$FILE"; then continue; fi if ! check_unique "$DIR" "$FILE"; then continue; fi
# Check if the file is already compressed with the specified method # Check if the file is already compressed with the specified method
BASE_FILE=`basename \`basename "$FILE" .bz2\` .gz` BASE_FILE=`basename \`basename "$FILE" .bz2\` .gz`
if [ "${FILE}" = "${BASE_FILE}${COMP_SUF}" -a "${FORCE_COMP}" = "no" ]; then continue; fi if [ "${FILE}" = "${BASE_FILE}${COMP_SUF}" -a "foo${FORCE_OPT}" = "foo" ]; then continue; fi
# If we have a symlink # If we have a symlink
if [ -h "$FILE" ]; then if [ -h "$FILE" ]; then
@ -355,7 +366,14 @@ for DIR in $MAN_DIR; do
LINK=`ls -l $FILE | cut -d "&gt;" -f2 | tr -d " "` LINK=`ls -l $FILE | cut -d "&gt;" -f2 | tr -d " "`
fi fi
rm -f "$FILE" &amp;&amp; ln -s "${LINK}$COMP_SUF" "${FILE}$COMP_SUF" if [ "$LN_OPT" = "-H" ]; then
# Change this soft-link into a hard- one
rm -f "$FILE" &amp;&amp; ln "${LINK}$COMP_SUF" "${FILE}$COMP_SUF"
chmod --reference "${LINK}$COMP_SUF" "${FILE}$COMP_SUF"
else
# Keep this soft-link a soft- one.
rm -f "$FILE" &amp;&amp; ln -s "${LINK}$COMP_SUF" "${FILE}$COMP_SUF"
fi
echo "Relinked $FILE" &gt; $DEST_FD1 echo "Relinked $FILE" &gt; $DEST_FD1
# else if we have a plain file # else if we have a plain file
@ -386,7 +404,7 @@ for DIR in $MAN_DIR; do
;; ;;
esac esac
# Compress the file with the highest compression ratio, if needed # Compress the file with the given compression ratio, if needed
case $COMP_SUF in case $COMP_SUF in
*bz2) *bz2)
bzip2 ${COMP_LVL} "$FILE" &amp;&amp; chmod 644 "${FILE}${COMP_SUF}" bzip2 ${COMP_LVL} "$FILE" &amp;&amp; chmod 644 "${FILE}${COMP_SUF}"
@ -405,7 +423,13 @@ for DIR in $MAN_DIR; do
if [ -n "$HLINKS" ]; then if [ -n "$HLINKS" ]; then
for i in $HLINKS; do for i in $HLINKS; do
NEWFILE=`echo $i | sed s/\.gz$// | sed s/\.bz2$//` NEWFILE=`echo $i | sed s/\.gz$// | sed s/\.bz2$//`
ln ${LN_OPT} "${FILE}$COMP_SUF" "${NEWFILE}$COMP_SUF" if [ "$LN_OPT" = "-S" ]; then
# Make this hard-link a soft- one
ln -s "${FILE}$COMP_SUF" "${NEWFILE}$COMP_SUF"
else
# Keep the hard-link a hard- one
ln "${FILE}$COMP_SUF" "${NEWFILE}$COMP_SUF"
fi
chmod 644 "${NEWFILE}$COMP_SUF" # Really work only for hard-links. Harmless for soft-links chmod 644 "${NEWFILE}$COMP_SUF" # Really work only for hard-links. Harmless for soft-links
done done
fi fi