#!/bin/sh
# Copyright 1994, 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.
#
#  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.
#
# Sun Nov 26 12:38:25 CST 1995
# Added patch from Glenn Moloney <glenn@physics.unimelb.edu.au> to allow
# packages to be installed to directories other than /.
#

# Plamo tweaks:
# 1997/12/23 Japanise
# 2002/10/04 doinst.sh hack
#            (by Shun-ichi TAHARA <jado@flowernet.gr.jp>)
# 2002/11/04 Fix problem on symlinks with recent tar
#            (by Chisato Yamauchi <cyamauch@hst.phyas.aichi-edu.ac.jp>)
# 2003/03/28 Gather installpkge, installpkgj and makepkg
#            (by Shun-ichi TAHARA <jado@flowernet.gr.jp>)
# 2003/04/06 Fix problem on . with recent tar
#            (by KOJIMA Mitsuhiro <kojima@linet.gr.jp>
#             and TAMUKI Shoichi <tamuki@linet.gr.jp>)
# 2003/04/06 More minor tweaks
#            (by Shun-ichi TAHARA <jado@flowernet.gr.jp>)
# 2003/09/17 Fasten extracting files
#            (by TAMUKI Shoichi <tamuki@linet.gr.jp>)
# 2004/01/23 add long package name facilities
#            (by KOJIMA Mitsuhiro <kojima@Linet.gr.jp>)

# Get script name

CMDNAME=`basename $0`


# Get language

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

TAR=tar

# Eliminate whitespace function:
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
}

# FIXME: needs perl (or prepare readlink(1) in basebin...)
readlink() {
 /usr/bin/perl -e "print readlink(\"$1\").\"\n\";"
}

link_script() {
 sed -e '/^#%% begin/,/^#%% end/d' $1
}

init_script() {
 sed -e '/^#%% begin/,/^#%% end/!d' $1
}

make_install_script() {
 COUNT=1
 LINE="`sed -n "$COUNT p" $1`"
 while [ ! "$LINE" = "" ]; do
  LINKGOESIN="`dirname $LINE`"
  LINKNAMEIS="`basename $LINE`"
  COUNT=`expr $COUNT + 1`
  LINKPOINTSTO="`sed -n "$COUNT p" $1`"
  echo "( cd $LINKGOESIN ; rm -rf $LINKNAMEIS )"
  echo "( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS )"
  COUNT=`expr $COUNT + 1`
  LINE="`sed -n "$COUNT p" $1`"
 done
}

make_excludelist() {
 EXCLUDELIST=""
 DIRALL=`tar ztvf - | awk '{print $1" "$6"/"}' | \
         sed -e 's/\/\///g' | cut -b 1,11- | grep "d " | cut -b 2-`
 for i in $DIRALL ; do
  if [ -d $i ] ; then
   if [ -h $i ] ; then
    EXCLUDELIST="$EXCLUDELIST ${i}/ "
   fi
  fi
 done
}

usage() {
 case "$CMDNAME/$MSGLANG" in
 makepkg/JP)
  cat << EOF
Usage: makepkg package_name.tgz

ȥǥ쥯ȥʲˤƤޤSlackware ߴ *.tgz 
ѥåޤܥå󥯤ޤޤϺ졢
뤿Υ󥹥ȡ륹ץȤޤΥץȤ
"install/doinst.sh" Ǥ

󥹥ȡ˼¹Ԥ륹ץȤ"install/initpkg" Ȥե
֤ȤǤޤ"install/initpkg" 硢Υե
Ƥ "install/doinst.sh" ɲäơ󥹥ȡ˼¹Ԥޤ
εǽϺǶ Plamo Linux ǤΤͭǤ
EOF
  ;;
 makepkg/*)
  cat << EOF
Usage: makepkg package_name.tgz

Makes a Slackware compatible "*.tgz" package containing the contents of
the current and all subdirectories. If symbolic links exist, they will be
removed and an installation script will be made to recreate them later.
This script will be called "install/doinst.sh".

You may put any of your own shell scripts to be executed after installation,
with the file named "install/initpkg". If an "install/initpkg" file exists,
the content of the file will be added to the file "install/doinst.sh" and
will be executed after installation. This feature works on only recent Plamo
Linux.
EOF
  ;;
 */JP)
  cat << EOF
