#!/bin/sh
#
# Copyright 1993, 1994, 1995, 1996, 1997,
#    1998, 1999  Patrick Volkerding,  Moorhead, MN  USA
# Copyright 2001  Slackware Linux, Inc.,  Concord, CA  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.

# 2004/01/25  bi-lingualize for Plamo Linux
#             KOJIMA Mitsuhiro <kojima@linet.gr.jp>

# Get language

case "$CMDNAME" in
pkgtoolj)
 MSGLANG=JP
 ;;
pkgtoole)
 MSGLANG=EN
 ;;
*)
 if env | grep -e "\(kterm\|kon\)" > /dev/null 2>&1 ; then
  MSGLANG=JP
 else
  MSGLANG=EN
 fi
 ;;
esac

SOURCE_DIR=/var/log/mount
ASK="tagfiles"
if [ -L /bin/chmod -a -L /bin/chown ]; then # probably on the bootdisk using busybox
 TARGET_DIR=/mnt
 TMP=/mnt/var/log/setup/tmp
 if mount | grep "on /mnt" 1> /dev/null 2>&1 ; then # good
  true
 else # bad
  if [ "$MSGLANG" = "JP" ]; then
      echo
      echo
      echo "rootdsk  pkgtool ¹Ԥϡ Linux ѤΥѡƥ /mnt "
      echo "˥ޥȤƤɬפޤʲˤΤ򤤤Ĥޤ "
      echo
      echo "롼ȥѡƥ /dev/hda1 ˤꡤext2 ˤʤäƤ:"
      echo "mount /dev/hda1 /mnt -t ext2"
      echo
      echo "/usr ѡƥ /dev/hda2 ˤ³ưʲΥޥɤ¹Ԥޤ:"
      echo "mount /dev/hda2 /mnt/usr -t ext2"
      echo
      echo "Linux ѤΥѡƥޥȤ pkgtool Ƽ¹ԤƤ"
      echo
  else
      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
  fi
  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

# remove whitespace
crunch() {
  while read FOO ; do
    echo $FOO
  done
}

package_name() {
  STRING=`basename $1 .tgz`
  # Check for old style package name with one segment:
  if [ "`echo $STRING | cut -f 1 -d -`" = "`echo $STRING | cut -f 2 -d -`" ]; then
    echo $STRING
  else # has more than one dash delimited segment
    # Count number of segments:
    INDEX=1
    while [ ! "`echo $STRING | cut -f $INDEX -d -`" = "" ]; do
      INDEX=`expr $INDEX + 1`
    done
    INDEX=`expr $INDEX - 1` # don't include the null value
    # If we don't have four segments, return the old-style (or out of spec) package name:
    if [ "$INDEX" = "2" -o "$INDEX" = "3" ]; then
      echo $STRING
    else # we have four or more segments, so we'll consider this a new-style name:
      NAME=`expr $INDEX - 3`
      NAME="`echo $STRING | cut -f 1-$NAME -d -`"
      echo $NAME
      # cruft for later ;)
      #VER=`expr $INDEX - 2`
      #VER="`echo $STRING | cut -f $VER -d -`"
      #ARCH=`expr $INDEX - 1`
      #ARCH="`echo $STRING | cut -f $ARCH -d -`"
      #BUILD="`echo $STRING | cut -f $INDEX -d -`"
    fi
  fi
}

