#!/bin/sh
#
# TODO:
# In single package mode check other packages for duplicate docfiles????. (this
# will NOT be done by SDK - simply a question of opportunity cost/time)
# Delete temp files (or print warning on stderr) when exit before completion
# (and trap signals??) - I doubt I'll be doing this one either
# Support for more than one DOCDIR at a time? (particularly when in pkgheader
# mode). Now that I have a manual pager which supports compressed source (as
# well as compressed cats) it would be nice to be able to compress files below
# usr/doc and $MANPATH all in one go - I may do this, time permitting.
# After zdd interactive has taken input redisplay the Working... type screen?
#
# BUGS (more to do with shell limitations than anything else):
# The relative-link resolving section can fail if the 'pwd' implementation used
# prints the fully resolved name of the current directory (i.e. ignoring any
# symlinks)
# Under ash there needs to be a space between an option and its argument
# v0.6 - I'm getting 'segmentation fault' under ash, wierd... -- no-one
# else has reported this, and I did discover a mismatched RAM problem in my
# system, maybe it was that.
#
# I don't expect to work on this again, now that disc space is so cheap and
# is not a big issue. AFAIK, this current script just about works and is
# quite handy, but making it better is not a priority. Jan 2000.

#unset DEBUG - temporarily disabled, because:
DEBUG=1 # default under beta release
if echo $* | grep -wq "\-\-debug2" ; then
	echo "zdd: Verbose logging on" 1>&2
	set -x
fi

# formerly (up to version 0.50 beta) known as 'zipdocdirs'
if echo $* | grep -wq "\-\-version" ; then
	echo "zdd (zipdocdirs) version 0.99 beta" 1>&2
	exit 0
fi

#
ZDD="$0"
[ "`echo "$ZDD" | cut -b 1`" = / ] || ZDD="`pwd`/$ZDD"
#
if echo $* | grep -wq "\-\-copyright" ; then
	cat <<EOF 1>&2
`sh "$ZDD" --version`
By Stephen Kennedy, G0LRI
<steve@sdk-software.com>  http://www.sdk-software.com/

