#!/bin/sh
#
# Copyright 1993, 1994, 1995, 1996, 1997,
#    1998, 1999  Patrick Volkerding,  Moorhead, MN  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.
#
#  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.
#

# Wed, 27 Apr 1994 00:06:50 -0700 (PDT)
# Optimization by David Hinds.

SOURCE_DIR=/var/log/mount
umask 000
ASK="tagfiles"
if [ ! -d /usr/sbin ]; then # we must be on the bootdisk
 TARGET_DIR=/mnt
 TMP=/mnt/var/log/setup/tmp
 if mount | fgrep "on /mnt" 1> /dev/null 2>&1 ; then # good
  echo > /dev/null
 else # bad
  echo
  echo
  echo "You can't run pkgtool from the rootdisk until you've mounted your Linux"
  echo "partitions beneath /mnt. Here are some examples of this:"
  echo
  echo "If your root partition is /dev/hda1, and is using ext2fs, you would type:"
  echo "mount /dev/hda1 /mnt -t ext2"
  echo
  echo "Then, supposing your /usr partition is /dev/hda2, you must do this:"
  echo "mount /dev/hda2 /mnt/usr -t ext2"
  echo
  echo "Please mount your Linux partitions and then run pkgtool again."
  echo
  exit
 fi
else
 TARGET_DIR=/
 TMP=/var/log/setup/tmp
fi
if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
 fi
ADM_DIR=$TARGET_DIR/var/log
LOG=$TMP/PKGTOOL.REMOVED

keep_files() {
 while read FILE ; do
  if [ -f "$TARGET_DIR/$FILE" ]; then
    echo "  --> $FILE was found in another package. Skipping." >> $LOG
  fi
 done
 }

keep_links() {
 while read LINK ; do
  echo "Duplicate link. Not executing: $LINK" >> $LOG
 done
}

delete_files() {
 while read FILE ; do
  if [ -f "$TARGET_DIR/$FILE" ]; then
    echo "  --> Deleting $FILE" >> $LOG
    rm -f $TARGET_DIR/$FILE
  fi
 done
 }

delete_links() {
 while read LINK ; do
  echo "Unique link. Executing: $LINK" >> $LOG
 done
}