remove_packages() {
 for pkg_name in $* 
 do
  # echo "pkg_name: $pkg_name"
  # sleep 5
  if [ -r $ADM_DIR/packages/$pkg_name ]; then
      if [ "$MSGLANG" = "JP" ]; then
	  dialog --title "ѥåκ" --cr-wrap --infobox \
"\n$pkg_name Ƥޤ\n\
\n\
¾ΥѥåȽʣƤե̵ͭ򤽤줾Υե뤴\
˥åƤΤǡѥåκˤϤʤ֤ޤ \
Ÿ򸫤ˤϡ̤Υ󥽡ưưʲΥޥɤ \
¹ԤƤ \n\
\n\
tail -f $TMP/PKGTOOL.REMOVED\n" 13 60
      else
	  dialog --title "PACKAGE REMOVAL IN PROGRESS" --cr-wrap --infobox \
"\nRemoving package $pkg_name.\n\
\n\
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:\n\
\n\
tail -f $TMP/PKGTOOL.REMOVED\n" 13 60
      fi
   export ROOT=$TARGET_DIR
   # echo "removepkg $pkg_name"
   # sleep 5
   removepkg $pkg_name >> $LOG 2> /dev/null
  else
      if [ "$MSGLANG" = "JP" ]; then
	  echo "$pkg_name ȤѥåϸʤΤǺǤޤ" >> $LOG
      else
	  echo "No such package: $pkg_name. Can't remove." >> $LOG
      fi
  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
     if [ "$MSGLANG" = "JP" ]; then
	 dialog --title "Plamo Linux ѥåġ (derived from pkgtool version 9.1.0)" \
--menu "\nPlamo Linux ѥåġؤ褦\n\
\nɤޤ礦\n" 17 75 7 \
"Current" "ȥǥ쥯Ȥˤѥå򥤥󥹥ȡ" \
"Other" "¾Υǥ쥯ȥ꤫ѥå򥤥󥹥ȡ" \
"Floppy" "եåԡѥå򥤥󥹥ȡ" \
"Remove" "󥹥ȡ뤵Ƥѥå" \
"View" "ѥå˴ޤޤƤե" \
"Setup" "󥹥ȡѥѥåΥ󥹥ȡ륹ץȤƼ¹" \
"Exit" "pkgtool νλ" 2> $TMP/reply
     else
	 dialog --title "Plamo Linux Package Tool (derived from pkgtool version 9.1.0)" \
--menu "\nWelcome to the Plamo Linux package tool.\n\
\nWhich option would you like?\n" 17 75 7 \
"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" \
"Setup" "Choose Slackware installation scripts to run again" \
"Exit" "Exit Pkgtool" 2> $TMP/reply
     fi
  if [ ! $? = 0 ]; then
   rm -f $TMP/reply
   dialog --clear
   exit
  fi
  REPLY="`cat $TMP/reply`"
  rm -f $TMP/reply
  if [ "$REPLY" = "Exit" ]; then
   dialog --clear
   exit
  fi
  if [ "$REPLY" = "Setup" ]; then
      if [ "$MSGLANG" = "JP" ]; then
	  echo 'dialog --title "ƥꥹץȤ" --item-help --checklist \
	      "ڡСǺƼ¹ԤץȤ򤷤Ƥ򤬽 \
Enter 򲡤Ƥ" 17 70 9 \' > $TMP/setupscr
      else
          echo 'dialog --title "SELECT SYSTEM SETUP SCRIPTS" --item-help --checklist \
	      "Please use the spacebar to select the setup scripts to run.  Hit enter when you \
are done selecting to run the scripts." 17 70 9 \' > $TMP/setupscr
      fi
    for script in $ADM_DIR/setup/setup.* ; do
      BLURB=`grep '#BLURB' $script | cut -b8-`
      if [ "$BLURB" = "" ]; then
        BLURB="\"\""
      fi
      echo " \"`basename $script | cut -f2- -d .`\" $BLURB \"no\" $BLURB \\" >> $TMP/setupscr 
    done
    echo "2> $TMP/return" >> $TMP/setupscr
    . $TMP/setupscr
    if [ ! "`cat $TMP/return`" = "" ]; then
      # Run each script:
      for script in `cat $TMP/return` ; do
        scrpath=$ADM_DIR/setup/setup.`echo $script | tr -d \"`
        # rootdevice="`mount | head -1 | cut -f 1 -d ' '`"
        rootdevice="`mount | sed -n '1p' | cut -f 1 -d ' '`"
        ( COLOR=on ; cd $TARGET_DIR ; . $scrpath / $rootdevice )
      done
    fi
    rm -f $TMP/return $TMP/setupscr
    continue
  fi # end Setup
  if [ "$REPLY" = "View" ]; then
   DEFITEM=""
   export DEFITEM
   if [ "$MSGLANG" = "JP" ]; then
       dialog --title "" --infobox "󥹥ȡ뤵줿ѥåΰɽ \
Τ˥ƥĴƤޤΤǤФ餯Ԥ" 0 0
       echo 'dialog $DEFITEM --item-help --menu "Ƥ򸫤ѥå򤷤Ƥ" 17 68 10 \' > $TMP/viewscr
   else   
     dialog --title "SCANNING" --infobox "Please wait while \
Pkgtool scans your system to determine which packages you have \
installed and prepares a list for you." 0 0
     echo 'dialog $DEFITEM --item-help --menu "Please select the package you wish to view." 17 68 10 \' > $TMP/viewscr
   fi
	  
   for name in `ls $ADM_DIR/packages` ; do
    pkg_name=`package_name $name`
    BLURB="`sed -n \"/$pkg_name:/{s/\\"//g;p;q;}\" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
    # Let's have some backward compatibility with the interim beta (for now):
    if [ "$BLURB" = "" ]; then
     BLURB="`sed -n \"/$name:/{s/\\"//g;p;q;}\" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
    fi 
    echo " \"$name\" \"$BLURB\" \"View information about package $name\" \\" >> $TMP/viewscr
   done
   echo "2> $TMP/return" >> $TMP/viewscr
   while [ 0 ]; do
    . $TMP/viewscr
    if [ ! "`cat $TMP/return`" = "" ]; then
     DEFITEM="--default-item `cat $TMP/return`"
     if [ "$MSGLANG" = "JP" ]; then
	 dialog --title "ѥå: `cat $TMP/return`" --no-shadow --textbox "$ADM_DIR/packages/`cat $TMP/return`"  0 0 2> /dev/null
     else
	 dialog --title "CONTENTS OF PACKAGE: `cat $TMP/return`" --no-shadow --textbox "$ADM_DIR/packages/`cat $TMP/return`" 0 0 2> /dev/null
     fi
    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
      if [ "$MSGLANG" = "JP" ]; then
	  dialog --title "" --infobox "ɤΥѥå򥤥󥹥ȡ뤷ΥꥹȤ \
뤿˥ƥĴ٤ƤޤФ餯Ԥ" 0 0
   # end section
   cat << EOF > $TMP/rmscript
#! /bin/bash
/bin/dialog --title "ѥå" --item-help --checklist \
"Хå򤷤ƤUP/DOWN ǥư \
ڡǺѥåޡƤޡ \
ǤդθĿդ뤳ȤǤޤ" 20 76 11 \\
EOF
     else
	  dialog --title "SCANNING" --infobox "Please wait while Pkgtool scans \
your system to determine which packages you have installed and prepares \
a list for you." 0 0
   # end section
cat << EOF > $TMP/rmscript
dialog --title "SELECT PACKAGES TO REMOVE" --item-help --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." 20 75 11 \\
EOF
     fi
   for name in `ls $ADM_DIR/packages` ; do
    pkg_name=`package_name $name`
    BLURB="`sed -n \"/$pkg_name:/{s/\\"//g;p;q;}\" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
    # Let's have some backward compatibility with the interim beta (for now):
    if [ "$BLURB" = "" ]; then
     BLURB="`sed -n \"/$name:/{s/\\"//g;p;q;}\" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
    fi 
    # echo "$pkg_name, $BLURB"
    if [ "$MSGLANG" = "JP" ]; then
	echo " \"$name\" \"$BLURB\" off \"$name /ʤΥޡդƤ\" \\" >> $TMP/rmscript
    else
	echo " \"$name\" \"$BLURB\" off \"Select/Unselect removing package $name\" \\" >> $TMP/rmscript
    fi
   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
    if [ "$MSGLANG" = "JP" ]; then
	dialog --title "ѥåϤޤ" --msgbox "ᥤ˥塼ˤϡλפ򲡤Ƥ" 8 70
    else
	dialog --title "NO PACKAGES REMOVED" --msgbox "Hit OK to return to the main menu." 5 40
    fi
   else
       if [ "$MSGLANG" = "JP" ]; then
	   dialog --title "ѥåκλ" --msgbox "ѥåϺޤ \
줿եΥꥹȤ$TMP: PKGTOOL.REMOVED ˺ޤ" 0 0
       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." 0 0
       fi
   fi
   rm -f $TMP/rmscript $TMP/return $TMP/tmpmsg $TMP/SeT*
   chmod 755 /
   chmod 1777 /tmp
# No, return to the main menu:
#   exit
  elif [ "$REPLY" = "Floppy" ]; then
    if [ "$MSGLANG" = "JP" ]; then
	dialog --title "եåԡɥ饤֤" --menu "ɤΥեåԡɥ饤֤ \
󥹥ȡ뤷ޤ" \
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
    else
   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
    fi
   if [ $? = 1 ]; then
    dialog --clear
    exit
   fi
   SOURCE_DEVICE="`cat $TMP/wdrive`"
   rm -f $TMP/wdrive 
   if [ "$MSGLANG" = "JP" ]; then
   cat << EOF > $TMP/tmpmsg

󥹥ȡ뤷ǥåȤ̾Ƥ
ʣΥåȤꤹ a ap x xap Τ褦˥ڡ
ڤäƤ

Υǥ饤󥹥ȡ뤹ϲϤ
Enter 򲡤Ƥ

EOF
dialog --title "եȥ" --inputbox "`cat $TMP/tmpmsg`" 13 70 2> $TMP/sets 
   else
   cat << EOF > $TMP/tmpmsg

Enter the names of any disk sets you would like to install.
Separate 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 
   fi
   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
    if [ "$MSGLANG" = "JP" ]; then
	dialog --title "󥹥ȡ븵Υǥ쥯ȥ" --inputbox "ѥå򥤥󥹥ȡ뤹 \
ǥ쥯ȥϤƤ:" 10 50 2> $TMP/pkgdir
    else
	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
    fi
   if [ $? = 1 ]; then
    rm -f $TMP/pkgdir $TMP/SeT*
    dialog --clear
    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
       if [ "$MSGLANG" = "JP" ]; then
	   dialog --title "ǥ쥯ȥ꤬Ĥޤ" --msgbox "Υǥ쥯ȥ($SOURCE_DIR) \
ĤޤǤǥ쥯ȥ̾ǧƺټ¹ԤƤ" 10 50
       else
    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
       fi
    dialog --clear
    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

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
      if [ "$MSGLANG" = "JP" ]; then
   cat << EOF > $TMP/tmpmsg

ꤵ줿󥹥ȡ븵˥ǤޤǤ

$SOURCE_DIR ޥȤƤꡤɬפʥǥåȤ
$SOURCE_DIR ʲΥ֥ǥ쥯ȥ֤Ƥ뤳Ȥǧ
Ƥ

EOF
   dialog --title "ޥȥ顼" --msgbox "`cat $TMP/tmpmsg`" 11 67
      else
   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
      fi
   rm -f $TMP/tmpmsg
   exit 1;
  fi
  return 0;
 fi
 if [ "$MSGLANG" = "JP" ]; then
     dialog --title "ǥ" --menu "ǥ $1  \
Enter 򲡤Ƥ" \
11 50 3 \
"Continue" "󥹥ȡ³" \
"Skip" "Υǥ꡼ϥåפ" \
"Quit" "󥹥ȡߤ" 2> $TMP/reply
 else
 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
 fi
 if [ ! $? = 0 ]; then
  REPLY="Quit"
 else
  REPLY="`cat $TMP/reply`"
 fi
 rm -f $TMP/reply
 if [ "$REPLY" = "Skip" ]; then
  return 1;
 fi
 if [ "$REPLY" = "Quit" ]; then
     if [ "$MSGLANG" = "JP" ]; then
	 dialog --title "λ" --msgbox "եȥΥ󥹥ȡǤޤ" 5 60
     else
	 dialog --title "ABORTING" --msgbox "Aborting software installation." 5 50
     fi
   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
       if [ "$MSGLANG" = "JP" ]; then
	   dialog --title "ޥȻȯ" --menu "ǥ˥ޥȤǤޤ \
ǤٻߤޤȤߤޤ" 10 72 2 \
"Yes" "ٻߤ" \
"No" "ߤ." 2> $TMP/mntans
       else
	   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
       fi
    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;
}

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

 # If this directory is missing or contains no *.tgz files, bail.
 if [ ! -d $PACKAGE_DIR ]; then
  return 1
 fi
 if ls $PACKAGE_DIR/*.tgz 1> /dev/null 2> /dev/null ; then
  true
 else
  return 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 there's a catalog file present, use it to check for missing files.
 # If not, forget about that and install whatever's there.
 if [ "$1" = "single_disk" -o -r $PACKAGE_DIR/disk$1 -o -r $PACKAGE_DIR/package-list.txt ]; then
  if [ -r $PACKAGE_DIR/package-list.txt ]; then
   CATALOG_FILE=$PACKAGE_DIR/package-list.txt
  else
   CATALOG_FILE=`basename $PACKAGE_DIR/disk*`;
  fi
  if [ -r $PACKAGE_DIR/$CATALOG_FILE -a ! -d $PACKAGE_DIR/$CATALOG_FILE ]; then
   if grep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE 1> /dev/null 2>&1 ; then
    # First we check for missing packages...
    for PKGTEST in `grep "^CONTENTS:" $PACKAGE_DIR/$CATALOG_FILE | cut -f2- -d : 2> /dev/null` ; do
     # This is not a perfect test.  (say emacs is missing but emacs-nox is not)
     if ls $PACKAGE_DIR/$PKGTEST*.tgz 1> /dev/null 2> /dev/null ; then # found something like it
      true
     else
	 if [ "$MSGLANG" = "JP" ]; then
      cat << EOF > $TMP/tmpmsg

ٹ!!!

ǥåե($CATALOG_FILE)Ĵ٤Ȥ
ʲΥѥå¸ߤʤȤ狼ޤ

$PKGTEST-\*-\*-\*.tgz

ΥѥåϤΥǥ(ǥ $1)ˤ٤ΤǤ
Τޤޥ󥹥ȡ³Ԥ뤳ȤϲǽǤ
ѥåƥưɬܤΤΤʤ顤󥹥ȡ뤷
ƥबưʤʤǽ⤢ޤ

EOF
      dialog --title "ե뤬Ĥޤ" --msgbox \
"`cat $TMP/tmpmsg`" 17 67
	 else
      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`" 17 67
       fi
     fi
    done # checking for missing packages
    # Now we test for extra packages:
    ALLOWED="`grep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE | cut -b10- 2> /dev/null`" 
    for PACKAGE_FILENAME in $PACKAGE_DIR/*.tgz; do
     BASE="`basename $PACKAGE_FILENAME .tgz`"
     BASE="`package_name $BASE`"
     if echo $ALLOWED | grep $BASE 1> /dev/null 2>&1 ; then
      true
     else
	 if [ "$MSGLANG" = "JP" ]; then
      cat << EOF > $TMP/tmpmsg

ٹ!!!

ǥåե($CATALOG_FILE)Ĵ٤Ȥ
ꥹȤˤʤΥѥåĤޤ

($BASE.tgz) 

Υѥåˤˤ٤ޤΥС
ѥåǤϤʤǧƤߥ顼ȤǤ
Ťѥå¸Ƥ礬ޤ

EOF
      dialog --title ";פʥեȯޤ" \
--msgbox "`cat $TMP/tmpmsg`" 17 67 
	 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 recognize. 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`" 17 67 
	 fi
      rm -f $TMP/tmpmsg
     fi
    done 
   fi
  fi
 fi # check for missing/extra packages

 # Install the packages:
 for PACKAGE_FILENAME in $PACKAGE_DIR/*.tgz; do
  if [ "$PACKAGE_FILENAME" = "$PACKAGE_DIR/*.tgz" ]; then
   continue;
  fi
  if [ "$ASK" = "never" ]; then # install the package
    installpkg -root $TARGET_DIR -infobox -tagfile $TMP/tagfile $PACKAGE_FILENAME
    # installpkg  $PACKAGE_FILENAME
   ERROR=$?
  elif [ "$ASK" = "tagfiles" ]; then
    installpkg -root $TARGET_DIR -menu -tagfile $TMP/tagfile $PACKAGE_FILENAME
    # installpkg $PACKAGE_FILENAME
   ERROR=$?
  else # ASK should be = always here, and that's how we'll treat it
    installpkg -root $TARGET_DIR -menu -ask -tagfile $TMP/tagfile $PACKAGE_FILENAME
    # installpkg -menu -ask -tagfile $TMP/$tagfile $PACKAGE_FILENAME
   ERROR=$?
  fi
  # Check for abort:
  if [ "$ERROR" = "99" ]; then
   umount_the_source;
   chmod 755 $TARGET_DIR
   chmod 1777 $TARGET_DIR/tmp
   exit 1;
   fi
 done
 OUTTAHERE="false"
 if [ -r $PACKAGE_DIR/install.end ]; then
  OUTTAHERE="true"
 fi
 umount_the_source;
 if [ "$OUTTAHERE" = "true" ]; then
  return 1;
 fi
}

install_disk_set() { # accepts one argument: the series name in lowercase.
 SERIES_NAME=$1
 CURRENT_DISK_NUMBER="1";
 while [ 0 ]; do
  # Don't start numbering the directories until 2:
  if [ $CURRENT_DISK_NUMBER = 1 ]; then
    DISKTOINSTALL=$SERIES_NAME
  else
    DISKTOINSTALL=$SERIES_NAME$CURRENT_DISK_NUMBER
  fi
  install_disk $DISKTOINSTALL
  if [ ! $? = 0 ]; 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;
}

# /* main() */ ;)
if [ "$DISK_SETS" = "disk" ]; then
 install_disk single_disk;
 ASK="always"
else
 touch $TMP/tagfile
 chmod 600 $TMP/tagfile
 if echo $DISK_SETS | grep "#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
   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
 dialog --clear
fi
chmod 755 $TARGET_DIR $TARGET_DIR/var $TARGET_DIR/usr
chmod 1777 $TARGET_DIR/tmp