Part of the \`morepkgtools\` package. See manpages zdd(8) and
morepkgtools(8).

Copyright (C) 1995-2000, S D K Software

All rights reserved.

Distribution and use of this script is permitted provided that the
following conditions are met:

 Distributions of this script must retain the above copyright
 notice, this list of conditions and the disclaimer below.
 If the script is modified it may be distributed provided this copyright
 notice is not removed and provided any changes you make are clearly marked.
 If the script is changed in any substantial way it must be distributed
 under a different name BUT with this copyright notice intact.
 Extracts of the script may be used in your work provided you include my
 copyright notice, list of conditions and disclaimer.

I also request, but do not insist, that the script is distributed together
with its manpage or, ideally, in its Slackware Package form as released by
me and that you send any suggested changes to me for inclusion in the
"official" version.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR \`AS IS' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Note: This is intended to be free software as currently defined,
available to all for use and distribution as they see fit, any
limitations herein are only to protect me from liability and to give me
proper credit as author.


Extracts from:
----------------------------------------------------------------------------
pkgtool v2.2.0, pkgtool.tty v5.0.0

Copyright 1993, 1994, 1998 Patrick Volkerding, Moorhead, Minnesota USA
All rights reserved.

Redistribution and use of this script, with or without modification, is
permitted provided that the following conditions are met:

1. Redistributions of this script must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   [The disclaimer is *exactly* the same as the one above (the paragraph in
   block capitals]

<volkerdi@slackware.com>
----------------------------------------------------------------------------
tbackup v0.8 (g0lri's note - tbackup is an excellent set of scripts which
              together form "a general backup system for Linux")

(c) 1993, 1994  Koen Holtman
This is free software.  Use it at your own risk.
Tbackup may be freely redistributed provided that
  1) this notice is left intact and
  2) any changes or extensions to tbackup are documented
     as such in this file.

<koen@stack.urc.tue.nl>
----------------------------------------------------------------------------
EOF
	exit 0
fi

PATH="`pwd`:$PATH"

# based on pkgtool 2.2.0 by Patrick Volkerding (see copyright notice above):
if [ ! -d /usr/sbin ] ; then # we must be on the bootdisk
# if TARGET_DIR is already set we hope the user knows what (s)he is doing
	TARGET_DIR=${TARGET_DIR:=/mnt}
	if mount | fgrep "on /mnt" 1> /dev/null 2>&1 ; then # good
		PATH="$PATH:/mnt/usr/local/sbin"
	else # bad
		cat <<EOF 1>&2


You can't run zdd from the rootdisk until you've mounted
your Linux partitions beneath /mnt. Here are some examples of this:

If your root partition is /dev/hda1, and is using ext2fs, you would type:
mount /dev/hda1 /mnt -t ext2

Then, supposing your /usr partition is /dev/hda2, you must do this:
mount /dev/hda2 /mnt/usr -t ext2

Please mount your Linux partitions and then run zdd again.

EOF
		exit 2
	fi
else
	TARGET_DIR=${TARGET_DIR:=/}
fi
# end of pkgtool borrowing

if echo $* | grep -qw "\-\-help" ; then
	cat <<EOF
`sh "$ZDD" --version 2>&1`: compress all the files in a directory and below, and then update the Slackware package record files

Usage: zdd [options] [package ...]

Options:

--all,-a		Compress all files below \$DOCDIR
--allpackages,-p	Only compress files listed in \$PACKAGES or \$SCRIPTS
--copyright
--debug | --debug2	Extra-verbose output (--debug2 is EXTREMELY verbose)
--display,-d DISPLAY	Open an xterm on DISPLAY for zdd's output
--filter,-f REGEX	Do not compress files which exactly match REGEX
--geometry,-g GEOMETRY	Open an xterm with GEOMETRY for zdd's output
--help
--notlinks,-l		Do not deal with symlinks other than those listed in
			\$SCRIPTS
--notscripts,-S		Do not deal with symlinks or scripts at all
--only,-o
--quick,-q		Just report irregularities, don't prompt user
--quiet			Suppress information-only messages
--textonly
--tty,-t TERMINAL	Display zdd's output on TERMINAL
--version
--warn			Perform a dummy run
--xterm,-x		Open an xterm for the display of zdd's output
-#			Speed of compression, from 1 (fastest) to 9 (best)

EOF
	exit 0
fi


TEST="$TARGET_DIR/usr/bin/test"

# bomb out if another zdd process is running (could end up with
# out-of-sync package files otherwise):

if "$TEST" -e /tmp/.zdd_is_running ; then
	echo "Lockfile /tmp/.zdd_is_running exists - delete if another zdd process is NOT running" 2>&1
	exit 1
else
	touch /tmp/.zdd_is_running || { echo "Can't create lockfile /tmp/.zdd_is_running - exiting" ; exit 1 }
fi

TMP=${TMP:="$TARGET_DIR/var/log/setup/tmp2"} # the "2" to make it totally impossible that we step on pkgtool's toes
TMP="`echo "$TMP/" | tr -s /`"
OLDIFS="$IFS"

# Patch possible /tmp security holes
if [ ! -d "$TMP" ]; then # make a secure temp directory: (pkgtool patch)
	rm -rf "$TMP" >/dev/null 2>&1
	mkdir -p "$TMP"
	chmod 700 "$TMP"
# end of pkgtool borrowing
else # is a directory, if we inherited an environment variable and the user still wants to use /tmp,
     # it's not very sociable to chmod it to 700 or delete files. We'll add a directory, and warn
     # about this in manpage
	if [ "$TMP" = "`echo "$TARGET_DIR/tmp/" | tr -s /`" ] ; then
		TMP="`echo $TMP/packagetools.tmp | tr -s /`"
		if [ ! -d "$TMP" ]; then rm -rf "$TMP" >/dev/null 2>&1 ; fi
		mkdir -p "$TMP"
	fi
	
	chmod 700 "$TMP"
fi

# maybe we imported a duff TMP or the mkdir failed:
if [ ! -d "$TMP" ] ; then # in view of the patch, we're not being so kind as we used to be in finding
# somewhere to store our files. If there's no temp dir, we don't try to find one, we exit.
	echo "\"$TMP\" is NOT a directory. I need to have somewhere to store my working files, sorry\!" 1>&2
	logger -t zdd[$$] "\"$TMP\" - no such directory"
	rm /tmp/.zdd_is_running
	exit 1
fi

# strip leading slashes
strip_search() {
	{ [ "`echo $1 | cut -b 1`" = "/" ] && echo $1 | tr -s / | cut -b 2- ; } || echo $1 | tr -s /
}

# this just removes one trailing slash - filter through tr before calling
strip_tail() {
	local LN=`expr length "$1"`
	{ [ "`echo $1 | cut -b $LN-`" = "/" ] && echo $1 | cut -b 1-$(expr $LN - 1) ; } || echo $1
}

if [ ! "$DOCDIR" ] ; then
	DOCDIR="usr/doc"
else
	DOCDIR="$(strip_tail "$(strip_search "$DOCDIR")")"
fi

ADM_DIR=${ADM_DIR:="`echo "$TARGET_DIR/var/log" | tr -s /`"}
PACKAGES=${PACKAGES:="`echo "$ADM_DIR/packages" | tr -s /`"}
SCRIPTS=${SCRIPTS:="`echo "$ADM_DIR/scripts" | tr -s /`"}
LOG=${LOG:="$TMP/zdd.log.$$"}
PAGER=${PAGER:=less}

unset strip_tail
renice -12 $$ >/dev/null 2>&1
TMP="$TMP/zdd.$$"
mkdir "$TMP" >/dev/null 2>&1
chmod 700 "$TMP"
echo >"$LOG"

if [ ! -d "$SCRIPTS" ] ; then
	cat <<EOF 1>&2
The SCRIPTS variable is set to "$SCRIPTS", and that is NOT a directory!!

Maybe you are using an old version of Slackware? If you are, type
'export ADM_DIR=/var/adm ; unset PACKAGES SCRIPTS' then try again.

If you're still stuck read the manpage or ask a Unix wizard to help you :-).
EOF
	logger -t zdd[$$] "\"$SCRIPTS\" - no such directory"
	rm /tmp/.zdd_is_running
	exit 1
fi

if [ ! -d "$PACKAGES" ] ; then
	cat <<EOF 1>&2
The PACKAGES variable is set to "$PACKAGES", and that is NOT a directory!!

Maybe you are using an old version of Slackware? If you are, type
'export ADM_DIR=/var/adm ; unset PACKAGES SCRIPTS' then try again.

If you're still stuck read the manpage or ask a Unix wizard to help you :-).
EOF
	logger -t zdd[$$] "\"$PACKAGES\" - no such directory"
	rm /tmp/.zdd_is_running
	exit 1
fi


if [ ! -d "$TARGET_DIR/$DOCDIR" ] ; then
	cat <<EOF 1>&2
The TARGET_DIR variable is set to "$TARGET_DIR" and the DOCDIR
variable is set to "$DOCDIR".

"$TARGET_DIR/$DOCDIR" is NOT a directory!!
EOF
	logger -t zdd[$$] "\"$TARGET_DIR/$DOCDIR\" - no such directory"
	rm /tmp/.zdd_is_running
	exit 1
fi


TEMP=0
unset PACKAGE_NAME PACKAGEFILE 

# before going any further we need to know whether we can use ncurses or not
if [ ! -t 0 ] || echo $* | grep -qw "\-\-textonly" || echo $* | grep -qw "\-\-quiet" ; then
	TEXTONLY=1
elif [ `which dialog >/dev/null 2>&1 ; echo $?` != 0 ] ; then
	echo "\`dialog\` not found - switching to \"--textonly\" mode" 1>&2
	TEXTONLY=1
fi


# Note: version 0.3 supported --xterm with geometry and display as optional
# arguments. However, this wouldn't work under ash which doesn't seem to
# increment OPTIND each time it starts a new arg (so one can't reference
# OPTIND within the getopts function or exit the function prematurely).
# ash really sucks :-(

get_opts() {
	IFS="$OLDIFS"
	unset PKGHEADER GZSPEED
	local OPTION
	while getopts 123456789ad:g:pf:lo:SPqQt:TwxzZ OPTION ; do #z/Z = debug/2
		case $OPTION in

			[1-9]) if [ "$GZSPEED" ] ; then # we've been here before
				echo "Ambigous command line - you have specified more than one number" 1>&2
				rm /tmp/.zdd_is_running
				exit 2
			fi
			GZSPEED=$OPTION ;;

			a) unset JUSTPACKAGES ;; # this is the default

			d) XTERM=1
			DisplaY="$OPTARG" ;;

			g) XTERM=1
			GEOMETRY="$OPTARG" ;;

			p) JUSTPACKAGES=1
			NOTSYMLINKS=1 ;;

			f) FILTER="$OPTARG"
			if [ "$OFILTER" ] ; then
				echo "You must use EITHER --filter OR --only, NOT both" 1>&2
				rm /tmp/.zdd_is_running
				exit 2
			fi ;;

			l) NOTSYMLINKS=1 ;;

			o) OFILTER="$OPTARG"
			if [ "$FILTER" ] ; then
				echo "You must use EITHER --filter OR --only, NOT both" 1>&2
				rm /tmp/.zdd_is_running
				exit 2
			fi ;;

			S) NOTSCRIPTS=1
			NOTSYMLINKS=1 ;;

			P) # This must be the LAST option on the command line
			bug_report() {
				echo "$1. If zdd has been called up by pkgheader or mkpkg please send a bug report to the author, quoting the following version numbers: `sh pkgheader --version 2>&1`, `sh "$ZDD" --version 2>&1`, `sh pkginstall --version 2>&1`." | tee -a "$LOG" | logger -st zdd[$$]
				sleep 2
				rm -fr "$TMP" 2>/dev/null
				rm /tmp/.zdd_is_running
				exit 1
			}
			PKGHEADER=1 ;;

			q) QUICK=1 ;;

			Q) QUIET=1
			QUICK=1
			TEXTONLY=1 ;; # quiet ncurses operation would be
				      # pointless methinks

			T) # TEXTONLY should already be set, but no harm in
			# making sure!
			TEXTONLY=1 ;;

			t) TTY="$OPTARG"
			test -c "$TTY" || { echo "\"$TTY\" is NOT a terminal" >>"$LOG"; TTY=/dev/null; }
			ln -s "$TTY" "$TMP/logttydev" ;;

			w) WARNONLY=1
			QUICK=1
			mkdir "$TMP/packages" "$TMP/scripts" 2>/dev/null ;;

			x) XTERM=1 ;;

			z) # debug-level output
			DEBUG=1 ;;

			Z) # debug2-level output
			# should already be set, but ok to set again:
			DEBUG=2
			set -x ;;

			# This MUST come last!!
			*) sh "$ZDD" --help 1>&2
			rm /tmp/.zdd_is_running
			exit 1 ;;

		esac
	done
}

CMDLINE="$@"
IFS='
'
# --pkgheader, -P, -Q and -T are undocumented and unsupported
get_opts $(for GET_OPTS in "$@" ; do echo $GET_OPTS | sed -e 's/--all\>/-a/' -e 's/--allpackages\>/-p/' -e 's/--debug\>/-z/' -e 's/--debug2\>/-Z/' -e 's/--display=\|--display\>/-d/' -e 's/--geometry=\|--geometry\>/-g/' -e 's/--filter=\|--filter\>/-f/' -e 's/--only=\|--only\>/-o/' -e 's/--notlinks\>/-l/' -e 's/--notscripts\>/-S/' -e 's/--pkgheader\>/-P/' -e 's/--quick\>/-q/' -e 's/--quiet\>/-Q/' -e 's/--textonly\>/-T/' -e 's/--tty=\|--tty\>/-t/' -e 's/--warn\>/-w/' -e 's/--xterm\>/-x/' ; done)

shift $(expr $OPTIND - 1)
GZSPEED=${GZSPEED:=9} # compress with -9 unless user has directed otherwise

if [ "$PKGHEADER" ] ; then
	if [ $# -lt 3 ] ; then
		bug_report "zdd: not enough arguments (\"$CMDLINE\")"
	else
		HEADER_FILE="$1"
		FILE_LIST="$2"
		PACKAGE_NAME="$3"
		[ $# -gt 3 ] && SCRIPTFILE="$4"
# we should set "--notlinks" but not "--notscripts"
		ONEPACKAGEONLY=1
		QUICK=1
		LOG() {
			cat - >>"$LOG"
		}
	fi
fi

unset get_opts GET_OPTS CMDLINE

if [ "$TEXTONLY" -a ! "$QUIET" -a ! "$QUICK" ] ; then
	please_wait() {
		cat <<EOF

Processing. Please wait.

EOF
	}
elif [ ! "$TEXTONLY" -a ! "$QUICK" -a ! "$QUIET" ] ; then
	please_wait() {
		dialog --title "zdd" --infobox "Processing. Please wait." 3 40
	}
else
	please_wait() {
		:
	}
fi

if [ ! "$PKGHEADER" ] ; then
# this variable name is a blast from the past:
	[ $# != 0 ] && ONEPACKAGEONLY=$#
# from tbackup 0.8 by Koen Holtman (see above), with changes:
# (many of the changes were to make this work with the ash shell)
		if [ ! "$TTY" ] ; then
			case "`tty`" in
# actually, this doesn't check for *X*, as a telnet session uses /dev/ttyp? too.
# not a problem though, I'll just change the manual so that it becomes a feature
# :-)
				/dev/tty[0-9]*|/dev/console) virtcons=1
			esac
			if [ "$virtcons" -a ! "$XTERM" ] ; then
				ln -s /dev/tty8 "$TMP/logttydev"
				TTY=/dev/tty8
			else
				touch "$TMP/.we are running"
				if [ "$QUIET" ] ; then
					echo "
[Creating progress window.]
" >>"$LOG"
				else
					echo "
[Creating progress window.]
" | tee -a "$LOG"
				fi
				if which xterm >/dev/null 2>&1 ; then
					cat <<EOF >"$TMP/temp.sh"
ln -s \`tty\` "$TMP/logttydev"
while "$TEST" -e "$TMP/.we are running" ; do sleep 45 ; done
echo Press enter to close window.
read
EOF
					TTY="the xterm that has popped up"
					GEOMETRY=${GEOMETRY:=90x23}
					if [ "$DisplaY" ] ; then
						xterm -sb -sl 500 -display $DisplaY -geometry $GEOMETRY -title "zdd progress"  -e sh "$TMP/temp.sh" &
					else
						xterm -sb -sl 500 -geometry $GEOMETRY -title "zdd progress"  -e sh "$TMP/temp.sh" &
					fi

# wait for creation of logttydev, timeout after 10 seconds or if the xterm exits
				t=10
				until "$TEST" -e "$TMP/logttydev"; do
					if [ $t = 0 ] || [ `"$TEST" -e /proc/$! ; echo $?` != 0 ] ; then
						if [ "$QUIET" ] ; then
							echo "WARNING: Can't create progress window.  Writing to /dev/null." >>"$LOG"
						else
							echo "WARNING: Can't create progress window.  Writing to /dev/null." | tee -a "$LOG"
						fi
						ln -s /dev/null "$TMP/logttydev"
						kill -KILL $! 2>/dev/null
						break
					fi
					sleep 1
					t=`expr $t - 1`
				done
			else
				if [ "$QUIET" ] ; then
					echo "WARNING: Can't find xterm program.  Writing to /dev/null." >>"$LOG"
				else
					echo "WARNING: Can't find xterm program.  Writing to /dev/null." | tee -a "$LOG"
				fi
				ln -s /dev/null "$TMP/logttydev"
				TTY=/dev/null
			fi
# end of tbackup borrowing
		fi
	fi
	LOG() {
		tee -a "$LOG" >"$TMP/logttydev"
	}
fi

cat <<EOF|LOG
zdd by Stephen Kennedy, with extracts from pkgtool by Patrick Volkerding
and tbackup by Koen Holtman.
Logfile for `hostname`, `date`.

Your disk usage at the start was:
`df`

EOF
[ "$WARNONLY" ] && echo "
** WARNONLY mode **
" | LOG

make_a_grep_for_it() {
	echo "looking for doc files in the \"$PACKAGE_NAME\" package" | LOG
	sort -u "$TMP/file-lists.old/$PACKAGE_NAME" >"$TMP/file-lists.old/$PACKAGE_NAME.tmp"

	local LINE DOCFILE

	if grep -qxE '/.*|.*//.*' "$TMP/file-lists.old/$PACKAGE_NAME.tmp" ; then
		IFS='
'
		for LINE in `cat "$TMP/file-lists.old/$PACKAGE_NAME.tmp"; IFS="$OLDIFS"` ; do
			strip_search "$LINE" >> "$TMP/file-lists.old/$PACKAGE_NAME.tmp.$$"
		done
		mv -f "$TMP/file-lists.old/$PACKAGE_NAME.tmp.$$" "$TMP/file-lists.old/$PACKAGE_NAME.tmp"
	fi

	IFS='
'
	for DOCFILE in `grep "$DOCDIR/" "$TMP/file-lists.old/$PACKAGE_NAME.tmp" ; IFS="$OLDIFS"` ; do

# filter out the rubbish:
		case "$DOCFILE" in

			*/|*.gz|*.z|*.Z|*.tgz|*.taz|*.zip) : ;;

			"$DOCDIR"*) test -d "$TARGET_DIR/$DOCFILE"
			if [ $? != 0 ] ; then # DOCFILE is NOT a directory
				if [ "`find "$TARGET_DIR/$DOCFILE" -printf %F`" = "iso9660" ] ; then # file is on CDROM
					[ "$DEBUG" ] && echo "* Skipping \"$DOCFILE\" - file is on CD/ROM" | LOG
					echo "$DOCFILE" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "File is on CD/ROM" >>"$TMP/DOCS_NOT_ZIPPED.why"
					continue
				fi
				if [ "$FILTER" ] && echo $DOCFILE | grep -Eqx $FILTER ; then # filter out regex:
					[ "$DEBUG" ] && echo "* Skipping \"$DOCFILE\" - matches --filter regex" | LOG
					echo "$DOCFILE" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "Matches \"--filter\" regex" >>"$TMP/DOCS_NOT_ZIPPED.why"
					continue
				fi
				if [ "$OFILTER" ] && echo $DOCFILE | grep -Eqxv $OFILTER ; then # filter out files NOT matching regex
					[ "$DEBUG" ] && echo "* Skipping \"$DOCFILE\" - does not match --only regex" | LOG
					echo "$DOCFILE" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "Does not match \"--only\" regex" >>"$TMP/DOCS_NOT_ZIPPED.why"
					continue
				fi
				echo "$DOCFILE" >> "$TMP/docs2zip/$PACKAGE_NAME"
				[ "$DEBUG" ] && echo "* Chosen \"$DOCFILE\" from the \"$PACKAGE_NAME\" package for compression" | LOG
				if [ "$ONEPACKAGEONLY" != 1 ] ; then
					mkdir -p "$TMP/`dirname "$DOCFILE"`"
					echo "$PACKAGE_NAME" >>"$TMP/$DOCFILE"
				fi
			fi ;;
		esac
	done

	if [ -s "$TMP/docs2zip/$PACKAGE_NAME" ] ; then
		comm -13 "$TMP/docs2zip/$PACKAGE_NAME" "$TMP/file-lists.old/$PACKAGE_NAME.tmp" >"$TMP/file-lists/$PACKAGE_NAME"
	else
		mv "$TMP/file-lists.old/$PACKAGE_NAME.tmp" "$TMP/file-lists/$PACKAGE_NAME"
	fi

# now we also have the list of doc files in "$TMP/docs2zip/$PACKAGE_NAME"
# and the remaining package files listed in "$TMP/file-lists/$PACKAGE_NAME"
}

if [ ! "$NOTSCRIPTS" ] ; then
	look_for_links() {

		target() {
			[ "$TARGET_DIR" != "/" ] && LINKPOINTSTO="$(echo "$LINKPOINTSTO" | tr -s / | cut -b $(expr length "$TARGET_DIR" + 1)-)"
		}

		do_nothing() {
			echo "$1" | LOG
			echo "$LINEIS" >>"$TMP/.scripts/.$SCRIPT.new"
			continue
		}

		captain_sensible() {
			was_it_zipped "$TMP/DOCS"
			if [ "$Reply" = "" ] ; then
				was_it_zipped "$TMP/DOCS_NOT_ZIPPED"
				if [ "$Reply" = "" ] ; then
					cat <<EOF >> "$TMP/.scripts/.$SCRIPT.new"
`(cd "$TMP/.scripts" ; sed -n ''$(expr $KLF - 1)' p' "$SCRIPT")`
$LINEIS
EOF
					[ ! "$ONEPACKAGEONLY" -a ! "$JUSTPACKAGES" ] && printf "ERROR?? " | LOG
					does_it_exist
					echo | LOG
				else
					cat <<EOF|LOG

The symlink "$LINKGOESIN/$LINKNAMEIS" in the script for "$SCRIPT" points to "/$LINKPOINTSTO", a file which was not zipped because `sed -n ${Reply}p "$TMP/DOCS_NOT_ZIPPED.why"`. You may want to rectify this problem and then edit the script (and package file) manually or re-run 'zdd'.

EOF
				fi
				continue
			else # should be a number
				buster_gonads
				if [ "$WARNONLY" ] ; then
					echo "I would have executed the following:" | LOG
				else
					echo "Executing the following:" | LOG
					( cd $LINKGOESIN ; rm -f $LINKNAMEIS 2>&1 | LOG )
					( cd $LINKGOESIN ; rm -rf $LINKNAMEIS.$NEW_SUFFIX 2>&1 | LOG )
					( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS.$NEW_SUFFIX 2>&1 | LOG )
				fi
				echo "( cd $LINKGOESIN ; rm -f $LINKNAMEIS )" | LOG
				cat <<EOF | tee -a "$TMP/.scripts/.$SCRIPT.new" | LOG
( cd $LINKGOESIN ; rm -rf $LINKNAMEIS.$NEW_SUFFIX )
( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS.$NEW_SUFFIX )
EOF
				echo "Updating the installation script for \"$SCRIPT\"..." | LOG
# development note - this could do with more testing:
				echo "$LINKGOESIN/$LINKNAMEIS" >>"$TMP/SYMLINKS"
				if [ "$ONEPACKAGEONLY" != 1 ] ; then
					mkdir -p "$TMP/$LINKGOESIN"
					echo $SCRIPT >>"$TMP/$LINKGOESIN/$LINKNAMEIS"
				fi
			fi
		}

# unless somebody wants to advise me differently, I am only issuing a warning
# for '/./' (obsolete) and not '/../' (relative path) :
		is_messy() {
			echo $LINKPOINTSTO | grep -Eq '\/\.\/|/{2,}' && echo "Warning: Above link is \"messy\"" | LOG
		}

		if [ "$QUICK" ] ; then
			fix_it() {
				do_nothing "$1"
			}
		else
			if [ "$TEXTONLY" ] ; then
				tput clear
				read_input() {
					local INPUT
					read INPUT
					case "`echo $INPUT | tr A-Z a-z`" in

						"") echo "Null input. Try again"
						read_input ;;

						1|*nothin*) do_nothing "$1" ;;

						2) echo "Deleting link (user input)..." | LOG
						rm -f "$LINKGOESIN/$LINKNAMEIS" 2>&1 | LOG
						continue ;;

						3) echo "Deleting link (user input)..." | LOG
						rm -f "$LINKGOESIN/$LINKNAMEIS" 2>&1 | LOG
						do_nothing "$1" ;;

						4|modify*) echo "Input the new link destination (LINKPOINTSTO):"
						read LINKPOINTSTO
						if [ "$LINKPOINTSTO" = "" ] ; then
							echo "Null input. Skipping..."
							do_nothing "$1"
						else
							ln -sf "$LINKPOINTSTO" "/$LINKGOESIN/$LINKNAMEIS" 2>&1 | LOG
							make_LINKPOINTSTO_sane
							captain_sensible
							continue
						fi ;;

						*) echo "Invalid input. Try again"
						read_input ;;
					esac
				}
				fix_it() {
					printf \\007
					cat <<EOF

Dangling link: "/$LINKGOESIN/$LINKNAMEIS"
Reason: $1

Please select one of the following actions:
 1      Do nothing
 2      Delete the link
 3	Delete the link and remove all references to it in the install scripts
 4      Modify the link and update the install scripts

EOF
					read_input "$1"
				}
			else
				fix_it() {
					printf \\007
					dialog --title "zdd interactive" --menu "Dangling link: \"/$LINKGOESIN/$LINKNAMEIS\"
Reason: $1

Select one of the following options:" 17 80 5 \
Nothing "Do Nothing" \
Delete "Delete the link" \
Delmod "Delete the link and remove all references to it in the scripts" \
Modify "Modify the link and update the install scripts" \
Exit "Exit \`zdd\`" 2>"$TMP/reply" || { rm -f "$TMP/reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
					case "`cat "$TMP/reply" ; rm -f "$TMP/reply" 2>/dev/null`" in

						Nothing) do_nothing "$1" ;;

						Delete) echo "Deleting link (user input)..." | LOG
						rm -f "$LINKGOESIN/$LINKNAMEIS" 2>&1 | LOG
						continue ;;

						Delmod) echo "Deleting link (user input)..." | LOG
						rm -f "$LINKGOESIN/$LINKNAMEIS" 2>&1 | LOG
						do_nothing "$1" ;;

						Modify) dialog --title "zdd interactive" --inputbox "Dangling link: \"/$LINKGOESIN/$LINKNAMEIS\"
Reason: $1

Input the new link destination (LINKPOINTSTO):" 12 80 2>"$TMP/LINKPOINTSTO" || { rm -f "$TMP/LINKPOINTSTO"; rm /tmp/.zdd_is_running ; exit; }
						LINKPOINTSTO="`cat "$TMP/LINKPOINTSTO"`"
						rm -f "$TMP/LINKPOINTSTO" 2>/dev/null
						if [ "$LINKPOINTSTO" = "" ] ; then
							echo "Null input. Skipping..."
						else
							ln -sf "$LINKPOINTSTO" "/$LINKGOESIN/$LINKNAMEIS" 2>&1 | LOG
							make_LINKPOINTSTO_sane
							captain_sensible
						fi ;;

						Exit) rm /tmp/.zdd_is_running
						exit ;;

					esac
					please_wait
				}
			fi
		fi
# end of tidylinks borrowing

# yikes. 'ash' alert again. my version of ash appears to have no 'pwd' or
# 'bltin' command as listed in the manpage. /bin/pwd prints the fully resolved
# name of the current directory, ignoring any symlinks - not what I want.
		make_LINKPOINTSTO_sane() {
			cd "$TARGET_DIR"
			unset SIMPLELINK
# the object is to have a full name, less any leading slashes (to aid the
# forthcoming grep), and, if made by a 'pwd', devoid of TARGET_DIR
			if [ "`echo $LINKPOINTSTO | cut -b 1`" = "/" ] ; then # a full path :-)
				echo "Absolute link" | LOG
				is_messy
				if echo $LINKPOINTSTO | grep -Eq '\/\.{1,2}\/' ; then # dots in path
					if test -d "$(strip_search "$(dirname "$LINKPOINTSTO")")" ; then
						LINKPOINTSTO="$(cd "$(strip_search "$(dirname "$LINKPOINTSTO")")" ; pwd)/$(basename "$LINKPOINTSTO")"
					else
						fix_it "The directory which the \"$LINKNAMEIS\"
link points to, \"$(dirname "$LINKPOINTSTO" | tr -s /)\",
does not exist."
						return 0
					fi
					target
				fi
			else # not a full path :-(
				if echo $LINKPOINTSTO | grep -q "\/" ; then # relative link
					echo "Relative link" | LOG
					is_messy
# TODO: set and use this variable to ensure that any link created retains its
# relativeness:
					RELATIVELINK=1
					if test -d "$LINKGOESIN/$(dirname "$LINKPOINTSTO")" ; then
						LINKPOINTSTO="$(echo $(cd "$LINKGOESIN/$(dirname "$LINKPOINTSTO")" ; pwd)/$(basename "$LINKPOINTSTO") | tr -s /)"
					else
						fix_it "The directory which the \"$LINKNAMEIS\"
link points to, \"$(echo $LINKGOESIN/$(dirname "$LINKPOINTSTO") | sed 's/\(\/\.\/\)\(\.\/\)*/\//g')\",
does not exist."
						return 0
					fi
					target
				else # must be a basename only
					LINKPOINTSTO="$LINKGOESIN/$LINKPOINTSTO"
					SIMPLELINK=1
				fi
			fi
			LINKPOINTSTO="$(strip_search "$LINKPOINTSTO")"
			is_it_already_zipped "$LINKPOINTSTO"
# development note - this could do with more testing:
# we are most likely to get here if in "-warn" mode:
			grep -xq "$LINKGOESIN/$LINKNAMEIS" "$TMP/SYMLINKS" && do_nothing "Link already processed in the installation script(s) for:
`which_package_is_it_from "$LINKGOESIN/$LINKNAMEIS"`
"
		}

		is_it_already_zipped() {
			case "$1" in
				*.gz|*.z|*.Z|*.tgz|*.taz|*.zip) do_nothing "\"$1\" - already zipped??. Above line ignored."
			esac
		}

		does_it_exist() {
			if [ "$LINKPOINTSTO" = "$DOCDIR" ] ; then
				echo "The link \"`echo $LINKGOESIN/$LINKNAMEIS | tr -s /`\" points to \$DOCDIR itself" | LOG
			else
				if [ -d "$LINKPOINTSTO" ]  ; then
					echo "\"$LINKPOINTSTO\" is a directory" | LOG
				elif [ -L "$LINKPOINTSTO" ]  ; then
					echo "\"$LINKPOINTSTO\" is itself a symbolic link (ho hum):" | LOG
					ls -dl "$LINKPOINTSTO" 2>/dev/null | LOG
				elif "$TEST" -e "$LINKPOINTSTO" ; then
					cat <<EOF|LOG
The link "$LINKGOESIN/$LINKNAMEIS" points to "/$LINKPOINTSTO" which I have not found in the list of files zipped OR the list of files not zipped
"$LINKPOINTSTO" exists, and is not a directory or another symbolic link. Wierd...
EOF
				else
					cat <<EOF|LOG
The link \"$LINKGOESIN/$LINKNAMEIS\" points to \"/$LINKPOINTSTO\" which I
have not found in the list of files zipped OR the list of files not zipped
"$LINKPOINTSTO" does NOT exist
EOF
					local NOT_EXIST=1
				fi
				"$TEST" -e "$LINKPOINTSTO.gz"
				local gz_x=$?
				[ $gz_x = 0 ] && echo "\"$LINKPOINTSTO.gz\" exists" | LOG
				"$TEST" -e "$LINKPOINTSTO.z"
				local z_x=$?
				[ $z_x = 0 ] && echo "\"$LINKPOINTSTO.z\" exists" | LOG
				"$TEST" -e "$LINKPOINTSTO.Z"
				local Z_x=$?
				[ $Z_x = 0 ] && echo "\"$LINKPOINTSTO.Z\" exists" | LOG
				[ "$NOT_EXIST" -a ! "$QUICK" ] && fix_it "$(echo "\"$LINKPOINTSTO\" does NOT exist$([ $gz_x = 0 ] && echo " BUT \"$LINKPOINTSTO.gz\" exists")$([ $z_x = 0 ] && echo " BUT \"$LINKPOINTSTO.z\" exists")$([ $Z_x = 0 ] && echo " BUT \"$LINKPOINTSTO.Z\" exists")" | sed 's/./&\
/68)"
			fi
		}

		was_it_zipped() {
			Reply=`grep -nx "$LINKPOINTSTO" "$1" | sed -e 's/:.*//' -ne 1p`
		}

		buster_gonads() {
			NEW_SUFFIX=`sed -n ${Reply}p "$TMP/DOCS_ZIPPED"`
			[ "$NEW_SUFFIX" != "gz" ] && do_nothing "\"/$LINKPOINTSTO.gz\" already existed so
\"/$LINKPOINTSTO\" was compressed as \"/$LINKPOINTSTO.$NEW_SUFFIX\".
Doing nothing."
# if the link was originally recorded as a simple link preserve it as such:
			if [ "$SIMPLELINK" ] ; then
				LINKPOINTSTO="$(basename "$LINKPOINTSTO").$NEW_SUFFIX"
			else
# add back the leading slash and add the new suffix:
				LINKPOINTSTO="/$LINKPOINTSTO.$NEW_SUFFIX"
			fi
# deal with spaces in names:
			[ `echo $LINKPOINTSTO | wc -w | tr -d ' '` -gt 1 ] && LINKPOINTSTO="\"$LINKPOINTSTO\""
			[ `echo $LINKNAMEIS | wc -w | tr -d ' '` -gt 1 ] && LINKNAMEIS="\"$LINKNAMEIS\""
			[ `echo $LINKGOESIN | wc -w | tr -d ' '` -gt 1 ] && LINKGOESIN="\"$LINKGOESIN\""
		}


# Stage 3 - DEAL WITH INSTALLATION SCRIPTS:
		touch "$TMP/DOCS" "$TMP/DOCS_ZIPPED" "$TMP/DOCS_NOT_ZIPPED" "$TMP/SYMLINKS"

		if [ "$PKGHEADER" ] ; then
			if [ ! "$SCRIPTFILE" ] ; then # we have no business here
				return 0
			elif [ "$TEXTONLY" -a ! "$QUIET" ] ; then
				echo "Dealing with installation script"
			elif [ ! "$TEXTONLY" ] ; then
				dialog  --title "zdd" --infobox "Dealing with installation script" 3 70
			fi
		else
			if [ "$TEXTONLY" -a ! "$QUIET" ] ; then
				cat <<EOF

Now looking for symbolic links and installation scripts which may need to be changed...

You guessed it - this may take quite a while!! :-(
EOF
			elif [ ! "$TEXTONLY" ] ; then
				dialog  --title "zdd" --infobox "Now looking for symbolic links and installation scripts which
may need to be changed...

You guessed it - this may take quite a while :-(" 6 70
			fi
			cat <<EOF|LOG

Now looking for symbolic links and installation scripts which may need to be changed...

EOF
		fi

# TODO: in single package mode ought we check all other installation scripts
# (and package files) too?
		if [ "`find "$TMP/backups/scripts/" -xtype f`" = "" ] ; then
			cat <<EOF|LOG

None found.

EOF

			if [ "$TEXTONLY" -a ! "$QUIET" ] ; then
				echo
				echo "No relevant entries found in the installation script(s)."
				echo
			elif [ ! "$TEXTONLY" ] ; then
				dialog  --title "zdd" --infobox "No relevant entries found in the installation script(s)." 3 70
			fi

		else
			cp -f "$TMP/backups/scripts"/* "$TMP/.scripts/" 2>/dev/null
			cd "$TMP/.scripts"

# with a "grep -l" we could list just the files we want, but this way
# we also get to tell the user which files we are going to do NOTHING with
			IFS='
'
			for OUT in `grep -L "$DOCDIR/" * ; IFS="$OLDIFS"` ; do
				rm -f "$OUT" 2>/dev/null
				echo "Doing nothing with the \"$OUT\" script" | LOG
			done
			echo | LOG
# we should now have only script files which contain the DOCDIR regex in
# "$TMP/.scripts"
			IFS='
'
			for SCRIPT in `find -type f | cut -b3- ; IFS="$OLDIFS"` ; do
				cd "$TMP/.scripts"
				cat <<EOF|LOG

Processing "$SCRIPT" script...
EOF
				Found=0
				KLF=0
				Count=`cat "$SCRIPT" | wc -l | tr -d ' '`
				until [ $KLF = $Count ] ; do
					KLF=`expr $KLF + 1`
					LINEIS="`sed -n ${KLF}p "$SCRIPT"`"
					cat <<EOF|LOG

Processing line:
$LINEIS
EOF

					if echo "$LINEIS" | grep "$DOCDIR/" >/dev/null 2>&1 ; then # we're in business

						case "$LINEIS" in

							*"rm -rf"*) echo "$LINEIS" | sed -e 's/( //g' -e 's/ )//g' -e 's/ ; rm -rf /\
/g' -e 's/cd //g'>"$TMP/.lineis"
							JAMs=`cat "$TMP/.lineis" | wc -l | tr -d ' '`
							if [ $JAMs = 2 ] ; then # good
# LINKGOESIN does not need a leading slash
								LINKGOESIN="$(strip_search "$(sed -n 1p "$TMP/.lineis")")"
								LINKNAMEIS="`sed -n 2p "$TMP/.lineis"`"
								is_it_already_zipped "$LINKNAMEIS"
							else # bad
								do_nothing "The above line contains the \$DOCDIR regex (\"$DOCDIR\"),
AND contains the token \"rm -rf\" but I was unable to understand it."
							fi ;;

							*) do_nothing "The above line contains the \$DOCDIR regex (\"$DOCDIR\"),
but I did nothing with it (\"rm -rf\" or \"ln -sf\" tokens not found)." ;;
						esac

# we should do the "ln -sf" line in the same run because they are a pair
						KLF=`expr $KLF + 1`
						LINEIS="`sed -n ${KLF}p "$SCRIPT"`"
						cat <<EOF|LOG

Processing line:
$LINEIS
EOF
						rm -f "$TMP/.lineis" 2>/dev/null
						unset JAMs
						case "$LINEIS" in

							*"ln -sf"*) LINEWRK="$(echo "$LINEIS" | sed -e 's/ )//g' -e 's/'.'*ln -sf //g' -e 's/cd //g')"
							rockman=0

							if [ `echo $LINEWRK | wc -w | tr -d ' '` -gt 2 ] ; then
# there must be a space in name. But since this is a script we're reading from
# there should be quote marks too
								echo $LINEWRK | sed 's/" /"\
/g' >"$TMP/.lineis"
								if [ `cat "$TMP/.lineis" | wc -l | tr -d ' '` = 2 ] ; then # good
									until [ $rockman = 2 ] ; do
									  rockman=`expr $rockman + 1`
									    if [ $rockman = 1 ] ; then
									      LINKPOINTSTO="`sed -n 1p "$TMP/.lineis"`"
									    elif [ $rockman = 2 -a "`sed -n 2p "$TMP/.lineis"`" != $LINKNAMEIS ] ; then
									      echo `sed -n ''$(expr $KLF - 1)' p' "$SCRIPT"` >>"$TMP/.scripts/.$SCRIPT.new"
do_nothing "ERROR. The above line contains the \$DOCDIR regex (\"$DOCDIR\"),
AND contains the token \"ln -sf\", but it failed a consistency check."
									    fi
									done
								else # bad
									do_nothing "ERROR encountered processing the above line."
								fi
								rm -f "$TMP/.lineis" 2>/dev/null
							else # no spaces in name, just two whole words to deal with :-)
								LINKPOINTSTO=`echo "$LINEWRK" | sed 's/ .*//'`
								if [ `echo "$LINEWRK" | sed 's/.* //'` != $LINKNAMEIS ] ; then
									echo `sed -n ''$(expr $KLF - 1)' p' "$SCRIPT"` >>"$TMP/.scripts/.$SCRIPT.new"
									do_nothing "ERROR. The above line contains the \$DOCDIR regex (\"$DOCDIR\"),
AND contains the token \"ln -sf\", but it failed a consistency check."
								fi
								unset rockman
							fi ;;

							*) do_nothing "The above line contains the \$DOCDIR regex (\"$DOCDIR\"),
but NOT the token \"ln -sf\". I was unable to do anything with it." ;;
						esac

					else # DOCDIR not mentioned
						echo "$LINEIS">>"$TMP/.scripts/.$SCRIPT.new"
						continue
					fi

					make_LINKPOINTSTO_sane
# now we have sensible settings for LINKGOESIN, LINKPOINTSTO and LINKNAMEIS
# lets do something with them:
					captain_sensible
				done

				touch "$TMP/.scripts/.$SCRIPT.new"

				if [ "$PKGHEADER" ] ; then
					if [ "`diff "$SCRIPTFILE" "$TMP/.scripts/.$SCRIPT.new"`" = "" ] ; then
						cat <<EOF|LOG

Not changing script file for "$SCRIPT"
EOF
					else
						cat <<EOF|LOG

The script file for "$SCRIPT" has been modified"
EOF
						mv -f "$TMP/.scripts/.$SCRIPT.new" "$SCRIPTFILE" 2>&1 | LOG
					fi
				else
					if [ "`diff "$SCRIPTS/$SCRIPT" "$TMP/.scripts/.$SCRIPT.new"`" = "" ] ; then
						cat <<EOF|LOG

Keeping existing script for "$SCRIPT"
EOF
					else
						if [ "$WARNONLY" ] ; then
							cat <<EOF|LOG

"$SCRIPT": Storing the script which would have been created as "$TMP/scripts/$SCRIPT"
EOF
							mv -f "$TMP/.scripts/.$SCRIPT.new" "$TMP/scripts/$SCRIPT" 2>/dev/null
						else
							echo "Moving new script file for \"$SCRIPT\" to \"$SCRIPTS\":" | LOG
							mv -fv "$TMP/.scripts/.$SCRIPT.new" "$SCRIPTS/$SCRIPT" 2>&1 | LOG
						fi
					fi
				fi

				rm -f "$TMP/.lineis" 2>/dev/null
				unset KLF Count LINEIS
			done

			[ ! "$Found" -a ! "$PKGHEADER" ] && echo "OK, so that means we're doing nothing with ALL the scripts. That's cool." | LOG

			cd /
		fi


# Stage 4 - LOOK FOR LINKS WHICH AREN'T LISTED IN SCRIPTS
		if [ ! "$NOTSYMLINKS" -a ! "$ONEPACKAGEONLY" ] ; then
			cat <<EOF|LOG


Searching down the directory tree for other symbolic links...
EOF

# First, define some functions for use in stage 4:

			if /bin/ls --no-colour / >/dev/null 2>&1 ; then
				ls() {
					/bin/ls --no-colour "$@"
				}
			else
				ls() {
					/bin/ls "$@"
				}
			fi

			do_nothing() {
				echo "$1" | LOG
				continue
			}
			if [ ! "$QUICK" ] ; then
				if [ "$TEXTONLY" ] ; then
					tput clear
					read_input() {
						local INPUT
						read INPUT
						case "`echo $INPUT | tr A-Z a-z`" in

							"") echo "Null input. Try again"
							read_input ;;

							2|dele*) echo "Deleting link (user input)..." | LOG
							rm -f "$LINKGOESIN/$LINKNAMEIS" ;;

							3|mod*) echo "Input the new link destination (LINKPOINTSTO):"
							read LINKPOINTSTO
							if [ "$LINKPOINTSTO" = "" ] ; then
								echo "Null input. Skipping..."
							else
								ln -sf "$LINKPOINTSTO" "/$LINKGOESIN/$LINKNAMEIS"
								make_LINKPOINTSTO_sane
							fi ;;

							1|*nothin*) do_nothing "$1" ;;

							*) echo "Invalid input. Try again"
							read_input ;;

						esac
					}
					fix_it() {
						printf \\007
						cat <<EOF

Dangling link: "/$LINKGOESIN/$LINKNAMEIS"
Reason: $1

Please select one of the following actions:
 1      Do nothing
 2      Delete the link
 3      Modify the link

EOF
						read_input "$1"
					}
				else
					fix_it() {
						printf \\007
						dialog --title "zdd interactive" --menu "Dangling link: \"/$LINKGOESIN/$LINKNAMEIS\"
Reason: $1

Select one of the following options:" 18 80 4 \
Nothing "Do Nothing" \
Delete "Delete the link" \
Modify "Modify the link" \
Exit "Exit \`zdd\`" 2>"$TMP/reply" || { rm -f "$TMP/reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
						case "`cat "$TMP/reply" ; rm -f "$TMP/reply" 2>/dev/null`" in

							Nothing) do_nothing "$1" ;;

							Delete) echo "Deleting link (user input)..." | LOG
							rm -f "$LINKGOESIN/$LINKNAMEIS" ;;

							Modify) dialog --title "zdd interactive" --inputbox "Dangling link: \"/$LINKGOESIN/$LINKNAMEIS\"
Reason: $1

Input the new link destination (LINKPOINTSTO):" 12 80 2>"$TMP/LINKPOINTSTO" || { rm -f "$TMP/LINKPOINTSTO"; rm /tmp/.zdd_is_running ; exit; }
							LINKPOINTSTO="`cat "$TMP/LINKPOINTSTO"`"
							rm -f "$TMP/LINKPOINTSTO" 2>/dev/null
							if [ "$LINKPOINTSTO" = "" ] ; then
								echo "Null input. Skipping..."
							else
								ln -sf "$LINKPOINTSTO" "/$LINKGOESIN/$LINKNAMEIS"
								make_LINKPOINTSTO_sane
							fi ;;

							Exit) rm /tmp/.zdd_is_running
							exit 0 ;;

						esac
					}	
				fi
			fi


			cd "$TARGET_DIR"
			GREEN_DAY=0
			IFS='
'
# lets filter out names which appear to be zipped straight away (if they aren't
# zipped I guess it serves the user right for choosing inappropriate filenames)
# |-)
# we use sed to add \ marks before characters which the shell would otherwise
# interpret as wildcards (e.g. I have, for good reason, a link called '?')
			for LINK in `find -path './proc' -prune , ! -fstype iso9660 ! -fstype proc ! -fstype nfs ! -fstype smbfs ! -name *.gz ! -iname *.z ! -name *.zip ! -name *.tgz ! -name *.taz -type l -printf %p\\\n%l\\\n 2>/dev/null | sed 's/\?/\\\\?/' | sed 's/\*/\\\\*/' ; IFS="$OLDIFS"` ; do

				if [ $GREEN_DAY = 0 ] ; then
					LINK="`echo "$LINK" | cut -b3-`"
					LINKNAMEIS="$(basename "/$LINK")"
# LINKGOESIN does not need a leading slash
					LINKGOESIN="$(dirname "$LINK")"
					[ "$DEBUG" ] && if echo "$LINK" | grep -q '\\' ; then
						cat <<EOF|LOG

Processing link:
$LINK -> (line contains a wildcard and/or backslash)
EOF
					else
						cat <<EOF|LOG

Processing link:
$(ls -dl "$LINK")
EOF
					fi
					GREEN_DAY=1
				else
					GREEN_DAY=0
					if [ "$LINK" ] ; then # good
						LINKPOINTSTO="$LINK"
						make_LINKPOINTSTO_sane

						if [ "$(echo "$LINKPOINTSTO" | cut -b -$(expr length "$DOCDIR"))" != "$DOCDIR" ] ; then
							[ "$DEBUG" ] && echo "LINKPOINTSTO (\"$LINKPOINTSTO\") is not below \"$DOCDIR\"" | LOG
							continue
						fi

						was_it_zipped "$TMP/DOCS"
						case "$Reply" in

							[0-9]*) buster_gonads
							if [ "$WARNONLY" ] ; then
								echo "I would have executed the following. Writing to \"$TMP/symlinks.sh\" instead:" | LOG
							else
								echo "Executing the following:" | LOG
								( cd $LINKGOESIN ; rm -f $LINKNAMEIS 2>&1 | LOG )
								( cd $LINKGOESIN ; rm -rf $LINKNAMEIS.$NEW_SUFFIX 2>&1 | LOG )
								( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS.$NEW_SUFFIX 2>&1 | LOG )
							fi
							cat <<EOF | tee -a "$TMP/.symlinks.sh" | LOG
( cd $LINKGOESIN ; rm -f $LINKNAMEIS )
( cd $LINKGOESIN ; rm -rf $LINKNAMEIS.$NEW_SUFFIX )
( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS.$NEW_SUFFIX )
EOF
							;;

							"") was_it_zipped "$TMP/DOCS_NOT_ZIPPED"
							case "$Reply" in
								[0-9]*) cat <<EOF

The symlink "$LINKGOESIN/$LINKNAMEIS" points to "/$LINKPOINTSTO", a file which was not zipped because `sed -n ${Reply}p "$TMP/DOCS_NOT_ZIPPED.why"`.
EOF
								;;
								"") does_it_exist ;;
							esac
							continue ;;

						esac

					else # bad
						echo "Not processing link. Can't parse it." | LOG
					fi
				fi
			done

			unset GREEN_DAY

			if [ "$WARNONLY" -a `cat "$TMP/.symlinks.sh" 2>/dev/null | wc -w | tr -d ' '` -gt 0 ] ; then
				cat <<EOF >"$TMP/symlinks.sh"
#!/bin/sh
#
# Scriptfile generated by `sh "$ZDD" --version 2>&1`, by S Kennedy et al
#
# WARNING: You are strongly advised to check, and then check again, the
# contents of this script before running it. THERE IS NO WARRANTY.
#
`cat "$TMP/.symlinks.sh"`
# END
EOF
				chmod u=rwx,go= "$TMP/symlinks.sh" >/dev/null 2>&1
			fi

			rm -fr "$TMP/.scripts" "$TMP/.symlinks.sh" "$TMP/SYMLINKS" 2>/dev/null
			unset LINKGOESIN LINKPOINTSTO LINKNAMEIS
		fi
	}
fi

which_package_is_it_from_warning() {
# call with an arbitrary second parameter to get a line of dashes
	if [ "$ONEPACKAGEONLY" != 1 ] ; then
		echo "This problem affects the following package(s):"
		IFS='
'
		for MoWax in `cat "$TMP/$1" 2>/dev/null ; IFS="$OLDIFS"` ; do
			printf "\"$MoWax\" "
		done
	fi
	[ $# = 2 ] && echo "
--------------------------------------------------------------------------------"
}

# development note: if the script ever supports the checking of other package
# files for duplicates in single-package mode this ONEPACKAGEONLY part will
# not give the right answer:
which_package_is_it_from(){
	if [ "$ONEPACKAGEONLY" = 1 ] ; then
		echo "$PACKAGE_NAME"
	else
		cat "$TMP/$1" 2>/dev/null
	fi
}

lets_get_ready_to_rumble() {
	cat <<EOF|LOG

Starting zip function...

EOF
	if [ "$TEXTONLY" -a ! "$QUIET" ] ; then
		echo
		echo "OK, I've done that. Now I'm going to start zipping. Again, this may take a while."
	elif [ ! "$TEXTONLY" ] ; then
		dialog --title "zdd" --infobox "OK, I've done that. Now I'm going to start zipping. Again, this may take a while." 4 70
	fi
	IFS='
'
	local ZipThis
	for ZipThis in `cat "$1" ; IFS="$OLDIFS"` ; do
		GZIP "$ZipThis"
	done
}

# GZIP function:

# we zipped docfile - update file-list accordingly:
GZIP_4() {
	echo "$1" >>"$TMP/DOCS"
	echo "$2" >>"$TMP/DOCS_ZIPPED"
	IFS='
'
	local OUT
	for OUT in `which_package_is_it_from "$1" ; IFS="$OLDIFS"` ; do
		echo "$1.$2" >> "$TMP/file-lists/$OUT"
	done
}

# we did nothing with the docfile - update file-list accordingly:
GZIP_3() {
	IFS='
'
	for Which_Pkg in `which_package_is_it_from "$1" ; IFS="$OLDIFS"` ; do
		echo $1 >> "$TMP/file-lists/$Which_Pkg"
	done
}


if [ "$WARNONLY" ] ; then
	GZIP_7() {
		echo "I would have compressed \"$1\" and renamed it \"$1.$2\"" | LOG
		GZIP_4 "$1" $2
	}
else
	GZIP_7() {
		if [ "$DEBUG" ] ; then
			gzip -v$GZSPEED -S .$2 "$TARGET_DIR/$1" 2>&1 | LOG
		else
			gzip -$GZSPEED -S .$2 "$TARGET_DIR/$1" 2>&1 | LOG
		fi
		GZIP_4 "$1" $2
	}
fi


if [ "$QUICK" ] ; then # non-interactive

	GZIP_5() {
		echo "BUT \"$1.$2\" DOES exist. Maybe it has already been compressed? If so, you'll need to update your package files manually." | LOG
		echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
		echo "it does not exist, BUT \"$1.$2\" DOES exist." >> "$TMP/DOCS_NOT_ZIPPED.why"
	}

	GZIP_1() {
# we get here if the docfile doesn't exist
		GZIP_3 "$1"
		if [ $gz_x = 0 ] ; then
			GZIP_5 "$1" gz
			[ "z_x" = 0 ] && echo "WARNING: \"$1.z\" also exists (doing nothing)" | LOG
			[ "Z_x" = 0 ] && echo "WARNING: \"$1.Z\" also exists (doing nothing)" | LOG
		elif [ $z_x = 0 ] ; then
			GZIP_5 "$1" z
			[ "Z_x" = 0 ] && echo "WARNING: \"$1.Z\" also exists (doing nothing)" | LOG
		elif [ $Z_x = 0 ] ; then
			GZIP_5 "$1" Z
		else
			echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
			echo "it does not exist." >> "$TMP/DOCS_NOT_ZIPPED.why"
		fi
		which_package_is_it_from_warning "$1" 2 | LOG
	}

	GZIP_2() {
# we get here when the docfile $1 AND one or two zipfiles exist
		if [ $gz_x = 0 ] ; then # gz exists
			cat <<EOF|LOG
--------------------------------------------------------------------------------WARNING: $1.gz already exists
EOF
			if [ $z_x = 0 ] ; then # so does .z but not .Z
				echo "AND $1.z already exists
Writing to $1.Z instead" | LOG
				GZIP_7 "$1" Z
				which_package_is_it_from_warning "$1" 2 | LOG
			else # .gz (and maybe .Z) exists - write to .z
				[ $Z_x = 0 ] && echo "AND $1.Z already exists" | LOG
				echo "Writing to $1.z instead" | LOG
				GZIP_7 "$1" z
				which_package_is_it_from_warning "$1" 2 | LOG
			fi
		else # .gz does not exist (so .Z and/or .z do)
			echo "--------------------------------------------------------------------------------" | LOG
			[ $Z_x = 0 ] && echo "WARNING: $1.Z already exists" | LOG
			[ $z_x = 0 ] && echo "WARNING: $1.z already exists" | LOG
			GZIP_7 "$1" gz
			which_package_is_it_from_warning "$1" 2 | LOG
		fi
	}

	GZIP_8() {
# we get here when the docfile $1 AND all three zipfiles exist
		cat <<EOF|LOG
--------------------------------------------------------------------------------WARNING: $1.gz already exists
AND $1.z already exists
AND $1.Z already exists :-(
Sorry, but I won't be compressing $1. Perhaps it has already been compressed???
If so, you'll need to update your package files manually.
EOF
		which_package_is_it_from_warning "$1" 2 | LOG
		echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
		echo "$1.gz, $1.z and $.Z ALL already exist. " >>"$TMP/DOCS_NOT_ZIPPED.why"
		GZIP_3 "$1"
	}

else # interactive

	GZIP_50() {
		cat <<EOF|LOG
BUT "$1.$2" DO[ES] exist. Maybe it has already been compressed? If so, you'll need to update your package files manually.
`which_package_is_it_from_warning "$1" 2`
EOF
		echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
		echo "it does not exist, BUT \"$1.{gz,z,Z}\" do[es] exist. " >>"$TMP/DOCS_NOT_ZIPPED.why"
}

	GZIP_150() {
		cat <<EOF|LOG
"$1" exists, but so DO[ES] "$1.$2". You may need to update your package files manually.
`which_package_is_it_from_warning "$1" 2`
EOF
		echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
		echo "it exists, but so do[es] \"$1.$2\". User input said to do nothing" >>"$TMP/DOCS_NOT_ZIPPED.why"
	}

	input_10() {
# called from docfile_plus_one
		case "`echo $Zip_REPLY | tr A-Z a-z`" in
			1|*nothin*) GZIP_3 "$1"
			GZIP_150 "$1" $2
			break ;;
			2) cat "$TARGET_DIR/$1" | $PAGER ;;
			3) echo "Deleting \"$1\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1" 2>&1 | LOG
			zip_exists "$1" $2
			break ;;
			4) zcat "$TARGET_DIR/$1.$2" | $PAGER ;;
			5) echo "Deleting \"$1.$2\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.$2" 2>&1 | LOG
			GZIP_7 "$1" gz
			break ;;
			6) GZIP_4 "$1" $2
			break ;;
			7|*remove*) cat <<EOF|LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
			break ;;
			*) # we only get here in TEXTONLY mode
			echo "Invalid input. Try again." ;;
		esac
	}

	input_9(){
# called from docfile_plus_two
		case "`echo $Zip_REPLY | tr A-Z a-z`" in
			1|*nothin*) GZIP_3 "$1"
			GZIP_150 "$1" "{$2,$3}"
			break ;;
			2) cat "$TARGET_DIR/$1" | $PAGER ;;
			3) echo "Deleting \"$1\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1" 2>&1 | LOG
			two_are_zipped "$1" $2 $3
			break ;;
			4) zcat "$TARGET_DIR/$1.$2" | $PAGER ;;
			5) echo "Deleting \"$1.$2\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.$2" 2>&1 | LOG
			docfile_plus_one "$1" $3
			break ;;
			6) GZIP_4 "$1" $2
			break ;;
			7) zcat "$TARGET_DIR/$1.$3" | $PAGER ;;
			8) echo "Deleting \"$1.$3\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.$3" 2>&1 | LOG
			docfile_plus_one "$1" $2
			break ;;
			9) GZIP_4 "$1" $3
			break ;;
			10|*remove*) cat <<EOF|LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
			break ;;
			*) echo "Invalid input. Try again." ;;
		esac 
	}

	input_8(){
# called from GZIP_8
		case "`echo $Zip_REPLY | tr A-Z a-z`" in
			1|*nothin*) GZIP_3 "$1"
			GZIP_150 "$1" "{gz,z,Z}"
			break ;;
			2) cat "$TARGET_DIR/$1" | $PAGER ;;
			3) echo "Deleting \"$1\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1" 2>&1 | LOG
			all_zips_exist "$1"
			break ;;
			4) zcat "$TARGET_DIR/$1.gz" | $PAGER ;;
			5) echo "Deleting \"$1.gz\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.gz" 2>&1 | LOG
			gz_x=1
			GZIP_2 "$1"
			break ;;
			6) GZIP_4 "$1" gz
			break ;;
			7) zcat "$TARGET_DIR/$1.z" | $PAGER ;;
			8) echo "Deleting \"$1.z\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.z" 2>&1 | LOG
			z_x=1
			GZIP_2 "$1"
			break ;;
			9) GZIP_4 "$1" z
			break ;;
			10) zcat "$TARGET_DIR/$1.Z" | $PAGER ;;
			11) echo "Deleting \"$1.Z\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.Z" 2>&1 | LOG
			Z_x=1
			GZIP_2 "$1"
			break ;;
			12) GZIP_4 "$1" Z
			break ;;
			13|*remove*) cat <<EOF | LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
			break ;;
			*) echo "Invalid input. Try again." ;;
		esac
	}

	input_1(){
# called from zip_exists
		case "`echo $Zip_REPLY | tr A-Z a-z`" in
			1|*nothin*) GZIP_3 "$1"
			GZIP_50 "$1" $2
			break ;;
			2|*read*) zcat "$TARGET_DIR/$1.$2" | $PAGER ;;
			3|*delete*) echo "Deleting \"$1.$2\" (user input)..." |  LOG
			rm -f "$TARGET_DIR/$1.$2" 2>&1 | LOG
			nowt_exists "$1"
			break ;;
			4|*replace*) GZIP_4 "$1" $2
			break ;;
			5|*remove*) cat <<EOF | LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
			break ;;
			*) echo "Invalid input. Try again." ;;
		esac
	}

	input_2(){
# called from all_zips_exist
		case "$Zip_REPLY" in
			1) GZIP_3 "$1"
# GZIP_50 is a modified GZIP_5
			GZIP_50 "$1" "{gz,z,Z}"
			break ;;
			2) zcat "$TARGET_DIR/$1.gz" | $PAGER ;;
			3) echo "Deleting \"$1.gz\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.gz" 2>&1 | LOG
			two_are_zipped "$1" z Z
			break ;;
			4) GZIP_4 "$1" gz
			break ;;
			5) zcat "$TARGET_DIR/$1.z" | $PAGER ;;
			6) echo "Deleting \"$1.z\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.z" 2>&1 | LOG
			two_are_zipped "$1" gz Z
			break ;;
			7) GZIP_4 "$1" z
			break ;;
			8) zcat "$TARGET_DIR/$1.Z" | $PAGER ;;
			9) echo "Deleting \"$1.Z\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.Z" 2>&1 | LOG
			two_are_zipped "$1" gz z
			break ;;
			10) GZIP_4 "$1" Z
			break ;;
			11) cat <<EOF|LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
			break ;;
			*) echo "Invalid input. Try again." ;;
		esac
	}

	input_3(){
# called from two_are_zipped
		case "$Zip_REPLY" in
			1) GZIP_3 "$1"
			GZIP_50 "$1" "{$2,$3}"
			break ;;
			2) zcat "$TARGET_DIR/$1.$2" | $PAGER ;;
			3) echo "Deleting \"$1.$2\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.$2" 2>&1 | LOG
			zip_exists "$1" $3
			break ;;
			4) GZIP_4 "$1" $2
			break ;;
			5) zcat "$TARGET_DIR/$1.$3" | $PAGER ;;
			6) echo "Deleting \"$1.$3\" (user input)..." | LOG
			rm -f "$TARGET_DIR/$1.$3" 2>&1 | LOG
			zip_exists "$1" $2
			break ;;
			7) GZIP_4 "$1" $3
			break ;;
			8) cat <<EOF|LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
			break ;;
			*) echo "Invalid input. Try again." ;;
		esac
	}

	GZIP_2() {
# we get here if the docfile and one or two zipfiles exist
		case $(expr $gz_x + $z_x + $Z_x) in
			2) # one zipped file found
			docfile_plus_one "$1"  `[ $gz_x = 0 ] && echo gz