Usage: installpkg package_name
       installpkg -warn package_name
       installpkg -r package_name
       installpkg -m package_name

̾ʲΤ褦 .tgz  .tar.gz եľܻꤷƵưޤ
   installpkg xf_bin.tgz

-warn ץդȥ󥹥ȡ¹Ԥ줿񤭤
եɽޤ

-r ץȤСΥǥ쥯ȥˤǥ쥯ȥ꤫ƵŪ
󥹥ȡ뤹뤳ȤǤޤ
   installpkg -r uucp

ξ硢ߤΥǥ쥯ȥʲ̾ΥѥåƱͤ / ʲإ
ԡ졢/var/log/packages  /var/log/scripts ʲ˥ѥå̾Ȥ
ꤷ"uucp" Ȥե뤬졢¾ Slackware ΥѥåƱ
˥å뤳Ȥǽˤʤޤ

ʲΤ褦ˡ-m ץȤ "*.tgz" ѥå뤳Ȥ
ǽǤ-m ץǤϡѥåϥ󥹥ȡ뤵ޤ
    installpkg -m uucp

ѥåκˤĤơܤ makepkg ޥɤΥإפ⻲ȤƤ

EOF
  ;;
 */*)
  cat << EOF
Usage: installpkg package_name
       installpkg -warn package_name
       installpkg -r package_name
       installpkg -m package_name

Usually used to install a .tgz or .tar.gz package like this:
   installpkg xf_bin.tgz

-warn does not install, but tells you what would be deleted or overwritten.

You can also install recursively from the current location, like this:
   installpkg -r uucp

In this case, everything from the current directory and all subdirectories
will be copied onto / as a package. The appropriate files under
/var/log/packages and /var/log/scripts will be created for a new package
called "uucp", and you will be able to review/remove the package just as 
easily as any other Slackware package. 

To install to a directory other than / (such as /mnt):  
   ROOT=/mnt installpkg package.tgz

It is also possible to make a "*.tgz" package with a command like this:
   installpkg -m uucp
When you use "-m", the package will not be installed.

For more information about making package, see also help message of "makepkg".
EOF
  ;;
 esac
}

# These tests, while by no means exhaustive, will give a usage message
# and exit with a wide range of bad input.

if [ ! "$CMDNAME" = "makepkg" -a "$1" = "-warn" -a -f "$2" ]; then # -warn
 case "$MSGLANG" in
 JP)
  echo "$2 ƤåƤޤ..."
  ;;
 *)
  echo "Scanning the contents of $2..."
  ;;
 esac
 echo
 mkdir -p /tmp/scan
 ( cd /tmp/scan ; tar xzf - install/doinst.sh ) < $2 2> /dev/null 
 if [ -r /tmp/scan/install/doinst.sh ]; then
  if link_script /tmp/scan/install/doinst.sh | fgrep 'rm -rf' > /dev/null 2>&1 ; then
   link_script /tmp/scan/install/doinst.sh | fgrep 'rm -rf' > /tmp/scan/install/delete
   case "$MSGLANG" in
   JP)
    cat << EOF
ʲ˼ʬϥܥå󥯤뤿˴ˡ־õפޤ
("rm -rf" ¹Ԥޤ)õоݤϥեǥ쥯ȥΤǤ

Υѥå򥤥󥹥ȡ뤹ˡõʬˤɬפʥե
˺줺˥ХååפƤƤ
EOF
    ;;
   *)
    cat << EOF
The following locations will be completely WIPED OUT to allow symbolic links
to be made (We are talking "rm -rf"). These locations may be files, or entire
directories.

Be sure you have backed up anything at these locations that you want to
save before you install this package:
EOF
    ;;
   esac
   echo
   cat /tmp/scan/install/delete | cut -f 3,7 -d ' ' | tr ' ' '/'
   echo
  fi
  if [ -d /tmp/scan ]; then
   ( cd /tmp/scan ; rm -rf install ) 2> /dev/null
   ( cd /tmp ; rmdir scan ) 2> /dev/null
  fi
 fi
 case "$MSGLANG" in
 JP)
  cat << EOF
ʲΥեϤΥѥå򥤥󥹥ȡ뤹Ⱦ񤭤ޤ

餬פʥեǤʤȤ򤢤餫ǧƤƤ
EOF
  ;;
 *)
  cat << EOF
The following files will be overwritten when installing this package.

Be sure they are not important before you install this package:
EOF
  ;;
 esac
 echo
 ( tar tzvvf - ) < $2 | fgrep -v 'drwx'
 echo
 case "$MSGLANG" in
 JP)
  echo "ٹ𤷤ޤ"
  ;;
 *)
  echo "You have been warned."
  ;;
 esac
 exit
fi

if [ "$CMDNAME" = "makepkg" ]; then
 if [ ! $# = 1 -a ! $# = 0 ]; then
  usage;
  exit
 fi
 if [ $# = 0 ]; then
  usage;
  echo
  case "$MSGLANG" in
  JP)
   echo "ѥå̾ꤵƤޤ"
   echo -n "ѥå̾ϤƤ: "
   ;;
  *)
   echo "You have not provided a name for this package."
   echo -n "What would you like to call it? "
   ;;
  esac
  read PKGNAME;
  echo
 else
  PKGNAME=$1
 fi
 set -- -m `basename $PKGNAME .tgz`
else
 if [ $# = 2 -a ! "$1" = "-m" -a ! "$1" = "-r" -a ! -r "$1" ]; then
  usage;
  exit
 elif [ $# = 0 ]; then
  usage;
  exit
 fi
 if [ "$1" = "-m" ]; then
  PKGNAME="$2.tgz"
  set -- -m `basename $2`
 fi
fi
if [ $# = 1 -a ! -r "$1" ]; then
 case "$MSGLANG" in
 JP)
  echo "ѥåĤޤ: $1"
  ;;
 *)
  echo "Package not found: $1"
  ;;
 esac
 echo
 usage;
 exit
fi

if [ "$1" = "-r" -o "$1" = "-m" ]; then
 if [ -r "$ROOT/var/log/packages/$2" -a ! "$1" = "-m" ]; then
  case "$MSGLANG" in
  JP)
   cat << EOF
 "$2" Ȥѥå¸ߤޤƱ̾Υѥå򥤥󥹥ȡ뤹
ˤ pkgtool ȤäơŤѥåƤƤ
ʤȡŤǥåե뤬񤭴Ƥޤᡢưǥե
󥯤ʤȤʤʤޤ

EOF
   echo -n "λޤ([y]es, [n]o)? "
   ;;
  *)
   cat << EOF
Hey - there is already a package named "$2"! You really should remove that
before attempting to install another package with that name, since the old
indexes for that package will be overwritten and you will have to remove
any leftover files and links manually.

EOF
   echo -n "Do you want to exit ([y]es, [n]o)? "
   ;;
  esac
  read EXIT;
  echo
  if [ "$EXIT" = "y" ]; then
   exit
  fi
 fi
 if find . -type l -print | grep '.*' > /dev/null; then
  case "$MSGLANG" in
  JP)
   cat << EOF
ѥå˥ܥå󥯤ޤޤƤޤ

Υѥå˴ޤޤƤ륷ܥå󥯤 "install/doinst.sh" 
󥹥ȡ륹ץѤѴޤ? Ѵ硢ä󥷥
å󥯤Ϻޤѥå縵Υǥ쥯ȥ
    sh install/doinst.sh
ȤСñǤޤ

ѥå硢ܥå󥯤ϥץȤѴ뤳Ȥ򤪴
ᤷޤ

EOF
   echo -n "ܥå󥯤Ѵޤ([y]es, [n]o)? "
   ;;
  *)
   cat << EOF
There are symbolic links in this package.

Do you want to convert the symbolic links in this package into an installation
script named "/install/doinst.sh"? If you do, the symbolic links will be
removed. You can get them back easily, though, by typing
    sh install/doinst.sh
in the root directory of the package.

It is recommended that you convert your links for best results.

EOF
   echo -n "Convert links to an install script ([y]es, [n]o)? "
   ;;
  esac
  read CONVERT_LINKS;
  echo
 else
  CONVERT_LINKS=n
 fi
 if [ "$CONVERT_LINKS" = "y" ]; then
  case "$MSGLANG" in
  JP)
   echo "ܥå󥯤Ĵ٤Ƥޤ..."
   ;;
  *)
   echo "Having a good look at your symbolic links..."
   ;;
  esac
  echo
  (
   for l in `find . -type l -print | cut -b3-`; do
    target=`readlink $l`
    echo "$l -> $target"
    echo $l >&2
    echo $target >&2
   done
  ) 2> /tmp/iNsT-a.$$
  echo
  if [ ! -d install ]; then
   mkdir install
  fi
  if [ -r install/doinst.sh ]; then
   case "$MSGLANG" in
   JP)
    cat << EOF
Ǥ "install/doinst.sh" ȤץȤ¸ߤޤʲ 3 
бǽǤ

1 - ¸ߤƤ륹ץȤκǸ˥ܥå󥯤Ѵ뤿
    ɤղä롣
2 - ¸ΥץȤơΤ֤
3 - ߤƽλ롣

EOF
    echo -n "ɤޤ(1, 2, 3)? "
    ;;
   *)
    cat << EOF
You already have an installation script named "install/doinst.sh". You have
3 choices:

1 - append the script code to create these links (if any) to the end of
    the existing installation script.
2 - erase the existing installation script, and replace it with the code
    to create these new links.
3 - Abort! Exit this script.

EOF
    echo -n "Which choice would you like (1, 2, 3)? "
    ;;
   esac
   read DOINST;
   echo
   if [ "$DOINST" = "1" ]; then
    case "$MSGLANG" in
    JP)
     echo "¸ΥץȤɲäƤޤ..."
     ;;
    *)
     echo "Appending to existing script..."
     ;;
    esac
    echo
    make_install_script /tmp/iNsT-a.$$ | tee --append install/doinst.sh
   elif [ "$DOINST" = "2" ]; then
    case "$MSGLANG" in
    JP)
     echo "¸ΥץȤ֤ޤ..."
     ;;
    *)
     echo "Replacing existing script..."
     ;;
    esac
    echo
    make_install_script /tmp/iNsT-a.$$ | tee install/doinst.sh
   else
    rm -f /tmp/iNsT-a.$$
    exit
   fi
  else # no script exists:
   case "$MSGLANG" in
   JP)
    echo "󥹥ȡѥץȤƤޤ..."
    ;;
   *)
    echo "Making your install script..."
    ;;
   esac
   echo
   make_install_script /tmp/iNsT-a.$$ | tee install/doinst.sh
  fi # is there already a script?
  echo
  rm -f /tmp/iNsT-a.$$
  case "$MSGLANG" in
  JP)
   echo "ܥå󥯤Ƥޤ:"
   ;;
  *)
   echo "Removing the symbolic links:"
   ;;
  esac
  echo
  find . -type l -print -exec rm {} \;
  echo
 fi # make install script?
 if [ -r install/initpkg ]; then
  case "$MSGLANG" in
  JP)
   cat << EOF
"install/initpkg" ȤץȤ¸ߤޤ

ΥץȤƤ򥤥󥹥ȡѥץȤϿ󥹥ȡ뽪λ
˽ȤƼ¹Ԥ褦ˤޤǤ˽Ͽ
ˤϾ񤭤ޤ

ʤνԤˤϡ¾Υѥå򥤥󥹥ȡ뤷
䡢ƥεư˺ټ¹Ԥޤ

EOF
   ;;
  *)
   cat << EOF
You have an script named "install/initpkg".

Now register the contents of this script into an installation sctipt, and
it will be executed after installation as an initialization process. If
an installation sctipt already has an initialization process, it will be
overwritten.

If the initialization process fails, it will be re-executed after another
package will be installed, or when the system starts up.

EOF
   ;;
  esac
  if [ ! -d install ]; then
   mkdir install
  fi
  if [ -r install/doinst.sh ]; then
   link_script install/doinst.sh > /tmp/iNsT-i.$$
   mv /tmp/iNsT-i.$$ install/doinst.sh
  fi
  echo "#%% begin initialize $2" >> install/doinst.sh
  cat install/initpkg >> install/doinst.sh
  echo "#%% end" >> install/doinst.sh
 fi
 # Install the package:
 if [ "$1" = "-r" ]; then # install
  if [ -r install/doinst.sh ]; then # hey, did we do anything?
   # Put the backup of the script in place
   cp install/doinst.sh $ROOT/var/log/scripts/$2
   chmod 755 $ROOT/var/log/scripts/$2
  fi
  # Write the filelist out:
  echo "PACKAGE NAME:     $2" > $ROOT/var/log/packages/$2
  echo "FILE LIST:" >> $ROOT/var/log/packages/$2
  echo "./" >> $ROOT/var/log/packages/$2
  find . -type f -print | grep -v '^\./install/initpkg$' | cut -b3- >> $ROOT/var/log/packages/$2
  # Copy the stuff into place:
  case "$MSGLANG" in
  JP)
   echo "ѥå $ROOT/ ʲ˥󥹥ȡ..."
   ;;
  *)
   echo "Installing your new package onto $ROOT/..."
   ;;
  esac
  cp -a . $ROOT/
  (
   cd $ROOT/
   rm install/initpkg > /dev/null 2>&1
   if [ ! -d var/log/initpkg ]; then
    mkdir -p var/log/initpkg
   fi
   if [ -r install/doinst.sh ]; then
    link_script install/doinst.sh | sh
    init_script install/doinst.sh > var/log/initpkg/$2
    sh var/log/initpkg/$2 && rm var/log/initpkg/$2
    rm install/doinst.sh
   fi
   rmdir install > /dev/null 2>&1
   # try initpkg script for the other packages again...
   for p in `ls -1tr var/log/initpkg`; do
    sh var/log/initpkg/$p && rm var/log/initpkg/$p
   done
  )
 elif [ "$1" = "-m" ]; then
  case "$MSGLANG" in
  JP)
   echo "$PKGNAME ..."
   ;;
  *)
   echo "Making $PKGNAME..."
   ;;
  esac
  # tar --create --gzip --exclude $PKGNAME --exclude initpkg --file $PKGNAME `ls -f | tail +3`
  tar --create --gzip --exclude $PKGNAME --exclude initpkg --file $PKGNAME `ls -a | sed '1,2d'`
 fi
else # we're just going to try to install each argument as a package:
 for package in $* ; do
  case "$MSGLANG" in
  JP)
   echo "$package ѥå򥤥󥹥ȡ..."
   ;;
  *)
   echo "Installing package $package..."
   ;;
  esac
  # This stuff looks for symbolic links that also belong to other packages and
  # leaves them in place. That way, if you have a couple package pointing to
  # /usr/include on the cdrom and delete one, you'll still have include files.
  shortname=`basename $package .tgz`
  packagedir="`dirname $package`"
  fullpath="`cd $packagedir ; pwd`/`basename $package`"
  # This is the base package name, used for grepping tagfiles and descriptions:
  packagebase="`package_name $shortname`"

  ADM_DIR="$ROOT/var/log" 
  TMP=/tmp
  if [ -r $ADM_DIR/scripts/$packagebase ]; then
   link_script $ADM_DIR/scripts/$packagebase | fgrep 'rm -rf' | sort -u > $TMP/del_link_list
   if [ ! -d $ADM_DIR/removed_scripts ]; then
    mkdir -p $ADM_DIR/removed_scripts
   fi
   mv $ADM_DIR/scripts/$packagebase $ADM_DIR/removed_scripts > /dev/null 2>&1    
   ( cat $ADM_DIR/scripts/* ; tar xzOf $package install/doinst.sh 2> /dev/null ) | link_script - | fgrep 'rm -rf' | sort -u > $TMP/required_links
   comm -23 $TMP/del_link_list $TMP/required_links > $TMP/delscript
   ( cd $ROOT/ ; sh $TMP/delscript )
   rm -f $TMP/del_link_list $TMP/required_links $TMP/delscript
  fi
  # Write the package file database entry and install the package:
  echo "PACKAGE NAME:     $shortname" > $ROOT/var/log/packages/$packagebase
  COMPRESSED=`gzip -l $package | fgrep -v uncompressed_name | cut -b0-19`
  UNCOMPRESSED=`gzip -l $package | fgrep -v uncompressed_name | cut -b20-39`
  echo "COMPRESSED PACKAGE SIZE:     `expr $COMPRESSED / 1024` K" >> $ROOT/var/log/packages/$packagebase
  echo "UNCOMPRESSED PACKAGE SIZE:     `expr $UNCOMPRESSED / 1024` K" >> $ROOT/var/log/packages/$packagebase
  echo "PACKAGE LOCATION: $fullpath" >> $ROOT/var/log/packages/$packagebase
  echo "PACKAGE DESCRIPTION:" >> $ADM_DIR/packages/$packagebase

  # Locate package description file:
  DESCRIPTION="/dev/null"
  # First check the usual locations outside the package, since this is faster:
  for file in $packagedir/disk* $packagedir/package_descriptions $packagedir/$shortname.txt $packagedir/$packagebase.txt ; do
    # echo "file:$file, packagebase:$packagebase, shortname:$shortname"
    # sleep 5
    if grep "^$packagebase:" "$file" 1> /dev/null 2> /dev/null ; then
      DESCRIPTION="$file"
    elif grep "^$shortname:" "$file" 1> /dev/null 2> /dev/null ; then
      DESCRIPTION="$file"
    fi
  done
  # If we still don't have anything, look inside the package.  This requires a costly untar.
  if [ "$DESCRIPTION" = "/dev/null" ]; then
    mkdir -p $TMP/scan$$
    ( cd $TMP/scan$$ ; $TAR xzf - install ) < $package 2> /dev/null
    if grep "^$packagebase:" "$TMP/scan$$/install/plamo-desc" 1> /dev/null 2> /dev/null ; then
      DESCRIPTION="$TMP/scan$$/install/plamo-desc"
    elif grep "^$shortname:" "$TMP/scan$$/install/plamo-desc" 1> /dev/null 2> /dev/null ; then
      DESCRIPTION="$TMP/scan$$/install/plamo-desc"
    fi
  fi

  # Simple package integrity check:
  if [ ! -f $package ]; then
    EXITSTATUS=4
    if [ "$MODE" = "install" ]; then
      echo "$package 򥤥󥹥ȡǤޤ󡥥ѥåʥեǤϤޤ"
    fi
    continue;
  fi
  gzip -l $package 1> /dev/null 2> /dev/null
  if [ ! "$?" = "0" ]; then
    EXITSTATUS=2 # failed gzip -l
    if [ "$MODE" = "install" ]; then
      echo "$package Ƥ褦Ǥ ('gzip -l $package' ǤޤǤ)"
    fi
    continue;
  fi

  # Collect the package information into a temp file:
  COMPRESSED=`gzip -l $package | grep -v uncompressed_name | crunch | cut -f 1 -d ' '`
  UNCOMPRESSED=`gzip -l $package | grep -v uncompressed_name | crunch | cut -f 2 -d ' '`
  COMPRESSED="`expr $COMPRESSED / 1024` K"
  UNCOMPRESSED="`expr $UNCOMPRESSED / 1024` K"
#  MD5SUM=`md5sum $package | cut -f 1 -d ' '`
  cat $DESCRIPTION | grep "^$packagebase:" | cut -f 2- -d : | cut -b2- 1> $TMP/tmpmsg$$ 2> /dev/null
  if [ "$shortname" != "$packagebase" ]; then
    cat $DESCRIPTION | grep "^$shortname:" | cut -f 2- -d : | cut -b2- 1>> $TMP/tmpmsg$$ 2> /dev/null
  fi
  # Adjust the length here.  This allows a slack-desc to be any size up to 13 lines instead of fixed at 11.
  LENGTH=`cat $TMP/tmpmsg$$ | wc -l`
  while [ $LENGTH -lt 12 ]; do
    echo >> $TMP/tmpmsg$$
    LENGTH=`expr $LENGTH + 1`
  done
  echo "Size: Compressed: $COMPRESSED, uncompressed: $UNCOMPRESSED." >> $TMP/tmpmsg$$
  # For recent versions of dialog it is necessary to add \n to the end of each line
  # or it will remove repeating spaces and mess up our careful formatting:
  cat << EOF > $TMP/controlns$$
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
EOF
  paste -d "" $TMP/tmpmsg$$ $TMP/controlns$$ > $TMP/pasted$$
  rm -f $TMP/controlns$$
  mv $TMP/pasted$$ $TMP/tmpmsg$$
  # Emit information to the console:
  if [ "$MODE" = "install" ]; then
    if [ "$PMSG" = "" ]; then
      echo "ѥå $shortname Υ󥹥ȡ... "
    else
      echo "ѥå $shortname ($PMSG) Υ󥹥ȡ... "
    fi
    echo "PACKAGE DESCRIPTION:"
    cat $DESCRIPTION | grep "^$packagebase:" | uniq
    if [ "$shortname" != "$packagebase" ]; then
      cat $DESCRIPTION | grep "^$shortname:" | uniq
    fi
  elif [ "$MODE" = "infobox" -a ! "$PRIORITY" = "SKP" ]; then # install non-SKP infobox package
    dialog --title "Installing package ==>$shortname<== $PMSG" --infobox "`cat $TMP/tmpmsg$$`" 0 0
  elif [ "$MODE" = "menu" -a "$PRIORITY" = "ADD" -a ! "$ALWAYSASK" = "yes" ]; then # ADD overrides menu mode unless -ask was used
    dialog --title "Installing package ==>$shortname<== $PMSG" --infobox "`cat $TMP/tmpmsg$$`" 0 0
  elif [ "$MODE" = "menu" -a "$USERPRIORITY" = "ADD" ]; then # install no matter what $PRIORITY
    dialog --title "Installing package ==>$shortname<== $PMSG" --infobox "`cat $TMP/tmpmsg$$`" 0 0
  elif [ "$MODE" = "menu" -a "$PRIORITY" = "SKP" -a ! "$ALWAYSASK" = "yes" ]; then # SKP overrides menu mode unless -ask used
    rm -f $TMP/tmpmsg$$
    continue # next package
  elif [ "$MODE" = "infobox" -a "$PRIORITY" = "SKP" -a ! "$ALWAYSASK" = "yes" ]; then # SKP overrides infobox mode, too
    rm -f $TMP/tmpmsg$$
    continue
  else # we must need a full menu:
    dialog --title "Package Name: ==>$shortname<== $PMSG" --menu "`cat $TMP/tmpmsg$$`" 23 72 4 \
    "Yes" "$shortname 򥤥󥹥ȡ뤹" \
    "No" "$shortname ϥ󥹥ȡ뤷ʤ" \
    "Quit" "եȥ󥹥ȡ" 2> $TMP/reply$$
    if [ ! $? = 0 ]; then
      echo "No" > $TMP/reply$$
    fi
    REPLY="`cat $TMP/reply$$`"
    rm -f $TMP/reply$$ $TMP/tmpmsg$$
    if [ "$REPLY" = "Quit" ]; then
      exit 99 # EXIT STATUS 99 = ABORT!
    elif [ "$REPLY" = "No" ]; then
      continue # skip the package
    fi
  fi

  # Test tarball integrity, and make sure we're not installing files on top of existing symbolic links:
  $TAR tzf $package 1> $TMP/tmplist$$ 2> /dev/null
  TARERROR=$?
  if [ ! "$TARERROR" = "0" ]; then
    EXITSTATUS=1 # tar file corrupt
    if [ "$MODE" = "install" ]; then
      echo "$package 򥤥󥹥ȡǤޤ(tar ޥɤ顼 $TARERROR ֤ޤ)"
    fi
    rm -f $TMP/tmplist$$
    continue
  fi
  # cat $TMP/tmplist$$ | grep -v "/$" | while read file ; do
  #   if [ -L "$ROOT/$file" ]; then
  #     rm -f "$ROOT/$file"
  #   fi
  # done
  rm -f $TMP/tmplist$$

  # Write the package file database entry and install the package:
  echo "PACKAGE NAME:     $shortname" > $ADM_DIR/packages/$packagebase
  echo "COMPRESSED PACKAGE SIZE:     $COMPRESSED" >> $ADM_DIR/packages/$packagebase
  echo "UNCOMPRESSED PACKAGE SIZE:     $UNCOMPRESSED" >> $ADM_DIR/packages/$packagebase
  echo "PACKAGE LOCATION: $package" >> $ADM_DIR/packages/$packagebase
#  echo "PACKAGE MD5SUM: $MD5SUM" >> $ADM_DIR/packages/$packagebase
  echo "PACKAGE DESCRIPTION:" >> $ADM_DIR/packages/$packagebase
  cat $DESCRIPTION | grep "^$packagebase:" >> $ADM_DIR/packages/$packagebase 2> /dev/null
  if [ "$shortname" != "$packagebase" ]; then
    cat $DESCRIPTION | grep "^$shortname:" >> $ADM_DIR/packages/$packagebase 2> /dev/null
  fi
  rm -f /tmp/ExClUdE.$$
  touch /tmp/ExClUdE.$$
  (
   cd $ROOT/
   make_excludelist
   for i in $EXCLUDELIST ; do
    echo $i
   done
  ) < $package >> /tmp/ExClUdE.$$
  echo "FILE LIST:" >> $ADM_DIR/packages/$packagebase
  ( cd $ROOT/ ; tar -xzlUpvf - --exclude-from /tmp/ExClUdE.$$ ) < $package >> $TMP/$shortname 2> /dev/null
  rm -f /tmp/ExClUdE.$$
  ##echo "FILE LIST:" >> $ADM_DIR/packages/$packagebase
  ##( cd $ROOT/ ; $TAR --overwrite -xzlUpvf - ) < $package >> $TMP/$shortname 2> /dev/null
  # if [ "`cat $TMP/$shortname | grep '^./' | wc -l | tr -d ' '`" = "1" ]; then
  #  # Good.  We have a package that meets the Slackware spec.
  #  cat $TMP/$shortname >> $ADM_DIR/packages/$shortname
  #else
  #  # Some dumb bunny built a package with something other than makepkg.  Bad!
  #  # Oh well.  Bound to happen.  Par for the course.  Fix it and move on...
  #  echo './' >> $ADM_DIR/packages/$shortname
  #  cat $TMP/$shortname | grep -v '^./$' | cut -b3- >> $ADM_DIR/packages/$shortname
  # fi
  cat $TMP/$shortname >> $ADM_DIR/packages/$packagebase
  rm -f $TMP/$shortname

  if [ -x /sbin/ldconfig ]; then
    /sbin/ldconfig
  fi

  (
   cd $ROOT/
   if [ ! -d var/log/initpkg ]; then
    mkdir -p var/log/initpkg
   fi
   if [ -r install/doinst.sh ]; then
    case "$MSGLANG" in
    JP)
     echo "$package ѤΥ󥹥ȡ륹ץȤ¹..."
     ;;
    *)
     echo "Executing install script for $package..."
     ;;
    esac
    link_script install/doinst.sh | sh
    init_script install/doinst.sh > var/log/initpkg/$packagebase
    sh var/log/initpkg/$packagebase && rm var/log/initpkg/$packagebase
   fi
   # try initpkg script for the other packages again...
   # for p in `ls -1tr var/log/initpkg`; do
   #  sh var/log/initpkg/$p && rm var/log/initpkg/$p
   # done
  )
  # Clean up the mess...
  if [ -d $ROOT/install ]; then
   cp $ROOT/install/doinst.sh $ROOT/var/log/scripts/$packagebase
   chmod 755 $ROOT/var/log/scripts/$packagebase
   (cd $ROOT/install ; rm -r -f doin* > /dev/null 2>&1 )
   rmdir $ROOT/install > /dev/null 2>&1
  fi
  echo
 done
fi
echo