# Conversion to 'comm' utility by Mark Wisdom.
remove_packages() {
 for package_name in $* 
 do
  if [ -r $ADM_DIR/packages/$package_name ]; then
   dialog --title "PACKAGE REMOVAL IN PROGRESS" --infobox "Removing \
package $package_name. Since each file must be checked \
against the contents of every other installed package to avoid wiping out \
areas of overlap, this process can take quite some time. If you'd like to \
watch the progress, flip over to another virtual console and type 'tail -f \
$TMP/PKGTOOL.REMOVED'." 9 60
   echo "Removing package $package_name:" >> $LOG
   if fgrep "./" $ADM_DIR/packages/$package_name 1> /dev/null 2>&1; then
    TRIGGER=".\/"
   else
    TRIGGER="FILE LIST:"
   fi
   echo "Removing files:" >> $LOG
   sed -n "/$TRIGGER/,/^$/p" < $ADM_DIR/packages/$package_name | sort -u > $TMP/delete_list
   # Pat's new-new && improved pre-removal routine.
   for DIR in $ADM_DIR/removed_packages $ADM_DIR/removed_scripts ; do
    if [ ! -d $DIR ] ; then mkdir -p $DIR ; chmod 755 $DIR ; fi
   done
   mv $ADM_DIR/packages/$package_name $ADM_DIR/removed_packages 1> /dev/null 2>&1
   # Look for duplicated links and leave them in place.
   if [ -r $ADM_DIR/scripts/$package_name ]; then
    cat $ADM_DIR/scripts/$package_name | fgrep 'rm -rf' | sort -u > $TMP/del_link_list
    mv $ADM_DIR/scripts/$package_name $ADM_DIR/removed_scripts 1> /dev/null 2>&1
    cat $ADM_DIR/scripts/* | fgrep 'rm -rf' | sort -u > $TMP/required_links
    comm -12 $TMP/del_link_list $TMP/required_links | keep_links
    comm -23 $TMP/del_link_list $TMP/required_links | delete_links
    comm -23 $TMP/del_link_list $TMP/required_links > $TMP/delscript
    ( cd $TARGET_DIR ; sh $TMP/delscript )
    rm -f $TMP/del_link_list $TMP/required_links $TMP/delscript
   fi
   cat $ADM_DIR/packages/* | sort -u > $TMP/required_files
   comm -12 $TMP/delete_list $TMP/required_files | keep_files
   comm -23 $TMP/delete_list $TMP/required_files | delete_files
   rm -f $TMP/delete_list
   rm -f $TMP/required_files
  else
   echo "No such package: $package_name. Can't remove." >> $LOG
  fi
 done
}

# Here, we read the list of arguments passed to the pkgtool script.
if [ $# -gt 0 ]; then # there are arguments to the command
 while [ $# -gt 0 ]; do
  case "$1" in
  "-sets")
   DISK_SETS=`echo $2 | tr "[A-Z]" "[a-z]"` ; shift 2 ;;
  "-source_mounted")
   SOURCE_MOUNTED="always" ; shift 1 ;;
  "-ignore_tagfiles")
   ASK="never" ; shift 1 ;;
  "-tagfile")
   USETAG=$2 ; shift 2 ;;
  "-source_dir")
   SOURCE_DIR=$2 ; shift 2 ;;
  "-target_dir")
   TARGET_DIR=$2
   ADM_DIR=$TARGET_DIR/var/log
   shift 2 ;;
  "-source_device")
   SOURCE_DEVICE=$2 ; shift 2 ;;
  esac
 done
else  # there were no arguments, so we'll get the needed information from the
      # user and then go on.
 CMD_START="true"
 rm -f $TMP/SeT*
 while [ 0 ]; do
  dialog --title "Slackware Package Tool (pkgtool version 7.1.0)" \
--menu "\nWelcome to the Slackware package tool.\n\
\nWhich option would you like?\n" 17 74 6 \
"Current" "Install packages from the current directory" \
"Other" "Install packages from some other directory" \
"Floppy" "Install packages from floppy disks" \
"Remove" "Remove packages that are currently installed" \
"View" "View the list of files contained in a package" \
"Exit" "Exit Pkgtool" 2> $TMP/reply
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/reply
   reset
   exit
  fi
  REPLY="`cat $TMP/reply`"
  rm -f $TMP/reply
  if [ "$REPLY" = "Exit" ]; then
   reset
   exit
  fi
  if [ "$REPLY" = "View" ]; then
   dialog --title "SCANNING" --infobox "Please wait while \
Pkgtool scans your system to determine which packages you have \
installed and prepares a list for you. This will take \
1.`date +"%S"`E+`date +"%M"` BogoMipSeconds." 7 40
   echo 'dialog --menu "Please select the package you wish to view." 15 55 8 \' > $TMP/viewscr
   ls $ADM_DIR/packages | sed -e 's/.*/"&" "" \\/' >> $TMP/viewscr
   echo "2> $TMP/return" >> $TMP/viewscr
   while [ 0 ]; do
    . $TMP/viewscr
    if [ ! "`cat $TMP/return`" = "" ]; then
     dialog --title "CONTENTS OF PACKAGE: `cat $TMP/return`" --textbox "$ADM_DIR/packages/`cat $TMP/return`" \
     22 74 2> /dev/null
    else
     break 
    fi
   done
   rm -f $TMP/return $TMP/viewscr $TMP/tmpmsg
   # This will clean up after most defective packages:
   chmod 755 /
   chmod 1777 /tmp
   continue
  fi  
  if [ "$REPLY" = "Remove" ]; then
   dialog --title "SCANNING" --infobox "Please wait while Pkgtool scans \
your system to determine  which packages you have installed and prepares \
a list for you. This will take 3.`date +"%S"`E+`date +"%M"` \
BogoMipSeconds." 7 40
   # next section added by Ellen Geertsema, 17-aug-1999
   # patched by volkerdi@slackware.com 22-aug-1999 to handle no database yet
   # calculate length of longest filename, initially set to 8 characters
   LONGEST=8
   BASELENGTH=72
   # comment this structure out on rootdisks:
   if [ -d $ADM_DIR/packages ]; then
     for name in `ls $ADM_DIR/packages` ; do
       LENGTH=${#name}
       if [ $LENGTH -gt $LONGEST ]; then
         LONGEST=$LENGTH
       fi
     done 
   fi
   CUT=$[ $BASELENGTH - $LONGEST ]
   # end section
   cat << EOF > $TMP/rmscript
dialog --title "SELECT PACKAGES TO REMOVE" --checklist "Please select the \
packages you wish to Remove. Use the \
spacebar to select packages to delete, and the UP/DOWN arrow keys to \
scroll up and down through the entire list." 22 75 13 \\
EOF
   for name in `ls $ADM_DIR/packages` ; do
    BLURB="`sed -n \"/$name:/{s/\\"//g;p;q;}\" $ADM_DIR/packages/$name | cut -b10-$CUT`"
    echo " \"$name\" \"$BLURB\" off \\" >> $TMP/rmscript
   done 
   echo "2> $TMP/return" >> $TMP/rmscript
   if [ -L $LOG -o -r $LOG ]; then
     rm -f $LOG
   fi
   cat /dev/null > $LOG
   chmod 600 $LOG
   chmod 700 $TMP/rmscript
   export ADM_DIR;
   $TMP/rmscript
   remove_packages `cat $TMP/return | tr -d "\042"`
   if [ "`cat $TMP/PKGTOOL.REMOVED`" = "" ]; then
    rm -f $TMP/PKGTOOL.REMOVED
   else
    dialog --title "PACKAGE REMOVAL COMPLETE" --msgbox "The packages have been \
removed. A complete log of the files that were removed has been created \
in $TMP: PKGTOOL.REMOVED. Pkgtool does not remove empty directories, so you may \
want to do that yourself." 8 63
   fi
   rm -f $TMP/rmscript $TMP/return $TMP/tmpmsg $TMP/SeT*
   chmod 755 /
   chmod 1777 /tmp
   dialog --clear
   exit
  elif [ "$REPLY" = "Floppy" ]; then
   dialog --title "SELECT FLOPPY DRIVE" --menu "Which floppy drive would \
you like to install from?" \
11 70 4 \
"/dev/fd0u1440" "1.44 MB first floppy drive" \
"/dev/fd1u1440" "1.44 MB second floppy drive" \
"/dev/fd0h1200" "1.2 MB first floppy drive" \
"/dev/fd1h1200" "1.2 MB second floppy drive" 2> $TMP/wdrive
   if [ $? = 1 ]; then
    dialog --clear
    exit
   fi
   SOURCE_DEVICE="`cat $TMP/wdrive`"
   rm -f $TMP/wdrive 
   cat << EOF > $TMP/tmpmsg

Enter the names of any disk sets you would like to install.
Seperate the sets with a space, like this: a b oi x

To install packages from one disk, hit [enter] without typing
anything.

EOF
   dialog --title "SOFTWARE SELECTION" --inputbox "`cat $TMP/tmpmsg`" 13 70 2> $TMP/sets 
   DISK_SETS="`cat $TMP/sets`"
   rm -f $TMP/sets
   if [ "$DISK_SETS" = "" ]; then
    DISK_SETS="disk"
   else
    DISK_SETS=`echo $DISK_SETS | sed 's/ /#/g'`
    DISK_SETS="#$DISK_SETS"
   fi
   break;
  elif [ "$REPLY" = "Other" ]; then
   dialog --title "SELECT SOURCE DIRECTORY" --inputbox "Please enter the name of the directory that you wish to \
install packages from:" 10 50 2> $TMP/pkgdir
   if [ $? = 1 ]; then
    rm -f $TMP/pkgdir $TMP/SeT*
    reset
    exit
   fi 
   SOURCE_DIR="`cat $TMP/pkgdir`"
   SOURCE_MOUNTED="always"
   DISK_SETS="disk" 
   chmod 755 $TARGET_DIR
   chmod 1777 $TARGET_DIR/tmp
   rm -f $TMP/pkgdir
   if [ ! -d $SOURCE_DIR ]; then
    dialog --title "DIRECTORY NOT FOUND" --msgbox "The directory you want to \
install from ($SOURCE_DIR) \
does not seem to exist. Please check the directory and then try again." \
10 50
    reset
    exit
   fi
   break;
  else # installing from current directory
   SOURCE_MOUNTED="always"
   SOURCE_DIR="$PWD"
   DISK_SETS="disk" 
   chmod 755 $TARGET_DIR
   chmod 1777 $TARGET_DIR/tmp
   break;
  fi 
 done
fi
if [ "$DISK_SETS" = "disk" ]; then
 ASK="always"
fi

for DIR in $ADM_DIR $ADM_DIR/packages $ADM_DIR/scripts $ADM_DIR/disk_contents
do
 if [ ! -d $DIR ]; then mkdir -p $DIR ; chmod 755 $DIR ; fi
done

if [ ! -d $ADM_DIR/mount -a ! -L $ADM_DIR/mount ]; then
 mkdir -p $ADM_DIR/mount ; chmod 755 $ADM_DIR/mount
fi

mount_the_source() {
 # is the source supposed to be mounted already?
 if [ "$SOURCE_MOUNTED" = "always" ]; then
  # The source should already be mounted, so we test it
  if [ ! -d $SOURCE_DIR ]; then # the directory is missing
   cat << EOF > $TMP/tmpmsg

Your source device cannot be accessed properly.

Please be sure that it is mounted on $SOURCE_DIR,
and that the Slackware disks are found in subdirectories 
of $SOURCE_DIR like specified.

EOF
   dialog --title "MOUNT ERROR" --msgbox "`cat $TMP/tmpmsg`" 11 67
   rm -f $TMP/tmpmsg
   exit 1;
  fi
  return 0;
 fi
 dialog --title "INSERT DISK" --menu "Please insert disk $1 and \
press ENTER to continue." \
11 50 3 \
"Continue" "Continue with the installation" \
"Skip" "Skip the current disk series" \
"Quit" "Abort the installation process" 2> $TMP/reply
 if [ $? = 1 -o $? = 255 ]; then
  REPLY="Quit"
 else
  REPLY="`cat $TMP/reply`"
 fi
 rm -f $TMP/reply
 if [ "$REPLY" = "Skip" ]; then
  return 1;
 fi
 if [ "$REPLY" = "Quit" ]; then
   dialog --title "ABORTING" --msgbox "Aborting software installation." 5 50
   chmod 755 $TARGET_DIR
   chmod 1777 $TARGET_DIR/tmp
   exit 1;
 fi;
 # Old line:
 # mount -r -t msdos $SOURCE_DEVICE $SOURCE_DIR
 # New ones: (thanks to Andy Schwierskott!)
 go_on=y
 not_successfull_mounted=1
 while [ "$go_on" = y -a "$not_successfull_mounted" = 1 ]; do
  mount -r -t msdos $SOURCE_DEVICE $SOURCE_DIR
  not_successfull_mounted=$?
  if [ "$not_successfull_mounted" = 1 ]; then
   mount_answer=x
   while [ "$mount_answer" != "y" -a "$mount_answer" != "q" ] ; do
    dialog --title "MOUNT PROBLEM" --menu "Media was not successfully \
mounted! Do you want to \
retry, or quit?" 10 60 2 \
"Yes" "Try to mount the disk again" \
"No" "No, abort." 2> $TMP/mntans
    mount_answer="`cat $TMP/mntans`"
    rm -f $TMP/mntans
    if [ "$mount_answer" = "Yes" ]; then
     mount_answer="y"
    else
     mount_answer="q"
    fi
   done
   go_on=$mount_answer
  fi
 done
 test $not_successfull_mounted = 0
}
umount_the_source() {
 if [ ! "$SOURCE_MOUNTED" = "always" ]; then
  umount $SOURCE_DEVICE 1> /dev/null 2>&1
 fi;
}
# The function below installs the package with the name $CURRENT_PACKAGE_NAME
# and with the DOS file extension .tgz
install_the_current_package() {
 rm -f $ADM_DIR/removed_packages/$CURRENT_PACKAGE_NAME
 rm -f $ADM_DIR/removed_scripts/$CURRENT_PACKAGE_NAME
 echo "PACKAGE NAME:     $CURRENT_PACKAGE_NAME" > $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
 echo "COMPRESSED PACKAGE SIZE:     $COMPRESSED" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
 echo "UNCOMPRESSED PACKAGE SIZE:     $UNCOMPRESSED" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
 BASE_DISK_NAME=`basename $PACKAGE_DIR/disk*`
 echo "PACKAGE LOCATION: $BASE_DISK_NAME" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
# echo "PACKAGE DESCRIPTION:" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
# if [ -r $PACKAGE_DIR/$BASE_DISK_NAME -a ! -d $PACKAGE_DIR/$BASE_DISK_NAME ]; then
#  fgrep "$CURRENT_PACKAGE_NAME:" $PACKAGE_DIR/$BASE_DISK_NAME | uniq >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
# fi
 for index_file in $PACKAGE_DIR/disk* $PACKAGE_DIR/package_descriptions ; do
  if [ ! "$index_file" = "$PACKAGE_DIR/disk??*" ]; then
   if [ -r "$index_file" ]; then
    echo "PACKAGE DESCRIPTION:" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
    if cat $index_file | uniq | fgrep "$CURRENT_PACKAGE_NAME:" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME 2> /dev/null ; then
     break;
    fi
   fi
  fi
 done
 echo "FILE LIST:" >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
 # Pat's new-new pre-install cleanup routine.
 if [ -r $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME -a ! -d $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME ]; then
  cat $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME | fgrep 'rm -rf' | sort -u > $TMP/del_link_list
  if [ ! -d $ADM_DIR/removed_scripts ]; then
   mkdir $ADM_DIR/removed_scripts
  fi
  mv $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME $ADM_DIR/removed_scripts 1> /dev/null 2>&1
  ( cat $ADM_DIR/scripts/* ; \
    tar xzOf $PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz install/doinst.sh ) | \
    fgrep 'rm -rf' | sort -u > $TMP/required_links
  comm -12 $TMP/del_link_list $TMP/required_links | keep_links
  comm -23 $TMP/del_link_list $TMP/required_links | delete_links
  comm -23 $TMP/del_link_list $TMP/required_links > $TMP/delscript
  ( cd $TARGET_DIR ; sh $TMP/delscript )
  rm -f $TMP/del_link_list $TMP/required_links $TMP/delscript $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME
 fi
 # Install the package:
 >$TMP/tar-error
 (cd $TARGET_DIR; tar -xzlpvf - ) < $PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz >> $ADM_DIR/packages/$CURRENT_PACKAGE_NAME 2> $TMP/tar-error
 if [ -x /sbin/ldconfig ]; then
   /sbin/ldconfig
 fi
 if [ ! "`filesize $TMP/tar-error`" = "0" ]; then # Package may be corrupt
  dialog --title "Error installing package \
$PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz" \
   --msgbox "\n\
An error has occured while installing this package.  Setup\n\
will attempt to continue anyway, but this is possibly a \n\
SERIOUS ERROR that may cause you problems later on.  It is\n\
recommended that you make sure the package is not corrupted\n\
and then reinstall.  If you obtained the package via FTP, be\n\
sure to use 'binary' mode for both the transfer and download\n\
of the package.\n\
\n\
Here is the actual error message (if any):\n\
`cat $TMP/tar-error` \n" 19 67
 fi
 chmod 644 $ADM_DIR/packages/$CURRENT_PACKAGE_NAME
 if [ -f $TARGET_DIR/install/doinst.sh ]; then
  # Executing installation script for package $CURRENT_PACKAGE_NAME... 
  (cd $TARGET_DIR; sh $TARGET_DIR/install/doinst.sh -install; )
  cp $TARGET_DIR/install/doinst.sh $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME
  chmod 755 $ADM_DIR/scripts/$CURRENT_PACKAGE_NAME
  # Clean up the mess...
  if [ -d $TARGET_DIR/install ]; then
   (cd $TARGET_DIR/install ; rm -r -f doin* 1> /dev/null 2>&1 )
   rmdir $TARGET_DIR/install 1> /dev/null 2>&1
  fi
 fi
 # Now we reload the shell hash table in case we've added something useful
 # to the command path:
 hash -r
 # Done installing package $CURRENT_PACKAGE_NAME.
 if [ ! "$DEBUG" = "" ]; then
  echo
  echo "Press enter to continue..."
  read JUNKVAL;
 fi
}
install_disk() {
 mount_the_source $1
 if [ $? = 1 ]; then
  umount_the_source;
  return 1;
 fi
 CURRENT_DISK_NAME="$1"
 PACKAGE_DIR=$SOURCE_DIR
 if [ "$SOURCE_MOUNTED" = "always" -a ! "$DISK_SETS" = "disk" ]; then
   PACKAGE_DIR=$PACKAGE_DIR/$1
 fi

 #
 # look for tagfile for this series and copy into $TMP/tagfile
 #
 touch $TMP/tagfile
 if [ ! "$DISK_SETS" = "disk" ]; then
  if [ -r $TMP/SeTtagext ]; then
   if [ -r $PACKAGE_DIR/tagfile`cat $TMP/SeTtagext` ]; then
    cat $PACKAGE_DIR/tagfile`cat $TMP/SeTtagext` >> $TMP/tagfile
   else
    if [ -r $PACKAGE_DIR/tagfile ]; then
     cat $PACKAGE_DIR/tagfile >> $TMP/tagfile
    fi
   fi

  #
  # Do we need to follow a custom path to the tagfiles?
  #
  elif [ -r $TMP/SeTtagpath ]; then
   custom_path=`cat $TMP/SeTtagpath`
   short_path=`basename $PACKAGE_DIR`

   # If tagfile exists at the specified custom path, copy it over.
   if [ -r $custom_path/$short_path/tagfile ]; then
    cat $custom_path/$short_path/tagfile >> $TMP/tagfile

   else # well, I guess we'll use the default one then.
    if [ -r $PACKAGE_DIR/tagfile ]; then
     cat $PACKAGE_DIR/tagfile >> $TMP/tagfile
    fi
   fi
  #
  # We seem to be testing for this too often... maybe this code should
  # be optimized a little...
  # 
  elif [ -r $PACKAGE_DIR/tagfile ]; then
   cat $PACKAGE_DIR/tagfile >> $TMP/tagfile
  fi

  #
  # Execute menus if in QUICK mode:
  #
  if [ -r $TMP/SeTQUICK -a -r $PACKAGE_DIR/maketag ]; then
   if [ ! "$MAKETAG" = "" -a -r $PACKAGE_DIR/$MAKETAG ]; then # use alternate maketag
    sh $PACKAGE_DIR/$MAKETAG
   else    
    sh $PACKAGE_DIR/maketag
   fi
   if [ -r $TMP/SeTnewtag ]; then
    mv $TMP/SeTnewtag $TMP/tagfile
   fi
  fi

  #
  # Protect tagfile from hacker attack:
  #
  if [ -r $TMP/tagfile ]; then
   chmod 600 $TMP/tagfile
  fi

 fi #  ! "$DISK_SETS" = "disk" 

 # It's possible that the tagfile was specified on the command line.  If that's
 # the case, then we'll just override whatever we figured out up above.
 if [ ! "$USETAG" = "" ]; then
   cat $USETAG > $TMP/tagfile
 fi

 if [ "$1" = "single_disk" -o -r $PACKAGE_DIR/disk$1 ]; then
  CATALOG_FILE=`basename $PACKAGE_DIR/disk*`;
  if [ -r $PACKAGE_DIR/$CATALOG_FILE -a ! -d $PACKAGE_DIR/$CATALOG_FILE ]; then
   if fgrep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE 1> /dev/null 2>&1 ; then
    # First we check for missing packages...
    for PKGTEST in `fgrep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE | cut -b10- 2> /dev/null` ; do
     if [ ! -r $PACKAGE_DIR/$PKGTEST.tgz ]; then
      cat << EOF > $TMP/tmpmsg

WARNING!!!

While looking through your index file ($CATALOG_FILE), I 
noticed that you might be missing a package ($PKGTEST.tgz) 
that is supposed to be on this disk (disk $1). You may go
on with the installation if you wish, but if this is a 
crucial file I'm making no promises that your machine will
boot.

EOF
      dialog --title "FILE MISSING FROM YOUR DISK" --msgbox \
"`cat $TMP/tmpmsg`" 15 73
     fi
    done # checking for missing packages
    # Now we test for extra packages
    ALLOWED="`fgrep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE | cut -b10- 2> /dev/null`" 
    for PACKAGE_FILENAME in $PACKAGE_DIR/*.tgz; do
     BASE="`basename $PACKAGE_FILENAME .tgz`"
     if echo $ALLOWED | fgrep $BASE 1> /dev/null 2>&1 ; then
      GOOD="yup yup"
     else
      cat << EOF > $TMP/tmpmsg

WARNING!!!

While looking through your index file ($CATALOG_FILE), I 
noticed that you have this extra package ($BASE.tgz) that
I don't recongnize. Please be sure this package is really
supposed to be here, and is not left over from an old 
version of Slackware. Sometimes this can happen at the 
archive sites.

EOF
      dialog --title "EXTRA FILE FOUND ON YOUR DISK" \
--msgbox "`cat $TMP/tmpmsg`" 15 67 
      rm -f $TMP/tmpmsg
     fi
    done 
   fi
   cat $PACKAGE_DIR/$CATALOG_FILE > $ADM_DIR/disk_contents/$CATALOG_FILE
   chmod 644 $ADM_DIR/disk_contents/$CATALOG_FILE
  fi
  for PACKAGE_FILENAME in $PACKAGE_DIR/*.tgz; do
   if [ "$PACKAGE_FILENAME" = "$PACKAGE_DIR/*.tgz" ]; then
    continue;
   fi
   CURRENT_PACKAGE_NAME=`basename $PACKAGE_FILENAME .tgz`
   AddKey=""
   SkipKey=""
   if [ "$ASK" = "tagfiles" ]; then # -a ! "$DISK_SETS" = "disk" ]; then
    if fgrep "$CURRENT_PACKAGE_NAME:" $TMP/tagfile | sed -n '$ p' | fgrep ADD 1> /dev/null 2>&1 ; then
     AddKey="ADD"
    fi
    if fgrep "$CURRENT_PACKAGE_NAME:" $TMP/tagfile | sed -n '$ p' | fgrep SKP 1> /dev/null 2>&1 ; then
     SkipKey="SKIP"
    fi
   elif [ "$ASK" = "never" ]; then
    AddKey="ADD"
   else # ASK must equal always
    ASK="always"
    fi  
   if [ ! "$DISK_SETS" = "disk" ]; then
    if fgrep "$CURRENT_PACKAGE_NAME:" $TMP/tagfile | sed -n '$ p' | fgrep ADD > /dev/null 2> /dev/null ; then
     PRIORITY="[required]"
    elif fgrep "$CURRENT_PACKAGE_NAME:" $TMP/tagfile | sed -n '$ p' | fgrep REC > /dev/null 2> /dev/null ; then
     PRIORITY="[recommended]"
    elif fgrep "$CURRENT_PACKAGE_NAME:" $TMP/tagfile | sed -n '$ p' | fgrep OPT > /dev/null 2> /dev/null ; then
     PRIORITY="[optional]"
    elif fgrep "$CURRENT_PACKAGE_NAME:" $TMP/tagfile | sed -n '$ p' | fgrep SKP > /dev/null 2> /dev/null ; then
     PRIORITY="[skip]"
    else
     PRIORITY="[unknown]"
    fi
   fi
   PACKAGE_SIZE=`filesize $PACKAGE_FILENAME`
   if [ "$AddKey" = "ADD" ]; then
    # echo "Auto-installing package ==>$CURRENT_PACKAGE_NAME<==  Priority: $PRIORITY" > $TMP/tmpmsg
    echo > $TMP/tmpmsg
    # Print out the description text:
    CATALOG_FILE=`basename $PACKAGE_DIR/disk*`;
#    if [ -r $PACKAGE_DIR/$CATALOG_FILE -a ! -d $PACKAGE_DIR/$CATALOG_FILE ]; then
#     fgrep "$CURRENT_PACKAGE_NAME:" $PACKAGE_DIR/$CATALOG_FILE | cut -b11- 1>> $TMP/tmpmsg 2> /dev/null ;
#    else
    for index_file in $PACKAGE_DIR/disk* $PACKAGE_DIR/package_descriptions ; do
     if [ ! "$index_file" = "$PACKAGE_DIR/disk??*" ]; then
      if [ -r "$index_file" ]; then
       cat $index_file | fgrep "$CURRENT_PACKAGE_NAME:" | cut -b11- 1>> $TMP/tmpmsg 2> /dev/null 
      fi
     fi
    done
#    fi
    COMPBYTES="`gzip -l $PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz | sed -n '$ p' | cut -b1-9`"
    UNCOMPBYTES="`gzip -l $PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz | sed -n '$ p' | cut -b10-19`"
    COMPRESSED="`expr $COMPBYTES / 1024` K"
    UNCOMPRESSED="`expr $UNCOMPBYTES / 1024` K"
    echo "Size: Compressed: $COMPRESSED, uncompressed: $UNCOMPRESSED." >> $TMP/tmpmsg
    if [ ! "$PRIORITY" = "" ]; then
     PMSG="  Priority: $PRIORITY"
    else
     PMSG=""
    fi
    dialog --title "Auto-installing package ==>$CURRENT_PACKAGE_NAME<==$PMSG" --infobox "`cat $TMP/tmpmsg`" 15 75
    rm -f $TMP/tmpmsg
    install_the_current_package;
   elif [ "$SkipKey" != "SKIP" ]; then
    # echo "Package Name: ==>$CURRENT_PACKAGE_NAME<==  Priority: $PRIORITY" > $TMP/tmpmsg
    echo > $TMP/tmpmsg
    CATALOG_FILE=`basename $PACKAGE_DIR/disk*`;
    if [ -r $PACKAGE_DIR/$CATALOG_FILE -a ! -d $PACKAGE_DIR/$CATALOG_FILE ]; then
     fgrep "$CURRENT_PACKAGE_NAME:" $PACKAGE_DIR/$CATALOG_FILE | cut -b11- 1>> $TMP/tmpmsg 2> /dev/null ;
    fi
    COMPBYTES="`gzip -l $PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz | sed -n '$ p' | cut -b1-9`"
    UNCOMPBYTES="`gzip -l $PACKAGE_DIR/$CURRENT_PACKAGE_NAME.tgz | sed -n '$ p' | cut -b10-19`"
    COMPRESSED="`expr $COMPBYTES / 1024` K"
    UNCOMPRESSED="`expr $UNCOMPBYTES / 1024` K"
    echo "Size: Compressed: $COMPRESSED, uncompressed: $UNCOMPRESSED." >> $TMP/tmpmsg
    echo >> $TMP/tmpmsg
    echo "Install package $CURRENT_PACKAGE_NAME? " >> $TMP/tmpmsg
    if [ ! "$PRIORITY" = "" ]; then
     PMSG="  Priority: $PRIORITY"
    else
     PMSG=""
    fi
    dialog --title "Package Name: ==>$CURRENT_PACKAGE_NAME<==$PMSG" --menu "`cat $TMP/tmpmsg`" 22 75 1 \
"Yes" "Install package $CURRENT_PACKAGE_NAME" \
"No" "Do not install package $CURRENT_PACKAGE_NAME" \
"Quit" "Abort software installation completely" 2> $TMP/reply
    if [ $? = 1 -o $? = 255 ]; then
     echo "No  " > $TMP/reply
    fi
    REPLY="`cat $TMP/reply`"
    rm -f $TMP/reply $TMP/tmpmsg
    if [ "$REPLY" = "Yes" ]; then
     dialog --title "INSTALLING" --infobox "Installing package $CURRENT_PACKAGE_NAME" 3 50
     install_the_current_package;
    elif [ "$REPLY" = "Quit" ]; then
     umount_the_source;
     chmod 755 $TARGET_DIR
     chmod 1777 $TARGET_DIR/tmp
     exit 1;
    elif [ "$REPLY" = "No" ]; then
     dialog --title "SKIPPING PACKAGE" --infobox "Skipping package $CURRENT_PACKAGE_NAME" 3 50
    fi
   fi
  done
  OUTTAHERE="false"
  if [ -r $PACKAGE_DIR/install.end ]; then
   OUTTAHERE="true"
  fi
  umount_the_source;
  if [ "$OUTTAHERE" = "true" ]; then
   return 1;
  fi
 else
  umount_the_source;
  if [ ! "$SOURCE_MOUNTED" = "always" ]; then
   cat << EOF > $TMP/tmpmsg

This does not look like the correct disk. You may either check to
see if you've got the right disk in there ($1) and try again, or 
you may skip the current disk series.

EOF
   dialog --title "INCORRECT DISK INSERTED" --menu "`cat $TMP/tmpmsg`" 15 70 2 \
"Retry" "Try to mount disk $1 again" \
"Skip" "Skip this disk series" 2> $TMP/reply
   if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/reply $TMP/tmpmsg
    exit
   fi
   REPLY="`cat $TMP/reply`"
   rm -f $TMP/reply $TMP/tmpmsg
   if [ "$REPLY" = "Skip" ]; then
    return 1;
   else
    install_disk $1;
   fi
  else
   cat << EOF > $TMP/tmpmsg
WARNING:

Can't find a disk series $SERIES_NAME in the source directory.
Skipping it...

EOF
   dialog --title "SELECTED SERIES NOT PRESENT" --msgbox "`cat $TMP/tmpmsg`" 10 65
   rm -f $TMP/tmpmsg
   return 1; 
  fi 
 fi;
}
install_disk_set() { # accepts one argument: the series name in lowercase.
 SERIES_NAME=$1
 CURRENT_DISK_NUMBER="1";
 while [ 0 ]; do
  install_disk $SERIES_NAME$CURRENT_DISK_NUMBER;
  if [ $? = 1 -o $? = 255 ]; then # install.end was found, or the user chose
        # to quit installing packages.
   return 0;
  fi
  CURRENT_DISK_NUMBER=`expr $CURRENT_DISK_NUMBER + 1`
 done;
}
if [ "$DISK_SETS" = "disk" ]; then
 install_disk single_disk;
 ASK="always"
else
 touch $TMP/tagfile
 chmod 600 $TMP/tagfile
 if echo $DISK_SETS | fgrep "#a#" 1> /dev/null 2>&1; then
  A_IS_NEEDED="true"
 else
  A_IS_NEEDED="false"
 fi
 while [ 0 ];
 do
  while [ 0 ]; # strip leading '#'s
  do
   if [ "`echo $DISK_SETS | cut -b1`" = "#" ]; then
    DISK_SETS="`echo $DISK_SETS | cut -b2-`"
   else
    break;
   fi
  done
  if [ "$A_IS_NEEDED" = "true" ]; then
   if [ "$TARGET_DIR" = "/" ]; then
    dialog --title "WARNING: BIG TROUBLE DETECTED" \
--menu " *** WARNING!  Reinstalling your A series to a running system \
is not (yet) a good idea. It is suggested that you do not do this." \
11 70 3 \
"Abort" "Abort software installation." \
"Ignore" "Ignore warning and reinstall the A series anyway." \
"Skip" "Skip the A series, but continue installing software." 2> $TMP/skip
    if [ $? = 1 -o $? = 255 ]; then
     exit
    fi
    WHATDO="`cat $TMP/skip`" 
    rm -f $TMP/skip
    if [ "$WHATDO" = "Abort" ]; then
     dialog --msgbox "Aborting..." 5 30
     A_IS_NEEDED="false"
     DISK_SETS=""
     continue;
    elif [ "$WHATDO" = "Skip" ]; then
     dialog --msgbox "Skipping A series..." 5 30
     A_IS_NEEDED="false"
     continue;
    elif [ ! "$WHATDO" = "Ignore" ]; then
     continue; # unknown response
    fi
   fi
   cat << EOF > $TMP/tmpmsg

--- Installing package series ==>a<==

EOF
   dialog --infobox "`cat $TMP/tmpmsg`" 5 45
   sleep 1
   rm -f $TMP/tmpmsg
   install_disk_set a;
   A_IS_NEEDED="false"
  fi
  count="1"
  if [ "`echo $DISK_SETS | cut -b$count`" = "" ]; then
   break; # we be done here :^)
  else
   count="2"
   while [ 0 ]; do
    if [ "`echo $DISK_SETS | cut -b$count`" = "" -o "`echo $DISK_SETS | cut -b$count`" = "#" ]; then
     count="`expr $count - 1`"
     break;
    else
     count="`expr $count + 1`"
    fi 
   done
  fi 
  diskset="`echo $DISK_SETS | cut -b1-$count`"
  count="`expr $count + 1`"
  DISK_SETS="`echo $DISK_SETS | cut -b$count-`"
  if [ "$diskset" = "a" ]; then
   continue; # we expect this to be done elsewhere
  fi
  cat << EOF > $TMP/tmpmsg

Installing package series ==>$diskset<==

EOF
  dialog --infobox "`cat $TMP/tmpmsg`" 5 45
  sleep 1
  rm -f $TMP/tmpmsg
  install_disk_set $diskset; 
 done
fi

if [ "$DISK_SETS" = "disk" -o "$CMD_START" = "true" ]; then
 if [ -r $TMP/tagfile ]; then
  rm $TMP/tagfile
 fi
 reset
fi
chmod 755 $TARGET_DIR $TARGET_DIR/var $TARGET_DIR/usr
chmod 1777 $TARGET_DIR/tmp