[ $z_x = 0 ] && echo z
[ $Z_x = 0 ] && echo Z` ;;
			1) # 2 zipped files found
			docfile_plus_two "$1"  `[ $gz_x = 0 ] && echo gz
[ $z_x = 0 ] && echo z
[ $Z_x = 0 ] && echo Z` ;;
			*) echo error
			rm /tmp/.zdd_is_running
			exit 2 ;;
		esac
	}

	GZIP_1() {
# we get here if the docfile doesn't exist
		case $(expr $gz_x + $z_x + $Z_x) in
			3) # no zipped files found either
			nowt_exists "$1" ;;
			2) # one zipped file found
			if [ $gz_x = 0 ] ; then
				zip_exists "$1" gz
			elif [ $z_x = 0 ] ; then
				zip_exists "$1" z
			elif [ $Z_x = 0 ] ; then
				zip_exists "$1" Z
			fi ;;
			0) # each of 3 suffixes found
			all_zips_exist "$1" ;;
			1) # 2 zipped files found
			two_are_zipped "$1"  `[ $gz_x = 0 ] && echo gz
[ $z_x = 0 ] && echo z
[ $Z_x = 0 ] && echo Z` ;;
			*) echo error
			rm /tmp/.zdd_is_running
			exit 2 ;;
		esac
	}

	if [ "$TEXTONLY" ] ; then # ascii-only functions

		two_are_zipped() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" does not exist, BUT "$1.$2" AND "$1.$3" *do* exist.
`which_package_is_it_from_warning "$1"`

Please select one of the following options (by number):

 1	Do nothing
 2	(Read "$1.$2")
 3	Delete "$1.$2"
 4	Replace references to "$BASENAME" with "$BASENAME.$2"
 5	(Read "$1.$3")
 6	Delete "$1.$3"
 7	Replace references to "$BASENAME" with "$BASENAME.$3"
 8	Remove .../"$BASENAME" from the package description files

Example:
Select option number 4 if "$1.$2" is in fact a gzipped copy of "$1".

EOF
				read Zip_REPLY
				input_3 "$1" $2 $3
			done
		}

		docfile_plus_two() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" exists, BUT so do "$1.$2" AND "$1.$3".
`which_package_is_it_from_warning "$1"`

Please select one of the following options (by number):

 1      Do nothing
 2      (Read "$1")
 3      Delete "$1"
 4      (Read "$1.$2")
 5      Delete "$1.$2"
 6      Replace references to "$BASENAME" with "$BASENAME.$2"
 7      (Read "$1.$3")
 8      Delete "$1.$3"
 9      Replace references to "$BASENAME" with "$BASENAME.$3"
 10     Remove .../"$BASENAME" from the package description files

EOF
				read Zip_REPLY
				input_9 "$1" $2 $3
			done
		}

		docfile_plus_one() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" exists, BUT so does "$1.$2".
`which_package_is_it_from_warning "$1"`

Please select one of the following options (by number):

 1      Do nothing
 2      (Read "$1")
 3      Delete "$1"
 4      (Read "$1.$2")
 5      Delete "$1.$2"
 6      Replace references to "$BASENAME" with "$BASENAME.$2"
 7     	Remove .../"$BASENAME" from the package description files

EOF
				read Zip_REPLY
				input_10 "$1" $2
			done
		}

		nowt_exists() {
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" does not exist.
`which_package_is_it_from_warning "$1"`

Please select one of the following options:

 1      Do nothing
 2	Remove all reference to .../"`basename "$1"`" in the package description files
EOF
				read Zip_REPLY
				case "`echo $Zip_REPLY | tr A-Z a-z`" in
					1|*nothing*) GZIP_3 "$1"
					echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "it does not exist." >> "$TMP/DOCS_NOT_ZIPPED.why"
					which_package_is_it_from_warning "$1" 2| LOG
					break ;;
					2|*remove*) cat <<EOF|LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
					break ;;
				esac
			done
		}

		zip_exists() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" does not exist, BUT "$1.$2" *does* exist.
`which_package_is_it_from_warning "$1"`

Please select one of the following options:

 1	Do nothing
 2	(Read "$1.$2")
 3	Delete "$1.$2"
 4	Replace references to "$BASENAME" with "$BASENAME.$2"
 5	Remove .../"$BASENAME" from the package description files

Select option number 4 if "$1.$2" is in fact a gzipped copy of "$1".

EOF
				read Zip_REPLY
				input_1 "$1" $2
			done
		}

		all_zips_exist() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" does not exist, BUT the following DO exist:
"$1.gz"
"$1.z"
"$1.Z"
`which_package_is_it_from_warning "$1"`

Please select one of the following options (by number):

 1	Do nothing
 2	(Read "$1.gz")
 3	Delete "$1.gz"
 4	Replace references to "$BASENAME" with "$BASENAME.gz"
 5	(Read "$1.z")
 6	Delete "$1.z"
 7	Replace references to "$BASENAME" with "$BASENAME.z"
 8	(Read "$1.Z")
 9	Delete "$1.Z"
 10	Replace references to "$BASENAME" with "$BASENAME.Z"
 11	Remove .../"$BASENAME" from the package description files

Example:
Select option number 4 if "$1.gz" is in fact a gzipped copy of "$1".

EOF
				read Zip_REPLY
				input_2 "$1"
			done
		}

		GZIP_8() {
# we get here when the docfile $1 AND all three zipfiles exist
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				tput clear
				cat <<EOF

"$1" exists, but so do "$1.gz", "$1.z", AND "$1.Z".
`which_package_is_it_from_warning "$1"`

Please select one of the following options (by number):

 1      Do nothing
 2	(Read "$1")
 3      Delete "$1"
 4      (Read "$1.gz")
 5      Delete "$1.gz"
 6      Replace references to "$BASENAME" with "$BASENAME.gz"
 7      (Read "$1.z")
 8      Delete "$1.z"
 9      Replace references to "$BASENAME" with "$BASENAME.z"
 10     (Read "$1.Z")
 11     Delete "$1.Z"
 12     Replace references to "$BASENAME" with "$BASENAME.Z"
 13     Remove .../"$BASENAME" from the package description files

EOF
				read Zip_REPLY
				input_8 "$1"
			done
		}

	else # can use dialog

		nowt_exists() {
			printf \\007
			dialog --title "zdd interactive" --menu "\"$1\" does not exist.
`which_package_is_it_from_warning "$1"`

Please select one of the following options:" 15 80 2 \
1 "Do nothing" \
2 "Remove .../\"`basename "$1"`\" from the package description files" \
			2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
			case "`cat "$TMP/zip_reply" ; rm -f "$TMP/zip_reply"`" in
				1) GZIP_3 "$1"
				echo "$1" >>"$TMP/DOCS_NOT_ZIPPED"
				echo "it does not exist." >> "$TMP/DOCS_NOT_ZIPPED.why"
				which_package_is_it_from_warning "$1" 2 | LOG ;;
				2) cat <<EOF | LOG
Removing "$1" from the package description files (user input)
--------------------------------------------------------------------------------
EOF
				;;
			esac
		}

		docfile_plus_two() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				dialog --title "zdd interactive" --menu "\"$1\" exists, BUT so do \"$1.$2\"
AND \"$1.$3\".

`which_package_is_it_from_warning "$1"`

Please select one of the following options:" 23 80 10 \
1 "Do nothing" \
2 "(Read \"$1\")" \
3 "Delete \"$1\"" \
4 "(Read \"$1.$2\")" \
5 "Delete \"$1.$2\"" \
6 "Replace references to \"$BASENAME\" with \"$BASENAME.$2\"" \
7 "(Read \"$1.$3\")" \
8 "Delete \"$1.$3\"" \
9 "Replace references to \"$BASENAME\" with \"$BASENAME.$3\"" \
10 "Remove .../\"$BASENAME\" from the package description files" \
				2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
				Zip_REPLY=`cat "$TMP/zip_reply"`
				rm -f "$TMP/zip_reply" 2>/dev/null
				input_9 "$1" $2 $3
			done
		}

		docfile_plus_one() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				dialog --title "zdd interactive" --menu "\"$1\" exists, BUT so does \"$1.$2\".
`which_package_is_it_from_warning "$1"`

Please select one of the following options:" 16 80 7 \
1 "Do nothing" \
2 "(Read \"$1\")" \
3 "Delete \"$1\"" \
4 "(Read \"$1.$2\")" \
5 "Delete \"$1.$2\"" \
6 "Replace references to \"$BASENAME\" with \"$BASENAME.$2\"" \
7 "Remove .../\"$BASENAME\" from the package description files" \
				2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
				Zip_REPLY=`cat "$TMP/zip_reply"`
				rm -f "$TMP/zip_reply" 2>/dev/null
				input_10 "$1" $2
			done
		}

		GZIP_8() {
# we get here when the docfile $1 AND all three zipfiles exist
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				dialog --title "zdd interactive" --menu "\"$1\" exists, but so do \"$1.{gz,z,Z}\".
`which_package_is_it_from_warning "$1"`

Please select one of the following options:" 25 80 13 \
1 "Do nothing" \
2 "(Read \"$1\")" \
3 "Delete \"$1\"" \
4 "(Read \"$1.gz\")" \
5 "Delete \"$1.gz\"" \
6 "Replace references to \"$BASENAME\" with \"$BASENAME.gz\"" \
7 "(Read \"$1.z\")" \
8 "Delete \"$1.z\"" \
9 "Replace references to \"$BASENAME\" with \"$BASENAME.z\"" \
10 "(Read \"$1.Z\")" \
11 "Delete \"$1.Z\"" \
12 "Replace references to \"$BASENAME\" with \"$BASENAME.Z\"" \
13 "Remove .../\"$BASENAME\" from the package description files" \
				2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
				Zip_REPLY=`cat "$TMP/zip_reply"`
				rm -f "$TMP/zip_reply" 2>/dev/null
				input_8 "$1"
			done
		}

		two_are_zipped() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				dialog --title "zdd interactive" --menu "\"$1\" does not exist,
BUT the following DO exist:
\"$1.$2\"
\"$1.$3\"

`which_package_is_it_from_warning "$1"`

Please select one of the following options:" 23 80 8 \
1 "Do nothing" \
2 "(Read \"$1.$2\")" \
3 "Delete \"$1.$2\"" \
4 "Replace references to \"$BASENAME\" with \"$BASENAME.$2\"" \
5 "(Read \"$1.$3\")" \
6 "Delete \"$1.$3\"" \
7 "Replace references to \"$BASENAME\" with \"$BASENAME.$3\"" \
8 "Remove .../\"$BASENAME\" from the package files" 2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
				Zip_REPLY=`cat "$TMP/zip_reply"`
				rm -f "$TMP/zip_reply" 2>/dev/null
				input_3 "$1" $2 $3
			done
		}

		all_zips_exist() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				dialog --title "zdd interactive" --menu "\"$1\" does not exist,
BUT all of the following DO exist:
\"$1.{gz,z,Z}\"

`which_package_is_it_from_warning "$1"`

Please select one of the following options:" 25 80 11 \
1 "Do nothing" \
2 "(Read \"$1.gz\")" \
3 "Delete \"$1.gz\"" \
4 "Replace references to \"$BASENAME\" with \"$BASENAME.gz\"" \
5 "(Read \"$1.z\")" \
6 "Delete \"$1.z\"" \
7 "Replace references to \"$BASENAME\" with \"$BASENAME.z\"" \
8 "(Read \"$1.Z\")" \
9 "Delete \"$1.Z\"" \
10 "Replace references to \"$BASENAME\" with \"$BASENAME.Z\"" \
11 "Remove .../\"$BASENAME\" from the package files" 2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
				Zip_REPLY=`cat "$TMP/zip_reply"`
				rm -f "$TMP/zip_reply" 2>/dev/null
				input_2 "$1"
			done
		}

		zip_exists() {
			local BASENAME="`basename "$1"`"
			while [ 0 ] ; do
				printf \\007
				dialog --title "zdd interactive" --menu "\"$1\" does not exist,
BUT \"$1.$2\" *does* exist.
`which_package_is_it_from_warning "$1"`

Please select one of the following options (select option number 4 if
\"$1.$2\" is in fact a gzipped copy of
"$1"):" 18 80 5 \
1 "Do nothing" \
2 "(Read \"$1.$2\")" \
3 "Delete \"$1.$2\"" \
4 "Replace references to \"$BASENAME\" with \"$BASENAME.$2\"" \
5 "Remove .../\"$BASENAME\" from the package files" 2>"$TMP/zip_reply" || { rm -f "$TMP/zip_reply" 2>/dev/null; rm /tmp/.zdd_is_running ; exit; }
				Zip_REPLY=`cat "$TMP/zip_reply"`
				rm -f "$TMP/zip_reply" 2>/dev/null
				input_1 "$1" $2
			done
		}
	fi
fi

GZIP() {
	"$TEST" -e "$TARGET_DIR/$1.gz"
	gz_x=$?
	"$TEST" -e "$TARGET_DIR/$1.z"
	z_x=$?
	"$TEST" -e "$TARGET_DIR/$1.Z"
	Z_x=$?
	"$TEST" -e "$TARGET_DIR/$1"
	if [ $? = 0 ] ; then # docfile $1 exists
		case $(expr $gz_x + $z_x + $Z_x) in
			0) # but so do all 3 zipfiles
			GZIP_8 "$1"
			please_wait ;;
			1|2) # but so do 1 or 2 zipfiles
			GZIP_2 "$1"
			please_wait ;;
			3) # best case - no zipfiles exist :-)
			GZIP_7 "$1" gz ;;
			*) echo "Error? in GZIP() function" 1>&2 ;;
		esac
	else # does not exist
		cat <<EOF|LOG
--------------------------------------------------------------------------------
WARNING: $1 does not exist
EOF
		GZIP_1 "$1"
		please_wait
	fi
}
# end of GZIP function

good_move() {
	if [ "$WARNONLY" ] ; then
		cat <<EOF|LOG

"$PACKAGE_NAME": Storing the package file which would have been created as "$TMP/packages/$PACKAGE_NAME"...
EOF
		cat "$TMP/headers/$PACKAGE_NAME" "$TMP/file-lists/$PACKAGE_NAME" >"$TMP/packages/$PACKAGE_NAME"
	else
		echo "\"$PACKAGE_NAME\": Moving new package file to \"$PACKAGES/$PACKAGE_NAME\"..." | LOG
		cat "$TMP/headers/$PACKAGE_NAME" "$TMP/file-lists/$PACKAGE_NAME" >"$PACKAGES/$PACKAGE_NAME"
	fi
}

goodbye() {
# search for links and deal with scripts:
	[ ! "$NOTSCRIPTS" ] && look_for_links

# tidy up:
	echo "
Tidying up...
" | LOG

	cd /
	[ "$DOCDIR" != "/" ] && rm -fr "$TMP/$DOCDIR" 2>/dev/null

	rm -fr "$TMP/headers" "$TMP/file-lists" "$TMP/file-lists.old" "$TMP/docs2zip" 2>/dev/null

	rmdir "$TMP/backups/scripts/" "$TMP/`dirname "$DOCDIR"`" 2>/dev/null

	echo "Finished." | LOG

	[ ! "$QUIET" ] && echo "Finished."

	if [ "$WARNONLY" ] ; then
		cat <<EOF|LOG

The log file is "$LOG", and zdd's working files are in "$TMP".

EOF
	else
		cat <<EOF|LOG

Your disk usage is now (`date`):
`df`

There are also backup files in "$TMP" and the logfile "$LOG" which you can delete, when you are SURE you no longer need them, to free up another `du -kcs "$TMP" "$LOG" 2>/dev/null|tail -n1|cut -f 1`k.
EOF
	fi

# adios amigos
	rm -f "$TMP/DOCS_ZIPPED" "$TMP/DOCS_NOT_ZIPPED" "$TMP/DOCS" "$TMP/DOCS_NOT_ZIPPED.why" "$TMP/.we are running" "$TMP/logttydev" "$TMP/temp.sh" 2>/dev/null
# if the directory is not empty this will just send an error to /dev/null:
	rmdir "$TMP/backups/scripts" 2>/dev/null
	rm /tmp/.zdd_is_running
	exit 0
}

job_centre() {
# Stage 1

	if [ "$DEBUG" ] ; then
		cat <<EOF|LOG

"$PACKAGE_NAME"
creating a backup package file...
EOF
		cp -fv "$PACKAGES/$PACKAGE_NAME" "$TMP/backups/packages/$PACKAGE_NAME" 2>&1 | LOG
	else
		cp -f "$PACKAGES/$PACKAGE_NAME" "$TMP/backups/packages/$PACKAGE_NAME" 2>&1 | LOG
	fi
	PACKAGEFILE="$TMP/backups/packages/$PACKAGE_NAME"

	if "$TEST" -e "$SCRIPTS/$PACKAGE_NAME" ; then
		if [ "$DEBUG" ] ; then
			echo "creating a backup installation script..." | LOG
			cp -fv "$SCRIPTS/$PACKAGE_NAME" "$TMP/backups/scripts/$PACKAGE_NAME" 2>&1 | LOG
		else
			cp -f "$SCRIPTS/$PACKAGE_NAME" "$TMP/backups/scripts/$PACKAGE_NAME" 2>&1 | LOG
		fi
	fi

# taken, with changes, from pkgtool v2.2.0 by P Volkerding, see copyright notice# above:
	if fgrep "./" "$PACKAGEFILE" > /dev/null 2>&1 ; then
# as borrowed this didn't work properly:
		TRIGGER="\.\/"
	else
		TRIGGER="FILE LIST:"
	fi
# end of pkgtool borrowing

# "off with his header"
	csplit -kn1 -qf "$TMP/csplit$$." "$PACKAGEFILE" /"$TRIGGER"/+1 >/dev/null 2>&1
# so that `mv` and `sort` never complain about non-existant files:
	touch "$TMP/csplit$$.0" "$TMP/csplit$$.1"
	mv -f "$TMP/csplit$$.0" "$TMP/headers/$PACKAGE_NAME" >/dev/null 2>&1
	mv -f "$TMP/csplit$$.1" "$TMP/file-lists.old/$PACKAGE_NAME" >/dev/null 2>&1
# we now have the package header in "$TMP/headers/$PACKAGE_NAME"
# and the list of files (less any leading slashes) in
# "$TMP/file-lists.old/$PACKAGE_NAME"

	make_a_grep_for_it
}

flipping() {
	if [ "$PKGHEADER" ] ; then
# TODO: make it so that each pkgheader run just produces one log and one set of
# working files? (pkgheader can pass an extra argument, being a $#)
		cat <<EOF
You can watch what's going on by flipping to another virtual
console (use ALT plus one of the function keys) or xterm and
typing 'tail -f "$LOG"'.
EOF
	else
	cat <<EOF
The log output of zdd is being sent to
$TTY (terminal), and 
"$LOG" (logfile).
EOF
	fi
}


mkdir -p "$TMP/.scripts" "$TMP/backups/packages" "$TMP/backups/scripts" "$TMP/docs2zip" "$TMP/file-lists" "$TMP/file-lists.old" "$TMP/headers" 2>&1 | LOG

if [ "$ONEPACKAGEONLY" ] ; then
# named-packages-only mode (including pkgheader)

	processing() {
		if [ "$TEXTONLY" -a ! "$QUIET" ] ; then
			cat <<EOF


zdd by Stephen Kennedy, with extracts from pkgtool by Patrick Volkerding
and tbackup by Koen Holtman.

Hang on while I look at the package contents file for "$PACKAGE_NAME" and process a few commands.

`flipping`
EOF
		elif [ ! "$TEXTONLY" ] ; then
			dialog --title "zdd" --infobox "Hang on while I look at the package contents file
for \"$PACKAGE_NAME\" and process a few commands.

`flipping`" 8 70
		fi
	}

	if [ "$PKGHEADER" ] ; then # pkgheader-mode
		processing
		cat <<EOF|LOG

"$PACKAGE_NAME"
creating a backup package file in "$TMP/backups/packages/$PACKAGE_NAME"...
EOF
		[ ! -r "$HEADER_FILE" ] && bug_report "\"$HEADER_FILE\" unreadable/non-existant"
		[ ! -r "$FILE_LIST" ] && bug_report "\"$FILE_LIST\" unreadable/non-existant"
		if [ "$SCRIPTFILE" ] ; then
			[ ! -r "$SCRIPTFILE" ] && bug_report "\"$SCRIPTFILE\" unreadable/non-existant"
			echo "creating a backup installation script..." | LOG
			cp -fv "$SCRIPTFILE" "$TMP/backups/scripts/$PACKAGE_NAME" | LOG
		fi
		unset -f bug_report
		cat "$HEADER_FILE" "$FILE_LIST" >"$TMP/backups/packages/$PACKAGE_NAME"
		mv "$HEADER_FILE" "$TMP/headers/$PACKAGE_NAME" 2>&1 | LOG
		mv "$FILE_LIST" "$TMP/file-lists.old/$PACKAGE_NAME" 2>&1 | LOG
		make_a_grep_for_it
# PKGHEADER mode does not support WARNONLY
		if [ -s "$TMP/docs2zip/$PACKAGE_NAME" ] ; then
			lets_get_ready_to_rumble "$TMP/docs2zip/$PACKAGE_NAME"
			cat "$TMP/headers/$PACKAGE_NAME" "$TMP/file-lists/$PACKAGE_NAME" >"$OLDTMP/pkg.$PACKAGE_NAME"
		else
			cat <<EOF|LOG

Nothing found. Keeping existing package file.
EOF
# just keep old package file
			cp "$TMP/backups/packages/$PACKAGE_NAME" "$OLDTMP/pkg.$PACKAGE_NAME"
		fi
# deal with scripts etc:
		goodbye

	else # named-packages-only mode

		while [ "$1" ] ; do
			PACKAGE_NAME="$1"
			shift
			if [ ! -s "$PACKAGES/$PACKAGE_NAME" ] ; then
				if [ "$TEXTONLY" -a ! "$QUIET" ] ; then
					cat <<EOF
zdd by Stephen Kennedy, with extracts from pkgtool by Patrick Volkerding
and tbackup by Koen Holtman.
There does not appear to be a package file for "$PACKAGE_NAME" in "$PACKAGES".
EOF
				elif [ ! "$TEXTONLY" ] ; then
					dialog --title "zdd" --infobox "There does not appear to be a package file for \"$PACKAGE_NAME\" in \"$PACKAGES\"." 6 70
				fi
				cat <<EOF|LOG

There does not appear to be a package file for "$PACKAGE_NAME" in
"$PACKAGES".
EOF
				rm -fr "$TMP" 2>/dev/null
				rm /tmp/.zdd_is_running
				exit 1
			fi
			processing
			job_centre
			if [ -s "$TMP/docs2zip/$PACKAGE_NAME" ] ; then
				lets_get_ready_to_rumble "$TMP/docs2zip/$PACKAGE_NAME"
				good_move
			else
				cat <<EOF|LOG

Nothing found. Keeping existing package file.
EOF
# just keep old package file
			fi
		done
	fi

else # all-packages mode

# bogomips from pkgtool 2.2.0, see above
	if [ "$TEXTONLY" -a ! "$QUIET" ] ; then
		cat <<EOF
zdd by Stephen Kennedy, with extracts from pkgtool by Patrick Volkerding
and tbackup by Koen Holtman.

I am building a list of the packages you have installed AND any document files they contain which are in "$DOCDIR".

This may take some time [100.`date +"%S"`E+`date +"%M"` BogoMipMinutes to be "precise" :-)].

`flipping`
EOF
	elif [ ! "$TEXTONLY" ] ; then
		dialog --title "zdd" --infobox "I am building a list of the packages you have installed AND any document
files they contain which are in \"$DOCDIR\".

This may take some time [10.`date +"%S"`E+`date +"%M"` BogoMipMinutes to be \"precise\" :-)].

`flipping`" 10 80
	fi

	cd "$PACKAGES"
	IFS='
'
	for PACKAGE_NAME in `find -maxdepth 1 -xtype f | cut -b 3- ; IFS="$OLDIFS"` ; do
		[ ! "$TEXTONLY" ] && \
		dialog --title "zdd" --infobox "Processing \"$PACKAGE_NAME\".

`flipping`" 8 70
		(job_centre)
	done
	cd "$TMP/docs2zip"
	cat * | sort -u - >../DOCS2ZIP.$$

	lets_get_ready_to_rumble "$TMP/DOCS2ZIP.$$"

	cd "$TMP/headers"
	{ echo ; echo; } | LOG
	IFS='
'
	for PACKAGE_NAME in `find -maxdepth 1 -xtype f | cut -b 3- ; IFS="$OLDIFS"` ; do
		good_move
	done

	if [ ! "$JUSTPACKAGES" ] ; then
# Stage 2
		cat <<EOF|LOG

Now zipping the files (if any) below "$DOCDIR" which are not listed in any package files...

EOF
		cd "$TARGET_DIR"

		IFS='
'
		for OUT in `find "$DOCDIR/" -type f ! -iname '*.gz' ! -iname '*.Z' ! -iname '*.z' ! -iname '*.tgz' ! -iname '*.taz' ! -iname '*.zip' ! -fstype iso9660 ; IFS="$OLDIFS"` ; do

			NONE=1

# make sure don't "compress" any files already dealt with: 
			if [ `grep -xq "$OUT" "$TMP/DOCS_NOT_ZIPPED" 2>/dev/null ; echo $?` != 0 ] && [ `grep -xq "$OUT" "$TMP/DOCS" 2>/dev/null ; echo $?` != 0 ] ; then

				if [ "$FILTER" ] && echo $OUT | grep -Eqx $FILTER ; then
					[ "$DEBUG" ] && echo "Skipping \"$OUT\" - matches \"-filter\" regex" | LOG
					echo "$OUT" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "Matches \"-filter\" regex" >>"$TMP/DOCS_NOT_ZIPPED.why"
					continue
# else subject it to the other checks
				fi
				if [ "$OFILTER" ] && echo $OUT | grep -Eqxv $FILTER ; then
					[ "$DEBUG" ] && echo "Skipping \"$OUT\" -  - does not match --only regex" | LOG
					echo "$OUT" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "Does not match \"--only\" regex" >>"$TMP/DOCS_NOT_ZIPPED.why"
					continue
				fi
				if "$TEST" -e "$OUT.gz" ; then
					cat <<EOF|LOG

Not compressing "$OUT" - "$OUT.gz" already exists
EOF
					echo "$OUT" >>"$TMP/DOCS_NOT_ZIPPED"
					echo "gz file already exists" >>"$TMP/DOCS_NOT_ZIPPED.why"
					[ ! "$WARNONLY" ] && echo "$OUT" >>"$TMP/docfiles found by zdd" # we'll just forget OUT.gz (we've told user about it)

				else
					if [ "$WARNONLY" ] ; then
						echo "I would have zipped \"$OUT\"" | LOG
					else
						if [ "$DEBUG" ] ; then
							gzip -v$GZSPEED "$OUT" 2>&1 | LOG
						else
							gzip -$GZSPEED "$OUT" 2>&1 | LOG
						fi
						echo "$OUT.gz" >>"$TMP/docfiles found by zdd"
					fi
					echo "$OUT" >>"$TMP/DOCS"
					echo "gz" >>"$TMP/DOCS_ZIPPED"
				fi
			fi
		done
	fi
fi

if [ ! "$NONE" ] ; then
	echo "Well, as it happens I didn't find any...

" | LOG
elif [ ! "$WARNONLY" -a -s "$TMP/docfiles found by zdd" ] ; then
	if pkgheader --version 2>/dev/null ; then
		export PACKAGES SCRIPTS TMP ADM_DIR
		{ echo; echo "Now calling up 'pkgheader' from SDK Software to make a package file for the \"stray\" docfiles:"; } | LOG
		pkgheader -td "$TMP" -D "Created by `sh "$ZDD" --version 2>&1` on `date`.\\nThis is a list of files found below \"$DOCDIR\"\\nby the zdd program and which are not listed\\nin any other package file. DO NOT DELETE THIS\\n\"PACKAGE\" (using pkgtool, for example) UNLESS YOU\\nARE _SURE_ YOU KNOW WHAT YOU ARE DOING\\041\\041\\nYou may delete THIS file,\\n\"$PACKAGES/docfiles found by zdd\",\\nhowever." "docfiles found by zdd" 2>&1 | LOG
		rm -f "$TMP/docfiles found by zdd" 2>/dev/null
	else
		echo "Pkgheader program not found" | LOG
	fi
fi
##

goodbye
