#!/bin/bash
#
# Copyright 2002, Kent Robotti <robotti@godmail.com> (1-22-2002)
#    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.
#

if [ ! `whoami` = "root" ]; then
echo "[`whoami`] You need to run this script as root."
exit 1 
fi

   DIALOG=`type -path dialog`
   if [ ! -x "$DIALOG" ]; then
     echo "This script requires the \`dialog' program to run."
     exit 1
   else
   if [ "$DISPLAY" = ":0.0" ]; then
     XDIALOG=`type -path Xdialog`
   if [ -x "$XDIALOG" ]; then
     DIALOG=$XDIALOG
   fi
   fi
   fi

LOG_DIR=/var/log
if [ ! -d $LOG_DIR ]; then
  mkdir -p $LOG_DIR || exit 1
  chmod 755 $LOG_DIR
  chown root.root $LOG_DIR
fi

TMP=$LOG_DIR/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP || exit 1
  chmod 700 $TMP
fi

#############################################################################
   extract_links() {
    sed -n 's,( *cd \([^ ;][^ ;]*\) *; *rm -rf \([^ )][^ )]*\) *) *$,\1/\2,p'
   }

trigger() {
  if grep "^\.\/$" $LOG_DIR/packages/$1 1> /dev/null 2>&1; then
    TRIGGER="^\.\/$"
  elif grep "FILE LIST:" $LOG_DIR/packages/$1 1> /dev/null 2>&1; then
    TRIGGER="FILE LIST:"
  else
    exit
  fi
}

find_rpm_dir() {
       for rpmdir in /usr/src/rpm/RPMS/$arch /usr/src/redhat/RPMS/$arch \
       /usr/src/RPM/RPMS/$arch /usr/src/Mandrake/RPMS/$arch \
       /usr/src/packages/RPMS/$arch /usr/src/mandrake/RPMS/$arch ; do
	if [ -d $rpmdir ]; then
          rpm_dir=$rpmdir
	  break;
	fi
	done
	if [ "$rpm_dir" = "" ]; then
	if [ -x "`type -path strings`" ]; then
	  rpm=`type -path rpm`
	  rpm_dir="`strings $rpm | grep /usr/src/`/RPMS/$arch"
          mkdir -p $rpm_dir
        fi
     fi
}

crunch() {
while read space ; do
  echo $space
done    
}

not_found() {
if [ -s $TMP/not_found ]; then
unset skip
if [ ! -s $TMP/$package_name.found ]; then
cat << EOF > $TMP/tmpmsg

These items are listed in the package, but i can't find
them on the system. There's nothing left to create a
package from.

`cat $TMP/not_found`

EOF
skip=yes
else
cat << EOF > $TMP/tmpmsg

These items are listed in the package, but i can't find
them on the system.

`cat $TMP/not_found`

EOF
fi
   dialog --title "NOT FOUND" --textbox "$TMP/tmpmsg" 20 76 

if [ "$skip" = "yes" ]; then
unset skip
continue;
fi

cat << EOF > $TMP/tmpmsg

Do you want to cancel the creation of this package?

EOF
     dialog --title "CANCEL" --yesno "`cat $TMP/tmpmsg`" 7 60 2> /dev/null

   if [ $? = 0 ]; then
     rm -f $TMP/$package_name $TMP/not_found
     continue
   fi 
 fi
}

docs() {
rm -f $TMP/docs
for file in ABOUT ABOUT-NLS ANNOUNCE* Artistic ARTISTIC AUTHORS *BUGS* CHANGES \
CONFIGURATION* *COPYING* *COPYRIGHT* CREDITS ChangeLog *faq* Changelog changelog \
CHANGELOG CONTRIBUTORS *FAQ* *faq* FEATURES FILES HACKING History HISTORY \
*HOWTO* INSTALL* LICENSE LSM MANIFEST NEWS *README* *Readme* *readme* SITES \
*RELEASE* RELNOTES THANKS TIPS *TODO* VERSION GPL License *.TXT *.txt *.lsm \
*.htm *.html ; do
if [ -f "$file" ]; then
echo "$file" >>$TMP/docs
fi
done

if [ -s "$TMP/docs" ]; then
echo "dialog --title \"DOCS\" --checklist \"These are some docs from this directory. \\n\\n\\
If you want to include any of them in the package, \\n\\
press the space key and tag them with X.\" 20 60 10 \\" > $TMP/tmpmsg

cat $TMP/docs | sort -u | while read file ; do
  doc_size=`ls -lh $file | awk '{print $5}'`  
  echo "$file" | sed -e 's/.*/"&" "('$doc_size')" off \\/' >> $TMP/tmpmsg
done
echo "2> $TMP/docs" >> $TMP/tmpmsg
. $TMP/tmpmsg

if [ $? = 0 ]; then 
if [ -s "$TMP/docs" ]; then
cat << EOF > $TMP/tmpmsg

Please enter a name in the box for the doc directory. 

The doc directory is usually named after the package.

If the package is named 'fooboo' then ('fooboo' or
'fooboo-1.0.0' name and version number) would be used.

If you leave the box blank the name
'$3' will be used.

EOF

   dialog --title "DOC DIRECTORY NAME" --inputbox "`cat $TMP/tmpmsg`" 17 60 2> $TMP/doc_dir
   if [ -s "$TMP/doc_dir" ]; then
     doc_dir="$2/`cat $TMP/doc_dir`" 
   else
     doc_dir="$2/$3"       
   fi

  dialog --title "Copying docs to:" --infobox "$doc_dir" 3 50
  sleep 3
  mkdir -p $TMP/$1$doc_dir
  for file in `cat $TMP/docs | tr -d \"` ; do
    cp $file $TMP/$1$doc_dir
    chmod 644 $TMP/$1$doc_dir/$file
    chown root.root $TMP/$1$doc_dir/$file
  done
 fi 
fi
fi
}

pkg_name() {
version=`echo $package | grep "[-._][0-9]" | sed "s/.*[a-z A-Z][0-9][-._]//g" | \
sed "s/.*[a-z A-Z][-._]//g" | sed "s/.i[3-6]86//g"`

package=`echo $package | sed "s/[-._][0-9].*//g"`

cat << EOF > $TMP/tmpmsg

Please enter a name in the box for the package. 

If you leave the box blank the name
'$package' will be used.

The name should not include the version number
of the package.

For example 'fooboo' not 'fooboo-1.0.0'.
EOF
   dialog --title "PACKAGE NAME" --inputbox "`cat $TMP/tmpmsg`" 17 52 2> $TMP/pkgname
 
   pkgname="`cat $TMP/pkgname`" 

   if [ ! "$pkgname" = "" ]; then
     package_name=$pkgname
   else
     package_name=$package
   fi 

if [ "$version" = "" ]; then
version=0.0.0
fi

cat << EOF > $TMP/tmpmsg
 
Please enter a version number for the package. 
Example: 2.1.0
 
If you leave the box blank the version
'$version' will be used.

EOF
   dialog --title "PACKAGE VERSION" --inputbox "`cat $TMP/tmpmsg`" 13 52 2> $TMP/version
  
   new_version="`cat $TMP/version`" 
 
   if [ ! "$new_version" = "" ]; then
     version=$new_version
   else
     version=$version
   fi 
}

find_links() {
rm -f $TMP/links
if [ -x "`type -path awk`" ]; then
find * -type l -exec ls -l {} \; | awk '{print $9,$11}' >> $TMP/links
else
find * -type l -exec ls -l {} \; | cut -d ' ' -f 27,29 >> $TMP/links
fi
}

recreate_links() {
     find_links
     if [ -s $TMP/links ]; then
     cat $TMP/links | grep -E -v "\.bz2$|\.gz$" | while read file ; do
     dir=`echo $file | cut -d' ' -f1`
     target=`echo $file | cut -d' ' -f2`
     link=`echo $file | cut -d' ' -f1`
     ( cd `dirname $dir` &&
     rm -f `basename $link`
     ln -sf `basename $target`.gz `basename $link`.gz )
     done
     fi
}

make_slackware_compatible() {
     if [ -d usr/share/man -o -d usr/local/man ]; then
     mkdir -p usr/man
     cp -a usr/share/man/* usr/local/man/* usr/man 2>/dev/null 
     rm -rf usr/share/man usr/local/man
     rmdir usr/share usr/local 2>/dev/null
     fi
     if [ -d usr/share/doc -o -d usr/local/doc ]; then
     mkdir -p usr/doc
     cp -a usr/share/doc/* usr/local/doc/* usr/doc 2>/dev/null 
     rm -rf usr/share/doc usr/local/doc
     rmdir usr/share usr/local 2>/dev/null
     gzip -d usr/doc/*/*.gz 2>/dev/null
     if [ "$1" = "deb" -a ! "$2" = "" ]; then
     ( cd usr/doc && mv `basename *` "`basename *`-$2" 2>/dev/null )
     fi
     fi
     if [ -d usr/share/info -o -d usr/local/info ]; then
     mkdir -p usr/info
     cp -a usr/share/info/* usr/local/info/* usr/info 2>/dev/null
     rm -rf usr/share/info usr/local/info
     rmdir usr/share usr/local 2>/dev/null
     fi
     if [ -d usr/man ]; then
     ( cd usr/man ; 
     recreate_links
     for file in `find * 2>/dev/null -type f | grep -E -v "\.bz2$|\.gz$"` 
     do
     gzip -f $file 2>/dev/null
     done )       
     fi
     if [ -d usr/X11R6/man ]; then
     ( cd usr/X11R6/man ; 
     recreate_links
     for file in `find * 2>/dev/null -type f | grep -E -v "\.bz2$|\.gz$"` 
     do
     gzip -f $file 2>/dev/null
     done )       
     fi
     if [ -d usr/info ]; then
     ( cd usr/info ; 
     for file in `find * 2>/dev/null -type f | grep -E -v "\.bz2$|\.gz$|^dir$"` 
     do
     gzip -f $file 2>/dev/null
     done )       
     fi

     if [ "$1" = "rpm" -a ! "$2" = "" ]; then
       ( cd usr/doc && mv `basename *` "`basename *`-$2" 2>/dev/null )
     fi
     
     if [ -d etc ]; then
     rm -rf etc/*.d etc/rc.boot etc/default etc/logcheck etc/cron.* etc/sysconfig
     rmdir etc 2>/dev/null
     fi
     rm -rf usr/share/doc-base usr/lib/menu
}
# End of make_slackware_compatible.

make_debian_rpm_compatible() {
     if [ -d usr/man -o -d usr/local/man ]; then
     mkdir -p usr/share/man
     cp -a usr/man/* usr/local/man/* usr/share/man 2>/dev/null 
     rm -rf usr/man usr/local/man
     rmdir usr/local 2>/dev/null
     fi
     if [ -d usr/doc -o -d usr/local/doc ]; then
     mkdir -p usr/share/doc
     cp -a usr/doc/* usr/local/doc/* usr/share/doc 2>/dev/null 
     rm -rf usr/doc usr/local/doc
     rmdir usr/local 2>/dev/null
     fi
     if [ -d usr/info -o -d usr/local/info ]; then
     mkdir -p usr/share/info
     cp -a usr/info/* usr/local/info/* usr/share/info 2>/dev/null
     rm -rf usr/info usr/local/info
     rmdir usr/local 2>/dev/null
     fi
     if [ -d usr/share/man ]; then
     ( cd usr/share/man ; 
     recreate_links
     for file in `find * 2>/dev/null -type f | grep -E -v "\.bz2$|\.gz$"` 
     do
     gzip -f $file 2>/dev/null
     done )       
     fi
     if [ -d usr/X11R6/man ]; then
     ( cd usr/X11R6/man ; 
     recreate_links
     for file in `find * 2>/dev/null -type f | grep -E -v "\.bz2$|\.gz$"` 
     do
     gzip -f $file 2>/dev/null
     done )       
     fi
     if [ -d usr/share/info ]; then
     ( cd usr/share/info ; 
     for file in `find * 2>/dev/null -type f | grep -E -v "\.bz2$|\.gz$|^dir$"` 
     do
     gzip -f $file 2>/dev/null
     done )       
     fi
}

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

mkpkg() {
find_links
if [ -s $TMP/links ]; then
  mkdir -p install
  make_install_script $TMP/links >> install/doinst.sh
  find . -type l -exec rm -f {} \;
  chmod 644 install/doinst.sh
  chown root.root install/doinst.sh
fi
 if [ "$2" = "y" ]; then
   find . -type d -exec chmod 755 {} \; 
   find . -type d -exec chown root.root {} \;
   find . -type f -exec chown root.root {} \;
 fi
tar -cpf $1 * 2>$TMP/error || error
gzip -9 $1 2>$TMP/error || error
}

    if [ "`tty | cut -b9`" = "p" ]; then
	TTY="`tty 2>/dev/null | cut -b10-11 2>/dev/null`" 
	NUM=$(( $TTY + 1 ))
    elif tty | grep -q "/vc/" ; then
	TTY="`tty 2>/dev/null | cut -b9-10 2>/dev/null`" 
	NUM="$TTY"
    elif tty | grep -q "/pts/" ; then
	NUM=?
    else
	TTY="`tty 2>/dev/null | cut -b9-10 2>/dev/null`" 
	NUM="$TTY"
    fi

pause() {
cat << EOF > $TMP/pause

You can add or remove something from the package if you wish.

I'll just wait here while you change consoles Alt-F?, and make
changes in the '$1'
directory.

The current console is 'Alt-F$NUM'. 

Press enter when ready to continue.
EOF

   dialog --title "PAUSE" --msgbox "`cat $TMP/pause`" 15 70
}

strip_binaries() {

if [ -x "`type -path strip`" ]; then 
cat << EOF > $TMP/strip

Do you want to strip the binaries in the package?
This is generally a good idea to make them smaller. 

EOF

   dialog --title "STRIP BINARIES" --yesno "`cat $TMP/strip`" 8 60 
   if [ $? = 0 ]; then
    dialog --title "Striping binaries..." --infobox "" 2 40
    sleep 2
    for bin in `find * -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \) -exec file {} \; | \
    grep -v ' shared object,' | \
    sed -n -e 's/^\(.*\):[  ]*ELF.*, not stripped/\1/p'`; do
    strip $bin
    done
   fi
fi
}

# Convert rpm or deb package to slackware tgz format.
convert_package() {

if echo $1 | grep -E -q "\.rpm$" ; then
if [ ! -x "`type -path unrpm`" ]; then
dialog --title "NOT FOUND" --msgbox "Can't find 'unrpm' to \
check and convert package." 8 50
continue
fi
if [ ! -x "`type -path cpio`" ]; then
dialog --title "NOT FOUND" --msgbox "Can't find 'cpio' to \
check and convert package." 8 50
continue
fi
D="`basename $1 .rpm`"

dialog --title "Checking rpm package for errors" --infobox "$D.rpm" 3 50
sleep 2
unset error
unrpm < $1 2>/dev/null | gzip -d 2>/dev/null | cpio -i --only-verify-crc --quiet >/dev/null 2>&1 || error=yes
if [ "$error" = "yes" ]; then
cat << EOF > $TMP/tmpmsg
	
There's something wrong with the '$D.rpm' 
package, you probably shouldn't convert it. 

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 8 70
continue
fi
cat << EOF > $TMP/tmpmsg

$D.rpm -> $D.tgz

EOF

dialog --title "CONVERTING" --infobox "`cat $TMP/tmpmsg`" 5 78
sleep 2

error() {
cd $dir && rm -rf $TMP/build_tgz
cat << EOF > $TMP/tmpmsg

Could not convert '$D.rpm' 
for some reason.

`cat $TMP/error`

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
continue;
}

dir=$PWD
rm -rf $TMP/build_tgz
mkdir -p $TMP/build_tgz
cd $TMP/build_tgz || exit 
unrpm < $1 | gzip -d | cpio -iumd --quiet 2>$TMP/error || error 
     
unset rpm_ver
if [ -d usr/doc -o -d usr/share/doc ]; then
if echo $D | grep -q "[0-9]mdk.i[3-6]86" ; then
rpm_ver="`echo $D | grep "[-._][0-9]" | sed "s/mdk.i[3-6]86//g" | \
sed "s/.*[a-z A-Z][0-9][-._]//g" | sed "s/.*[a-z A-Z][-._]//g" | sed "s/$/mdk/g"`"
else
rpm_ver="`echo $D | grep "[-._][0-9]" | sed "s/.*[a-z A-Z][0-9][-._]//g" | \
sed "s/.*[a-z A-Z][-._]//g" | sed "s/.i[3-6]86//g"`"
fi
fi

     make_slackware_compatible rpm $rpm_ver
     pause "$TMP/build_tgz" 
     strip_binaries

dialog --title "CONVERTING" --infobox "`cat $TMP/tmpmsg`" 5 78

mkpkg $D.tar y
mv $D.tar.gz $dir/$D.tgz && cd $dir && rm -rf $TMP/build_tgz
dialog --title "DONE" --msgbox "$D.tgz" 5 40
echo $D.rpm > $TMP/done 
echo $D.tgz > $TMP/current_done 
continue
elif echo $1 | grep -E -q "\.deb$" ; then
if [ ! -x "`type -path undeb`" ]; then
dialog --title "NOT FOUND" --msgbox "Can't find 'undeb' to \
check and convert package!" 8 50
continue
fi
D="`basename $1 .deb`"

dialog --title "Checking deb package for errors" --infobox "$D.deb" 3 50
sleep 2
rm -f $TMP/error.deb
undeb -p $1 data.tar.gz 2>/dev/null | tar -tzf - 1>/dev/null 2>$TMP/error.deb
if [ -s $TMP/error.deb ]; then
cat << EOF > $TMP/tmpmsg
	
There's something wrong with the '$D.deb'
package, you probably shouldn't convert it. 

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 8 70
continue
fi
cat << EOF > $TMP/tmpmsg

$D.deb -> $D.tgz

EOF
dialog --title "CONVERTING" --infobox "`cat $TMP/tmpmsg`" 5 78
sleep 2

error() {
cd $dir && rm -rf $TMP/build_tgz
cat << EOF > $TMP/tmpmsg

Could not convert '$D.deb' 
for some reason.

`cat $TMP/error`

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
continue;
}

dir=$PWD
rm -rf $TMP/build_tgz
mkdir -p $TMP/build_tgz
cd $TMP/build_tgz || exit
unset deb_ver
deb_ver="`undeb -p $1 control.tar.gz | tar -xzO "*control" -f - | \
grep "^Version: " | cut -d: -f2- | cut -d: -f2 | sed "s/^[ ]*//g"`"
undeb -p $1 data.tar.gz | tar -xzpf - 2>$TMP/error || error 

     make_slackware_compatible deb $deb_ver
     pause "$TMP/build_tgz" 
     strip_binaries

dialog --title "CONVERTING" --infobox "`cat $TMP/tmpmsg`" 5 78

mkpkg $D.tar y 
mv $D.tar.gz $dir/$D.tgz && cd $dir && rm -rf $TMP/build_tgz
dialog --title "DONE" --msgbox "$D.tgz" 5 40
echo $D.deb > $TMP/done 
echo $D.tgz > $TMP/current_done 
continue
fi
}
# End of convert.

# Monitor "make install" command and create record of install.
monitor() {

if [ ! -f /usr/lib/installwatch.so ]; then
    dialog --title "NOT FOUND" --msgbox \
    "I can't find /usr/lib/installwatch.so!" 5 55
    exit 1
fi

logdir="$LOG_DIR/packages"
if [ ! -d $logdir ]; then
mkdir -p $logdir
fi

scriptdir="$LOG_DIR/scripts"
if [ ! -d $scriptdir ]; then
mkdir -p $scriptdir
fi

command_output() {
   dialog --title "COMMAND OUTPUT" --textbox "$TMP/output" 22 78 
}
command_output_error() {
   dialog --title "ERROR: COMMAND OUTPUT" --textbox "$TMP/output.error" 22 78 
}

error() {
echo "" > $TMP/error.msg
echo "While running the command '$COMMAND'" >> $TMP/error.msg
echo "I got the error at the end of this output:" >> $TMP/error.msg 
echo "" >> $TMP/error.msg
cat $TMP/error.msg $TMP/output $TMP/error > $TMP/output.error
command_output_error

    if [ -s "`cat $INSTALLWATCHFILE | grep -v "/dev/tty"`" ]; then
    unset INSTALLWATCHFILE
    unset LD_PRELOAD
    [ "$INSTALLWATCH_BACKUP_PATH" ] && rm -rf $backup/no-backup
  dialog --title "ERROR" \
  --menu "" 9 50 2 \
"Continue" "Continue anyway." \
"Quit" "Do not continue." 2> $TMP/reply
  REPLY="`cat $TMP/reply`"
 if [ ! "$REPLY" = "Continue" ]; then
    rm -f $TMP/tmpmsg $TMP/$package* $TMP/output
    unset INSTALLWATCH_BACKUP_PATH
    reset 2>/dev/null
    exit 1
 else
 skip=yes
 fi
 else
    [ "$INSTALLWATCH_BACKUP_PATH" ] && rm -rf $backup
    unset INSTALLWATCH_BACKUP_PATH
    unset INSTALLWATCHFILE
    unset LD_PRELOAD
    rm -f $TMP/tmpmsg $TMP/$package* $TMP/output
    reset 2>/dev/null
    exit 1
 fi
}

if [ "$1" = "" ]; then
cat << EOF > $TMP/tmpmsg

Please enter the command in the box you want to run:

The default is 'make install' just press enter for it.

You could put more than one install target after make.

Example: make install install-man install-info etc.
EOF

   dialog --title "ENTER COMMAND" --inputbox "`cat $TMP/tmpmsg`" 15 60 2> $TMP/command
   if [ $? = 1 ]; then
    rm -f $TMP/command $TMP/SeT*
    reset 2>/dev/null
    exit
   fi 

    COMMAND="`cat $TMP/command`"

    if [ "$COMMAND" = "" ]; then
    COMMAND="make install"
    fi 
else
COMMAND="$1"
fi

newer_version="$(basename $(pwd) | sed "s/[-._][0-9].*//g")"
if [ ! `ls "$LOG_DIR/packages" | wc -l` -eq 0 ]; then
if ls $LOG_DIR/packages/$newer_version* 1>/dev/null 2>/dev/null ; then
cat << EOF > $TMP/tmpmsg

Is this a newer version of an already installed package?

If yes i'll install this package then remove anything
in the other package not in this one.

EOF

   dialog --title "NEWER VERSION" --yesno "`cat $TMP/tmpmsg`" 11 60 2> $TMP/pkgname
   if [ $? = 0 ]; then
     dialog --title "Creating list..." --infobox "" 2 30
     sleep 1
   echo 'dialog --menu "Please select the package that this is a newer version off." 22 40 14 \' > $TMP/packages.n
   ( cd $LOG_DIR/packages && find * -type f -maxdepth 0 | sed -e 's/.*/"&" "" \\/' >> $TMP/packages.n )
   echo "2> $TMP/return" >> $TMP/packages.n
    . $TMP/packages.n
    upgrade_package="`cat $TMP/return`"
    
   if [ ! "$upgrade_package" = "" ]; then
    upgrade=yes 
   fi
   fi
fi
fi

if [ "$upgrade" = "yes" ]; then
cat << EOF > $TMP/tmpmsg

Please enter a name in the box for the package. 

If you leave the box blank the name of the currently
installed package '$upgrade_package' will be used.

EOF
else
cat << EOF > $TMP/tmpmsg

Please enter a name in the box for the package. 

If you leave the box blank the name of the current
directory '$(basename $(pwd))' will be used.

EOF
fi

   dialog --title "PACKAGE NAME" --inputbox "`cat $TMP/tmpmsg`" 12 70 2> $TMP/pkgname
   if [ ! $? = 0 ]; then
    rm -f $TMP/pkgname $TMP/SeT*
    continue;
   fi 
 
   pkgname="`cat $TMP/pkgname`" 
   
   if [ "$pkgname" = "" ]; then
   if [ "$upgrade" = "yes" ]; then
   package="$upgrade_package"
   skip=yes
   else
   package="$(basename $(pwd))"
   fi
   else
   package="$pkgname"
   fi

   if [ ! "$skip" = "yes" ]; then
while :
do
   if [ -s "$LOG_DIR/packages/$package" ]; then

cat << EOF > $TMP/tmpmsg

There's a package named '$package' 
listed in $LOG_DIR/packages.

If it's the same package with basically 
the same contents, you should remove it
'removepkg $package' then 
install this one.

Choose another name or cancel.

EOF

   dialog --title "PACKAGE NAME EXIST" --inputbox "`cat $TMP/tmpmsg`" 17 70 2> $TMP/pkgname
   if [ $? = 1 ]; then
    rm -f $TMP/pkgname $TMP/SeT*
    reset 2>/dev/null
    exit
   fi 

   pkgname="`cat $TMP/pkgname`" 
   
   if [ "$pkgname" = "" ]; then
     continue
   else
     package="$pkgname"
   fi
else
break;
fi
done
fi
unset skip

cat << EOF > $TMP/tmpmsg

Do you want a backup made of anything that 
is overwritten by the install command?

The backup will be put in 
$LOG_DIR/backups/$package. 

If you don't want a backup, choose No.
EOF

   dialog --title "BACKUP?" --yesno "`cat $TMP/tmpmsg`" 13 60 2> $TMP/backup
   if [ $? = 0 ]; then
   backup="$LOG_DIR/backups/$package" 
   mkdir -p $backup && export INSTALLWATCH_BACKUP_PATH=$backup
   fi 

   dialog --title "RUNNING COMMAND" --infobox "$COMMAND" 4 70 
   sleep 2

    export INSTALLWATCHFILE="$TMP/$package"
    >$INSTALLWATCHFILE
    export LD_PRELOAD="/usr/lib/installwatch.so"
   
    $COMMAND 1>$TMP/output 2>$TMP/error || error 

    unset INSTALLWATCHFILE	
    unset LD_PRELOAD

    if [ -s $TMP/output ]; then
    command_output
    fi

 if [ "$upgrade" = "yes" ]; then
 
    trigger $upgrade_package
    sed -n "/$TRIGGER/,/^$/p" < $LOG_DIR/packages/$upgrade_package | \
    grep -v "$TRIGGER" | sort -u > $TMP/$package-old-upgraded

  if [ -r $LOG_DIR/scripts/$upgrade_package ]; then
    extract_links < $LOG_DIR/scripts/$upgrade_package | \
    sed "s/^[ 	]*//g" | sort -u >> $TMP/$package-old-upgraded
  fi
 fi
    
    dialog --title "Creating installation file..." --infobox "$package" 3 50

    grep -v -E "/dev/|$PWD|/tmp/" $TMP/$package | cut -f3 | \
    sed "/\.\.\//d" | sed "s/\.\///g" | sed "/^$/d" | sort -u >> $TMP/$package.a

    if [ ! -s $TMP/$package.a ]; then
    dialog --title "There was a problem. I can't continue!" --msgbox "" 4 50
    exit 1
    fi

    rm -f $TMP/$package.b $TMP/$package.bb 
    cat $TMP/$package.a | while read file ; do
    if [ -f "$file" ]; then
    echo "$file" >> $TMP/$package.bb
    echo "`dirname $file`/" >> $TMP/$package.bb
    elif [ -d "$file" ]; then
    echo "$file/" >> $TMP/$package.bb
    fi
    done
    
    if [ ! -s $TMP/$package.bb ]; then
    dialog --title "There was a problem. I can't continue!" --msgbox "" 4 50
    exit 1
    fi
    sort -u $TMP/$package.bb | sed "s/^\///g" > $TMP/$package.b 
    rm -f $TMP/$package.bb 

  if cut -f2 $TMP/$package | grep -q "symlink" ; then
    grep "symlink" $TMP/$package | cut -f4 | grep -v "$PWD" | \
    sed "/\.\.\//d" | sed "s/\.\///g" | sed "s/^\///g" | sort -u > $TMP/$package.s
    rm -f $TMP/$package.script 
    cat $TMP/$package.s | while read file; do
  if [ -L /$file ]; then
    echo "( cd `dirname $file` ; rm -rf `basename $file` )" >> $TMP/$package.script
    echo "( cd `dirname $file` ; ln -sf `ls -l /$file | awk '{print $11}'` `basename $file` )" >> $TMP/$package.script
 fi
    done 
  fi

    echo "PACKAGE NAME: $package" > $logdir/$package 
    echo "INSTALL COMMAND: $COMMAND" >> $logdir/$package 
    echo "PACKAGE DESCRIPTION:" >> $logdir/$package 
    echo "$package: Put description here!" >> $logdir/$package 
    echo "$package:" >> $logdir/$package 

    rm -f $TMP/deps $TMP/deps1
    if [ -x "`type -path ldd`" -a -x "`type -path fmt`" -a -x "`type -path awk`" ]; then
    cat $TMP/$package.b | while read file; do
    if [ -x "/$file" -a ! -d "/$file" ]; then
	ldd "/$file" 2>/dev/null | grep "lib" | awk '{print $1}' 1>> $TMP/deps
    fi
    done
    if [ -s $TMP/deps ]; then
        sort -u $TMP/deps | grep -v "statically" > $TMP/deps1
	echo -n `cat $TMP/deps1` | fmt -w 66 | while read file; do
 	  echo "LIB DEPENDS: $file" >> $logdir/$package 
        done
    fi
    fi
    
    echo "DATE: `date +%c 2>/dev/null`" >> $logdir/$package 
    echo "FILE LIST:" >> $logdir/$package 

    cat $TMP/$package.b >> $logdir/$package 
    
    if [ -s $TMP/$package.script ]; then 
    mv $TMP/$package.script $LOG_DIR/scripts/$package
    chmod 755 $LOG_DIR/scripts/$package 2>/dev/null
    fi

 if [ "$upgrade" = "yes" ]; then

    trigger $package
    sed -n "/$TRIGGER/,/^$/p" < $LOG_DIR/packages/$package | \
    grep -v "$TRIGGER" | sort -u > $TMP/$package-new-upgraded

  if [ -r $LOG_DIR/scripts/$package ]; then
    extract_links < $LOG_DIR/scripts/$package | \
    sed "s/^[ 	]*//g" | sort -u >> $TMP/$package-new-upgraded
  fi

  echo "FILE LIST:" > $LOG_DIR/packages/$package-delete
  comm -13 $TMP/$package-new-upgraded $TMP/$package-old-upgraded \
  >> $LOG_DIR/packages/$package-delete
 
  OLD_delete="`grep -v -E "FILE LIST:|^install/" $LOG_DIR/packages/$package-delete`"
  if [ ! "$OLD_delete" = "" ]; then
  
 cat << EOF > $TMP/remove

The files below were found in the old '$upgrade_package' 
package, but not in the new '$package' package.

They will be removed if you answer yes in the next box.

`cat $LOG_DIR/packages/$package-delete`
 
EOF

  dialog --title "REMOVE FILES" --textbox "$TMP/remove" 22 70 
  dialog --title "Should i remove the files?" --yesno "" 4 40 
  if [ $? = 0 ]; then
     dialog --title "Removing files..." --infobox "" 2 40
     sleep 2
     removepkg $package-delete >/dev/null 2>/dev/null
  fi
  fi

  rm -f $LOG_DIR/packages/$package-delete
  rm -f $LOG_DIR/removed_packages/$package-delete
  rm -f $TMP/$package-new-upgraded $TMP/$package-old-upgraded
  if [ ! "$upgrade_package" = "$package" ]; then
   rm -f $LOG_DIR/packages/$upgrade_package
   rm -f $LOG_DIR/scripts/$upgrade_package
  fi 
  fi

echo $package > $TMP/package_name
while :
do
cat << EOF > $TMP/tmpmsg

Do you want to create a slackware tgz, a debian deb,
or a rpm package from what you just installed?

EOF

  dialog --title "TGZ/RPM/DEB" \
  --menu "`cat $TMP/tmpmsg`" 13 60 4 \
"Debian" "Create debian deb package" \
"Slackware" "Create slackware tgz package" \
"Rpm" "Create rpm package" \
"None" "Don't Create a package" 2> $TMP/reply
    if [ ! $? = 0 ]; then
      EXIT=YES     
    fi
    
     REPLY="`cat $TMP/reply`"
     
     if [ "$REPLY" = "Slackware" ]; then
     pkg_name
     dialog --title "Creating slackware package..." --infobox "$package_name-$version.tgz" 3 50

     dir=$PWD
     rm -rf $TMP/build_tgz
     mkdir -p $TMP/build_tgz
     cd $TMP/build_tgz

     package=`cat $TMP/package_name`
     
     cat $TMP/$package.b | while read file; do
     if [ -d `dirname /$file` ]; then
     if [ ! -d `dirname $file` ]; then
     mkdir -p `dirname $file`
     fi
     fi
     done

     cat $TMP/$package.b | while read file; do
     if [ ! -d "/$file" -a ! -L "/$file" ]; then
     cp -p /$file `dirname $file` 
     fi
     done

     if [ -s $LOG_DIR/scripts/$package ]; then 
     mkdir -p install
     cp $LOG_DIR/scripts/$package install/doinst.sh
     chmod 644 install/doinst.sh
     chown root.root install/doinst.sh
     fi

     package="$package_name"
    
     make_slackware_compatible

     if echo "$COMMAND" | grep "make " | grep -q " install" ; then
       cd $dir
       docs build_tgz /usr/doc $package-$version
       cd $TMP/build_tgz
     fi
     
     pause "$TMP/build_tgz" 
     strip_binaries

error() {
cd $dir && rm -rf $TMP/build_tgz
rm -f $TMP/$package_name.s $TMP/$package_name 2>/dev/null 
cat << EOF > $TMP/tmpmsg

Could not create '$package-$version.tgz' 
for some reason.

`cat $TMP/error`

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
continue;
}

     dialog --title "Creating slackware package..." --infobox "$package-$version.tgz" 3 50
     find . -type d -exec chmod 755 {} \;
     find . -type d -exec chown root.root {} \;
     tar -cpf $package-$version.tar * 1>/dev/null 2>$TMP/error || error      
     gzip -9 $package-$version.tar 1>/dev/null 2>$TMP/error || error      
     mv $package-$version.tar.gz $dir/$package-$version.tgz
     cd $dir && rm -rf $TMP/build_tgz

     elif [ "$REPLY" = "Rpm" ]; then

     if [ ! -x "`type -path rpm`" ]; then
     dialog --title "NOT FOUND" --msgbox \
     "The 'rpm' program is not on the system." 5 50 
     continue;
     else

     pkg_name
     dialog --title "Creating rpm package..." --infobox "$package_name-$version-1.i386.rpm" 3 50

     dir=$PWD
     rm -rf $TMP/build_rpm
     mkdir -p $TMP/build_rpm
     cd $TMP/build_rpm
     
     package=`cat $TMP/package_name`

     cat $TMP/$package.b | while read file; do
     if [ -d `dirname /$file` ]; then
     if [ ! -d `dirname $file` ]; then
     mkdir -p `dirname $file`
     fi
     fi
     done

     cat $TMP/$package.b | while read file; do
     if [ ! -d "/$file" -a ! -L "/$file" ]; then
     cp -p /$file `dirname $file` 
     fi
     done

     if [ -s $LOG_DIR/scripts/$package ]; then 
     cp $LOG_DIR/scripts/$package .
     bash $package
     rm -f $package
     fi
     
     package="$package_name"

     make_debian_rpm_compatible

     if echo "$COMMAND" | grep "make " | grep -q " install" ; then
       cd $dir
       docs build_rpm /usr/doc $package-$version
       cd $TMP/build_rpm
     fi
     
     pause "$TMP/build_rpm" 
     strip_binaries

     dialog --title "Creating rpm package..." --infobox "$package-$version-1.i386.rpm" 3 50

     find . -type d -exec chmod 755 {} \;
     find . -type d -exec chown root.root {} \;
     
     find * -not -type d | sed "s/^/\//g" > $TMP/files
     files="`cat $TMP/files`"
     
cat << EOF > $package.spec
Summary:   None
Name:      $package
Version:   $version
Release:   1
Copyright: GPL
Group:     None          
Packager:  Jerry Donut <jerry@donut.com>
BuildArchitectures: i386
BuildRoot: $TMP/build_rpm

%description
No description

%files
$files
EOF

while :
do
cat << EOF > $TMP/tmpmsg
This is a chance to edit the spec file for the rpm package.
Don't change the "%files" or "BuildRoot:" lines.

`cat $package.spec | head -n 11`

Put the name of the editor you want to use in the box below?
If you don't want to edit the spec file just press enter.
EOF
   dialog --title "EDIT SPEC FILE?" --inputbox "`cat $TMP/tmpmsg`" 24 78 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ ! "$editor" = "" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      dialog --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      $editor $package.spec
      break;    
    fi
    fi
break;    
done    

err_rpm() {
 cd $dir && rm -rf $TMP/build_rpm
 dialog --title "ERROR from 'rpm'!" --textbox "$TMP/rpm_error" 20 70
 continue;
}

       package=`grep "^Name: " $package.spec | awk '{print $2}'` 
       version=`grep "^Version: " $package.spec | awk '{print $2}'` 
       release=`grep "^Release: " $package.spec | awk '{print $2}'` 
       arch=`grep "^BuildArchitectures: " $package.spec | awk '{print $2}'` 

       dialog --title "Creating rpm package..." --infobox "$package-$version-$release.$arch.rpm" 3 50

       find_rpm_dir 
       rpm -bb $package.spec 1>/dev/null 2>$TMP/rpm_error || err_rpm
       mv $rpm_dir/$package-$version-$release.$arch.rpm $dir || err_rpm
       cd $dir && rm -rf $TMP/build_rpm
     fi

     elif [ "$REPLY" = "Debian" ]; then
     if [ ! -x "`type -path dpkg-deb`" ]; then
     dialog --title "NOT FOUND" --msgbox \
     "The 'dpkg-deb' program is not on the system." 5 50 
     continue;
     else
    
     pkgname
     dialog --title "Creating debian package..." --infobox "$package_name-$version"_i386".deb" 3 50

     dir=$PWD
     rm -rf $TMP/build_deb
     mkdir -p $TMP/build_deb
     cd $TMP/build_deb
     
     package=`cat $TMP/package_name`

     cat $TMP/$package.b | while read file; do
     if [ -d `dirname /$file` ]; then
     if [ ! -d `dirname $file` ]; then
     mkdir -p `dirname $file`
     fi
     fi
     done

     cat $TMP/$package.b | while read file; do
     if [ ! -d "/$file" -a ! -L "/$file" ]; then
     cp -p /$file `dirname $file` 
     fi
     done

     if [ -s $LOG_DIR/scripts/$package ]; then 
     cp $LOG_DIR/scripts/$package .
     bash $package
     rm -f $package
     fi
     
     package="$package_name"
     
     rm -f $TMP/deps 
     for file in `find * -type f` ; do
     if [ -x "$file" ]; then
       ldd "$file" 2>/dev/null | grep "lib" | awk '{print $1}' 1>> $TMP/deps
     fi
     done

        sort -u $TMP/deps | grep -v "statically" | \
	sed "s/$/,/g" | sed "s/\/lib\///g" > $TMP/deps1
	rm -f $TMP/deps 

	 (echo -n "Depends: " ; echo -n `cat $TMP/deps1`) > $TMP/deps2
	  sed "s/,$//g" $TMP/deps2 > $TMP/deps
          Depends=`cat $TMP/deps`
    
    total_size=0
    for file in `find * -type f` ; do
     size=`du -k $file | cut -f1`
     total_size=$(( $size + $total_size ))
    done

     mkdir -p DEBIAN
    
cat << EOF > DEBIAN/control
Package: $package
Version: $version
Section: base
Priority: optional
Architecture: i386
$Depends
Installed-Size: $total_size
Maintainer: Jerry Donut <jerry@donut.com> 
Description: No description 
EOF

while :
do
cat << EOF > $TMP/tmpmsg
This is a chance to edit the control file for the debian package.
Don't change the "Depends:" or "Installed-Size:" lines.

`cat DEBIAN/control | fold -w 72 | head -n 11`

Put the name of the editor you want to use in the box below?
If you don't want to edit the control file just press enter.
EOF
   dialog --title "EDIT CONTROL FILE?" --inputbox "`cat $TMP/tmpmsg`" 24 78 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ ! "$editor" = "" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      dialog --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      $editor DEBIAN/control
      break;    
    fi
    fi
break;    
done    
    
     package=`grep "^Package: " DEBIAN/control | awk '{print $2}'` 
     version=`grep "^Version: " DEBIAN/control | awk '{print $2}'` 
     arch=`grep "^Architecture: " DEBIAN/control | awk '{print $2}'` 
     
     dialog --title "Creating debian package..." --infobox "$package-$version"_$arch".deb" 3 50
     sleep 1
     
     make_debian_rpm_compatible

     if echo "$COMMAND" | grep "make " | grep -q " install" ; then
       cd $dir
       docs build_deb /usr/share/doc $package
       cd $TMP/build_deb
     fi
     
     pause "$TMP/build_deb" 
     strip_binaries

     dialog --title "Creating debian package..." --infobox "$package-$version"_$arch".deb" 3 50

     find . -type d -exec chmod 755 {} \;
     find . -type d -exec chown root.root {} \;

     if [ -x "`type -path md5sum`" ]; then
     for file in `find * -type f` ; do
       md5sum $file >> DEBIAN/md5sums
     done
     fi

err_deb() {
 cd $dir && rm -rf $TMP/build_deb
 dialog --title "ERROR from 'dpkg-deb'!" --textbox "$TMP/deb_error" 20 70
 continue;
}

     dpkg-deb -b . $dir/"$package-$version"_$arch".deb" 1>/dev/null 2>$TMP/deb_error || err_deb
     cd $dir && rm -rf $TMP/build_deb
    fi
    else
    EXIT=YES     
 fi

if [ ! "$skip" = "yes" ]; then
package=`cat $TMP/package_name`
cat << EOF > $TMP/tmpmsg

Package Log: $LOG_DIR/packages/$package

EOF

if [ "$INSTALLWATCH_BACKUP_PATH" ]; then 
rm -rf $backup/no-backup
if [ ! `ls "$backup" | wc -l` -eq 0 ]; then
    dialog --title "Tar gziping backup:" --infobox "$backup" 3 45
    sleep 1
    ( cd $backup && tar czpf ../$package.tgz * ) 
    rm -rf $backup
    mkdir -m 755 -p $backup
    chown root.root $backup
    mv $LOG_DIR/backups/$package.tgz $backup
cat << EOF >> $TMP/tmpmsg
The backup is here '$backup/$package.tgz'.
                                 
EOF
unset INSTALLWATCH_BACKUP_PATH
else
cat << EOF >> $TMP/tmpmsg
Nothing needed to be backed up.                                 

EOF
rm -rf $backup
unset INSTALLWATCH_BACKUP_PATH
fi
fi

   dialog --title "DONE" --msgbox "`cat $TMP/tmpmsg`" 9 76 

if [ "$EXIT" = "YES" ]; then
break;
else
skip=yes
continue;
fi
fi
if [ "$EXIT" = "YES" ]; then
break;
else
continue;
fi
done
rm -f $TMP/tmpmsg $TMP/$package* $TMP/output
reset 2>/dev/null
exit
}
# End of monitor.

 while [ 0 ]; do
  dialog --title "Slackware Package Tool (pkgtool2 v2.0.9)" \
--menu "\nWelcome to the Slackware package tool supplement.\n\
\nWhich option would you like?\n" 18 78 7 \
"Convert" "Convert rpm or deb package to slackware tgz package" \
"List" "contents of rpm, deb, tgz, tar.gz, tar.bz2, or zip package" \
"Configure" "& make rules for packages (run, create, or read/edit/url)" \
"Monitor" "Monitor make install and create record of install" \
"Restore" "backup for $LOG_DIR/packages/package_name" \
"Create" "tgz, rpm, or deb package from contents of file or directory" \
"Exit" "Exit Pkgtool2" 2> $TMP/reply
  if [ ! $? = 0 ]; then
   rm -f $TMP/* 2>/dev/null
   reset 2>/dev/null
   exit
  fi
  REPLY="`cat $TMP/reply`"
  rm -f $TMP/* 2>/dev/null
  if [ "$REPLY" = "Exit" ]; then
   rm -f $TMP/*
   reset 2>/dev/null
   exit
  fi

   if [ "$REPLY" = "Restore" ]; then

cat << EOF > $TMP/tmpmsg

The restore option removes an installed package,
then it installs the backup for that package.

Backups are created when the monitor option
is used to monitor a 'make install' command.

Continue?

EOF
     dialog --title "RESTORE README" --yesno "`cat $TMP/tmpmsg`" 13 60 2> /dev/null
     if [ ! $? = 0 ]; then
     continue;
     fi

   dialog --title "Creating list..." --infobox "" 2 30
   sleep 1
    
     rm -f $TMP/packages
    ( cd $LOG_DIR/packages 2>/dev/null && find * -type f -maxdepth 0 2>/dev/null > $TMP/packages )

   if [ ! -s "$TMP/packages" ]; then
    dialog --title "DIRECTORY EMPTY" --msgbox "The directory ($LOG_DIR/packages) \
does not seem to contain any package listings." 8 60
    continue;
   fi

   echo 'dialog --menu "Please select the package you want to restore." 22 50 14 \' > $TMP/packages.n
   cat $TMP/packages | sed -e 's/.*/"&" "" \\/' >> $TMP/packages.n
   echo "2> $TMP/return" >> $TMP/packages.n
   
    while [ 0 ]; do
    . $TMP/packages.n
    if [ ! "`cat $TMP/return`" = "" ]; then
    package="`cat $TMP/return`"

   if [ ! -f "$LOG_DIR/backups/$package/$package.tgz" ]; then
     dialog --title "NOT FOUND" --msgbox \
     "The backup package ($LOG_DIR/backups/ \n$package/$package.tgz) \ndoes not seem to exist." 8 60
     continue;
   fi
   dialog --title "Removing package: $package" --infobox \
   "This may take a few minutes." 4 50 
     sleep 1
     removepkg $package 1>$TMP/removed.$package 2>/dev/null 
     dialog --title "REMOVED: $package" --textbox "$TMP/removed.$package" 22 78 2> /dev/null
     rm -f $TMP/removed.$package
   dialog --title "Restoring package..." --infobox "$package.tgz" 4 50 
     sleep 1
     tar -Uxzplf $LOG_DIR/backups/$package/$package.tgz -C / 2>/dev/null
    if [ -f "$LOG_DIR/removed_scripts/$package" ]; then
      grep " ln -sf " $LOG_DIR/removed_scripts/$package | while read links ; do
      dir=`echo $links | awk '{print $3}'`
      file=`echo $links | awk '{print $7}'`
      link=`echo $links | awk '{print $8}'`
      if [ -e "/$dir/$file" ]; then
      if [ ! -e "/$dir/$link" ]; then
      ( cd /$dir && ln -sf $file $link )
      fi
      fi
     done
    fi

cat << EOF > $TMP/tmpmsg

Should i remove the backup directory?
$LOG_DIR/backups/$package

EOF

   dialog --title "REMOVE BACKUP?" --yesno "`cat $TMP/tmpmsg`" 8 60 2> /dev/null
   if [ $? = 0 ]; then
   dialog --title "Removing backup..." --infobox "$package.tgz" 4 50
   sleep 1 
     rm -rf $LOG_DIR/backups/$package
   fi
   dialog --title "DONE" --msgbox "$package" 5 50
   else
   break;
   fi
done
   fi

 if [ "$REPLY" = "Create" ]; then

cat << EOF > $TMP/tmpmsg

You can create the slackware tgz, deb, or rpm package based
on the contents of a $LOG_DIR/packages/package_name file, or
from the contents of a directory.

EOF

  dialog --title "Create TGZ/RPM/DEB package" \
  --menu "`cat $TMP/tmpmsg`" 13 66 3 \
"File" "Based on contents of $LOG_DIR/packages file" \
"Directory" "Based on contents of directory" \
"Exit" "Cancel" 2> $TMP/reply
  if [ ! $? = 0 ]; then
    continue;
  fi

     REPLY="`cat $TMP/reply`"
     
     if [ "$REPLY" = "Exit" ]; then
       continue;
     fi

     if [ "$REPLY" = "Directory" ]; then
     
while :
do
cat << EOF > $TMP/tmpmsg

Please enter the name of the directory that contains 
the contents you want to use. 

Press enter on empty box for current directory.
EOF

   dialog --title "SELECT DIRECTORY" --inputbox "`cat $TMP/tmpmsg`" 12 60 2> $TMP/dir
   if [ ! $? = 0 ]; then
     exit
   fi 

   directory="`cat $TMP/dir`"
   rm -f $TMP/dir

   if [ "$directory" = "" ]; then
    directory=$PWD
    break;   
   elif [ ! -d $directory ]; then
    dialog --title "DIRECTORY NOT FOUND" --msgbox "The directory ($directory) \
does not seem to exist. Please try again or cancel." 8 60
    continue;
   fi
break; 
done     
    dialog --title "Getting contents of directory..." --infobox "$directory" 3 50
    sleep 2
    
         ( cd $directory && tar -cpf - * | tar -tvf - 1>$TMP/tmpmsg 2>/dev/null )   
     
     dialog --title "CONTENTS OF DIRECTORY" --textbox "$TMP/tmpmsg" 22 78 2>/dev/null

echo $package > $TMP/package_name
while :
do
cat << EOF > $TMP/tmpmsg

You saw the contents of the directory.

Do you want to create a tgz, rpm, or deb
package based on those contents?

EOF

  dialog --title "TGZ/RPM/DEB" \
  --menu "`cat $TMP/tmpmsg`" 15 60 4 \
"Debian" "Create debian deb package" \
"Slackware" "Create slackware tgz package" \
"Rpm" "Create rpm package" \
"None" "Don't Create a package" 2> $TMP/reply
  if [ ! $? = 0 ]; then
    break;
  fi

     REPLY="`cat $TMP/reply`"
     
     if [ "$REPLY" = "None" ]; then
       break;
     fi

     if [ "$REPLY" = "Slackware" ]; then

    package="slackware-$$"
    pkg_name
    package="$package_name"
    dialog --title "Creating slackware package..." --infobox "$package-$version.tgz" 3 50
    sleep 1
        
    dir=$PWD
    cd $directory

error() {
cat << EOF > $TMP/tmpmsg

Could not create '$package-$version.tgz' 
for some reason.

`cat $TMP/error`

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
continue;
}
    
    make_slackware_compatible
    mkpkg $package-$version.tar n 2>$TMP/error
    mv $package-$version.tar.gz $dir/$package-$version.tgz 2>$TMP/error || error

    dialog --title "DONE" --msgbox "$package-$version.tgz" 5 50

cat << EOF > $TMP/tmpmsg

Do you want to remove the '$directory'
directory?

EOF
     dialog --title "REMOVE DIRECTORY?" --yesno "`cat $TMP/tmpmsg`" 8 70 2> /dev/null

   if [ $? = 0 ]; then
     dialog --title "Removing directory..." --infobox "$directory" 3 50
     sleep 1
     rm -rf $directory
   fi
     
     elif [ "$REPLY" = "Debian" ]; then
     
    if [ ! -x "`type -path dpkg-deb`" ]; then
      dialog --title "NOT FOUND" --msgbox \
      "The 'dpkg-deb' program is not on the system." 5 50 
      continue;
    fi
   
    package="debian-$$"
    pkg_name
    package="$package_name"
    dialog --title "Creating debian package..." --infobox "$package-$version.deb" 3 50
        
    dir=$PWD
    cd $directory

     rm -f $TMP/deps 
     for file in `find * -type f` ; do
     if [ -x "$file" ]; then
       ldd "$file" 2>/dev/null | grep "lib" | awk '{print $1}' 1>> $TMP/deps
     fi
     done

        sort -u $TMP/deps | grep -v "statically" | \
	sed "s/$/,/g" | sed "s/\/lib\///g" > $TMP/deps1
	rm -f $TMP/deps 

	 (echo -n "Depends: " ; echo -n `cat $TMP/deps1`) > $TMP/deps2
	  sed "s/,$//g" $TMP/deps2 > $TMP/deps
          Depends=`cat $TMP/deps`
    
    total_size=0
    for file in `find * -type f` ; do
     size=`du -k $file | cut -f1`
     total_size=$(( $size + $total_size ))
    done

     mkdir -p DEBIAN
    
cat << EOF > DEBIAN/control
Package: $package
Version: $version
Section: base
Priority: optional
Architecture: i386
$Depends
Installed-Size: $total_size
Maintainer: Jerry Donut <jerry@donut.com>
Description: No description
EOF

while :
do
cat << EOF > $TMP/tmpmsg
This is a chance to edit the control file for the debian package.
Don't change the "Depends:" or "Installed-Size:" lines.

`cat DEBIAN/control | fold -w 72 | head -n 11`

Put the name of the editor you want to use in the box below?
If you don't want to edit the control file just press enter.
EOF
   dialog --title "EDIT CONTROL FILE?" --inputbox "`cat $TMP/tmpmsg`" 24 78 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ ! "$editor" = "" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      dialog --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      $editor DEBIAN/control
      break;    
    fi
    fi
break;    
done    

     package=`grep "^Package: " DEBIAN/control | awk '{print $2}'` 
     version=`grep "^Version: " DEBIAN/control | awk '{print $2}'` 
     arch=`grep "^Architecture: " DEBIAN/control | awk '{print $2}'` 

     dialog --title "Creating debian package..." --infobox "$package-$version"_$arch".deb" 3 50
     sleep 1
     
     make_debian_rpm_compatible
     strip_binaries
     
     dialog --title "Creating debian package..." --infobox "$package-$version"_$arch".deb" 3 50

     if [ -x "`type -path md5sum`" ]; then
     for file in `find * -type f | grep -v "DEBIAN/control"` ; do
       md5sum $file >> DEBIAN/md5sums
     done
     fi

err_deb() {
 dialog --title "ERROR from 'dpkg-deb'!" --textbox "$TMP/deb_error" 20 70
 continue;
}

    dpkg-deb -b . $dir/"$package-$version"_$arch".deb" 1>/dev/null 2>$TMP/deb_error || err_deb
    rm -rf DEBIAN
    
    dialog --title "DONE" --msgbox "$package-$version"_$arch".deb" 5 50

cat << EOF > $TMP/tmpmsg

Do you want to remove the '$directory'
directory?

EOF
     dialog --title "REMOVE DIRECTORY?" --yesno "`cat $TMP/tmpmsg`" 8 70 2> /dev/null

   if [ $? = 0 ]; then
     dialog --title "Removing directory..." --infobox "$directory" 3 50
     sleep 1
     rm -rf $directory
   fi
          
     elif [ "$REPLY" = "Rpm" ]; then
     
    if [ ! -x "`type -path rpm`" ]; then
      dialog --title "NOT FOUND" --msgbox \
      "The 'rpm' program is not on the system." 5 50 
      continue;
    fi
   
    package="rpm-$$"
    pkg_name
    package="$package_name"
    dialog --title "Creating rpm package..." --infobox "$package-$version.rpm" 3 50
    sleep 1
            
    dir=$PWD
    cd $directory
     
     make_debian_rpm_compatible
     strip_binaries

     dialog --title "Creating rpm package..." --infobox "$package-$version.rpm" 3 50

     find * -not -type d | sed "s/^/\//g" > $TMP/files
     files="`cat $TMP/files`"

cat << EOF > $package.spec
Summary:   None
Name:      $package
Version:   $version
Release:   1
Copyright: GPL
Group:     None          
Packager:  Jerry Donut <jerry@donut.com>
BuildArchitectures: i386
BuildRoot: $directory

%description
No description

%files
$files
EOF

while :
do
cat << EOF > $TMP/tmpmsg
This is a chance to edit the spec file for the rpm package.
Don't change the "%files" or "BuildRoot:" lines.

`cat $package.spec | head -n 11`

Put the name of the editor you want to use in the box below?
If you don't want to edit the spec file just press enter.
EOF
   dialog --title "EDIT SPEC FILE?" --inputbox "`cat $TMP/tmpmsg`" 24 78 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ ! "$editor" = "" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      dialog --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      clear 2>/dev/null
      $editor $package.spec
      break;    
    fi
    fi
break;    
done    

err_rpm() {
 dialog --title "ERROR from 'rpm'!" --textbox "$TMP/rpm_error" 20 70
 continue;
}

       package=`grep "^Name: " $package.spec | awk '{print $2}'` 
       version=`grep "^Version: " $package.spec | awk '{print $2}'` 
       release=`grep "^Release: " $package.spec | awk '{print $2}'` 
       arch=`grep "^BuildArchitectures: " $package.spec | awk '{print $2}'` 

       dialog --title "Creating rpm package..." --infobox "$package-$version-$release.$arch.rpm" 3 50

       find_rpm_dir 
       rpm -bb $package.spec 1>/dev/null 2>$TMP/rpm_error || err_rpm
       mv $rpm_dir/$package-$version-$release.$arch.rpm $dir || err_rpm
       cd $dir && rm -rf $TMP/build_rpm

    dialog --title "DONE" --msgbox "$package-$version-$release.$arch.rpm" 5 50

cat << EOF > $TMP/tmpmsg

Do you want to remove the '$directory'
directory?

EOF
     dialog --title "REMOVE DIRECTORY?" --yesno "`cat $TMP/tmpmsg`" 8 70 2> /dev/null

   if [ $? = 0 ]; then
     dialog --title "Removing directory..." --infobox "$directory" 3 50
     sleep 1
     rm -rf $directory
   fi
     fi
done

     elif [ "$REPLY" = "File" ]; then

   if [ `ls "$LOG_DIR/packages" | wc -l` -eq 0 ]; then
     dialog --title "DIRECTORY EMPTY" --msgbox \
     "The $LOG_DIR/packages directory is empty." 5 60 
     continue;
   fi

    dialog --title "Creating list..." --infobox "" 2 30
    sleep 1

   echo 'dialog --menu "Please select the package content listing you \
   wish to create the package from." 22 50 14 \' > $TMP/packages
   ( cd $LOG_DIR/packages && find * -type f -maxdepth 0 | sed -e 's/.*/"&" "" \\/' >> $TMP/packages )
   echo "2> $TMP/return" >> $TMP/packages
   
    while [ $? = 0 ]; do
    . $TMP/packages

    if [ ! "`cat $TMP/return`" = "" ]; then
   
     package="`cat $TMP/return`"

     cat $LOG_DIR/packages/$package >$TMP/tmpmsg
     if [ -s $LOG_DIR/scripts/$package ]; then
       echo "" >>$TMP/tmpmsg
       echo "This is the install/doinst.sh script for $package." >>$TMP/tmpmsg
       echo "" >>$TMP/tmpmsg
       cat $LOG_DIR/scripts/$package >>$TMP/tmpmsg
     fi     

     dialog --title "CONTENTS OF PACKAGE" --textbox "$TMP/tmpmsg" 22 78 2>/dev/null

echo $package > $TMP/package_name
while :
do
cat << EOF > $TMP/tmpmsg

You saw the contents of package
'$package'.

Do you want to create a tgz, rpm, or deb
package based on those contents?

EOF

  dialog --title "TGZ/RPM/DEB" \
  --menu "`cat $TMP/tmpmsg`" 16 50 4 \
"Debian" "Create debian deb package" \
"Slackware" "Create slackware tgz package" \
"Rpm" "Create rpm package" \
"None" "Don't Create a package" 2> $TMP/reply
  if [ ! $? = 0 ]; then
    break;
  fi

     REPLY="`cat $TMP/reply`"
     
     if [ "$REPLY" = "None" ]; then
       break;
     fi

    if [ "$REPLY" = "Slackware" ]; then

    pkg_name
    dialog --title "Creating slackware tgz package..." --infobox "$package_name-$version.tgz" 3 50
    sleep 1
        
    dir=$PWD
    package=`cat $TMP/package_name`

    trigger $package    
    sed -n "/$TRIGGER/,/^$/p" < $LOG_DIR/packages/$package | \
    grep -E -v "$TRIGGER|/$" | sort -u > $TMP/$package_name
     
     cat $TMP/$package_name | sed "s/^\.//g" | sed "s/^\///g" > $TMP/$package_name.s
     cat $TMP/$package_name.s > $TMP/$package_name 
     rm -f $TMP/$package_name.s 
     
     >$TMP/not_found 
     cat $TMP/$package_name | grep -v "^install/" | while read file; do
     if [ ! -e /$file ]; then
     echo $file >>$TMP/not_found 
     else
     echo $file >>$TMP/$package_name.found 
     fi
     done
     
     not_found
     dialog --title "Creating slackware tgz package..." --infobox "$package_name-$version.tgz" 3 50
     sleep 1

     rm -rf $TMP/build_tgz
     mkdir -p $TMP/build_tgz
     cd $TMP/build_tgz

     cat $TMP/$package_name.found | grep -v "^install/" | while read file; do
     if [ -d `dirname /$file` ]; then
     if [ ! -d `dirname $file` ]; then
       mkdir -p `dirname $file`
     fi
     fi
     done
     
     cat $TMP/$package_name.found | grep -v "^install/" | while read file; do
     if [ -f /$file ]; then
       cp -p /$file `dirname $file` 
     fi
     done
     
     if [ -s $LOG_DIR/scripts/$package ]; then 
       mkdir -p install
       cp $LOG_DIR/scripts/$package install/doinst.sh
       chmod 644 install/doinst.sh
       chown root.root install/doinst.sh
     fi

     package="$package_name"

     make_slackware_compatible
     pause "$TMP/build_tgz" 
     strip_binaries

    dialog --title "Creating slackware tgz package..." --infobox "$package-$version.tgz" 3 50
    sleep 1

error() {
cd $dir && rm -rf $TMP/build_tgz
rm -f $TMP/$package_name.s $TMP/$package_name 2>/dev/null 
cat << EOF > $TMP/tmpmsg

Could not create '$package-$version.tgz' 
for some reason.

`cat $TMP/error`

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
continue;
}

    find . -type d -exec chmod 755 {} \; 
    find . -type d -exec chown root.root {} \;
    tar -cpf $package-$version.tar * 2>$TMP/error || error
    gzip -9 $package-$version.tar 2>$TMP/error || error
    mv $package-$version.tar.gz $dir/$package-$version.tgz
    cd $dir && rm -rf $TMP/build_tgz

    dialog --title "DONE" --msgbox "$package-$version.tgz" 5 50
    rm -f $TMP/$package_name.s $TMP/$package_name  
     
     elif [ "$REPLY" = "Debian" ]; then

     if [ ! -x "`type -path dpkg-deb`" ]; then
     dialog --title "NOT FOUND" --msgbox \
     "The 'dpkg-deb' program is not on the system." 5 50 
       continue;
     fi

    pkg_name
    dialog --title "Creating debian package..." --infobox "$package_name-$version"_i386".deb" 3 50
    sleep 1
        
    dir=$PWD
    package=`cat $TMP/package_name`
    
    trigger $package
    sed -n "/$TRIGGER/,/^$/p" < $LOG_DIR/packages/$package | \
    grep -E -v "$TRIGGER|/$" | sort -u > $TMP/$package_name
     
     cat $TMP/$package_name | sed "s/^\.//g" | sed "s/^\///g" > $TMP/$package_name.s
     cat $TMP/$package_name.s > $TMP/$package_name 
     rm -f $TMP/$package_name.s 
     
     >$TMP/not_found 
     cat $TMP/$package_name | grep -v "^install/" | while read file; do
     if [ ! -e /$file ]; then
     echo $file >>$TMP/not_found 
     else
     echo $file >>$TMP/$package_name.found 
     fi
     done
     
     not_found
     dialog --title "Creating debian package..." --infobox "$package_name-$version"_i386".deb" 3 50
     sleep 1

     rm -rf $TMP/build_deb
     mkdir -p $TMP/build_deb
     cd $TMP/build_deb

     cat $TMP/$package_name.found | grep -v "^install/" | while read file; do
     if [ -d `dirname /$file` ]; then
     if [ ! -d `dirname $file` ]; then
       mkdir -p `dirname $file`
     fi
     fi
     done
     
     cat $TMP/$package_name.found | grep -v "^install/" | while read file; do
     if [ -f /$file ]; then
       cp -p /$file `dirname $file` 
     fi
     done
     
     if [ -s $LOG_DIR/scripts/$package ]; then 
     cp $LOG_DIR/scripts/$package .
     bash $package
     rm -f $package
     fi
     
     package="$package_name"
     
     rm -f $TMP/deps 
     for file in `find * -type f` ; do
     if [ -x "$file" ]; then
       ldd "$file" 2>/dev/null | grep "lib" | awk '{print $1}' 1>> $TMP/deps
     fi
     done

        sort -u $TMP/deps | grep -v "statically" | \
	sed "s/$/,/g" | sed "s/\/lib\///g" > $TMP/deps1
	rm -f $TMP/deps 

	 (echo -n "Depends: " ; echo -n `cat $TMP/deps1`) > $TMP/deps2
	  sed "s/,$//g" $TMP/deps2 > $TMP/deps
          Depends=`cat $TMP/deps`
    
    total_size=0
    for file in `find * -type f` ; do
     size=`du -k $file | cut -f1`
     total_size=$(( $size + $total_size ))
    done

     mkdir -p DEBIAN
    
cat << EOF > DEBIAN/control
Package: $package
Version: $version
Section: base
Priority: optional
Architecture: i386
$Depends
Installed-Size: $total_size
Maintainer: Jerry Donut <jerry@donut.com>
Description: No description 
EOF

while :
do
cat << EOF > $TMP/tmpmsg
This is a chance to edit the control file for the debian package.
Don't change the "Depends:" or "Installed-Size:" lines.

`cat DEBIAN/control | fold -w 72 | head -n 11`

Put the name of the editor you want to use in the box below?
If you don't want to edit the control file just press enter.
EOF
   dialog --title "EDIT CONTROL FILE?" --inputbox "`cat $TMP/tmpmsg`" 24 78 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ ! "$editor" = "" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      dialog --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      $editor DEBIAN/control
      break;    
    fi
    fi
break;    
done    

     package=`grep "^Package: " DEBIAN/control | awk '{print $2}'` 
     version=`grep "^Version: " DEBIAN/control | awk '{print $2}'` 
     arch=`grep "^Architecture: " DEBIAN/control | awk '{print $2}'` 

     dialog --title "Creating debian package..." --infobox "$package-$version"_$arch".deb" 3 50
     sleep 1
     
     make_debian_rpm_compatible
     pause "$TMP/build_deb" 
     strip_binaries
     
     dialog --title "Creating debian package..." --infobox "$package-$version"_$arch".deb" 3 50

     find . -type d -exec chmod 755 {} \;
     find . -type d -exec chown root.root {} \;

     if [ -x "`type -path md5sum`" ]; then
     for file in `find * -type f | grep -v "DEBIAN/control"` ; do
       md5sum $file >> DEBIAN/md5sums
     done
     fi

err_deb() {
 cd $dir && rm -rf $TMP/build_deb
 dialog --title "ERROR from 'dpkg-deb'!" --textbox "$TMP/deb_error" 20 70
 continue;
}

    dpkg-deb -b . $dir/"$package-$version"_$arch".deb" 1>/dev/null 2>$TMP/deb_error || err_deb
    cd $dir && rm -rf $TMP/build_deb

    dialog --title "DONE" --msgbox "$package-$version"_$arch".deb" 5 50
    rm -f $TMP/$package_name.s $TMP/$package_name  
          
     elif [ "$REPLY" = "Rpm" ]; then

     if [ ! -x "`type -path rpm`" ]; then
     dialog --title "NOT FOUND" --msgbox \
     "The 'rpm' program is not on the system." 5 50 
       continue;
     fi
     
    pkg_name 
    dialog --title "Creating rpm package..." --infobox "$package_name-$version-1.i386.rpm" 3 50
    sleep 1
        
    dir=$PWD
    package=`cat $TMP/package_name`
    
    trigger $package
    sed -n "/$TRIGGER/,/^$/p" < $LOG_DIR/packages/$package | \
    grep -E -v "$TRIGGER|/$" | sort -u > $TMP/$package_name
     
     cat $TMP/$package_name | sed "s/^\.//g" | sed "s/^\///g" > $TMP/$package_name.s
     cat $TMP/$package_name.s > $TMP/$package_name 
     rm -f $TMP/$package_name.s 
     
     >$TMP/not_found 
     cat $TMP/$package_name | grep -v "^install/" | while read file; do
     if [ ! -e /$file ]; then
     echo $file >>$TMP/not_found 
     else
     echo $file >>$TMP/$package_name.found 
     fi
     done

     not_found
     dialog --title "Creating rpm package..." --infobox "$package_name-$version-1.i386.rpm" 3 50
     sleep 1

     rm -rf $TMP/build_rpm
     mkdir -p $TMP/build_rpm
     cd $TMP/build_rpm

     cat $TMP/$package_name.found | grep -v "^install/" | while read file; do
     if [ -d `dirname /$file` ]; then
     if [ ! -d `dirname $file` ]; then
       mkdir -p `dirname $file`
     fi
     fi
     done
     
     cat $TMP/$package_name.found | grep -v "^install/" | while read file; do
     if [ -f /$file ]; then
       cp -p /$file `dirname $file` 
     fi
     done
     
     if [ -s $LOG_DIR/scripts/$package ]; then 
     cp $LOG_DIR/scripts/$package .
     bash $package
     rm -f $package
     fi

     package="$package_name"

     make_debian_rpm_compatible
     pause "$TMP/build_rpm" 
     strip_binaries

     dialog --title "Creating rpm package..." --infobox "$package-$version-1.i386.rpm" 3 50

     find . -type d -exec chmod 755 {} \;
     find . -type d -exec chown root.root {} \;
     
     find * -not -type d | sed "s/^/\//g" > $TMP/files
     files="`cat $TMP/files`"

cat << EOF > $package.spec
Summary:   None
Name:      $package
Version:   $version
Release:   1
Copyright: GPL
Group:     None          
Packager:  Jerry Donut <jerry@donut.com>
BuildArchitectures: i386
BuildRoot: $TMP/build_rpm

%description
No description

%files
$files
EOF

while :
do
cat << EOF > $TMP/tmpmsg
This is a chance to edit the spec file for the rpm package.
Don't change the "%files" or "BuildRoot:" lines.

`cat $package.spec | head -n 11`

Put the name of the editor you want to use in the box below?
If you don't want to edit the spec file just press enter.
EOF
   dialog --title "EDIT SPEC FILE?" --inputbox "`cat $TMP/tmpmsg`" 24 78 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ ! "$editor" = "" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      dialog --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      clear 2>/dev/null
      $editor $package.spec
      break;    
    fi
    fi
break;    
done    

err_rpm() {
 cd $dir && rm -rf $TMP/build_rpm
 dialog --title "ERROR from 'rpm'!" --textbox "$TMP/rpm_error" 20 70
 continue;
}

       package=`grep "^Name: " $package.spec | awk '{print $2}'` 
       version=`grep "^Version: " $package.spec | awk '{print $2}'` 
       release=`grep "^Release: " $package.spec | awk '{print $2}'` 
       arch=`grep "^BuildArchitectures: " $package.spec | awk '{print $2}'` 

       dialog --title "Creating rpm package..." --infobox "$package-$version-$release.$arch.rpm" 3 50

       if [ ! -d /usr/src/rpm/RPMS/$arch -a ! -d /usr/src/redhat/RPMS/$arch ]; then
         mkdir -p /usr/src/rpm/RPMS/$arch /usr/src/redhat/RPMS/$arch
       fi
 
       foox
       
       dialog --title "DONE" --msgbox "$package-$version-$release.$arch.rpm" 5 50
       rm -f $TMP/$package_name.s $TMP/$package_name  
     else
     break;
  fi  
done
break;
   else
   break;
   fi
   done
fi
fi

  if [ "$REPLY" = "List" ]; then

while :
do
cat << EOF > $TMP/tmpmsg

Please enter the name of the directory that contains the
rpm, deb, tar.gz, tgz, tar.bz2, or zip package, you 
want to list the contents of.  

Press enter on empty box for current directory.
EOF

   dialog --title "SELECT PACKAGE DIRECTORY" --inputbox "`cat $TMP/tmpmsg`" 13 62 2> $TMP/pkgdir
   if [ $? = 1 ]; then
    rm -f $TMP/pkgdir $TMP/SeT*
    reset 2>/dev/null
    exit
   fi 

   PACKAGE_DIR="`cat $TMP/pkgdir`"
   rm -f $TMP/pkgdir

   if [ "$PACKAGE_DIR" = "" ]; then
   PACKAGE_DIR=$PWD
   elif [ ! -d $PACKAGE_DIR ]; then
    dialog --title "DIRECTORY NOT FOUND" --msgbox "The directory ($PACKAGE_DIR) \
does not seem to exist. Please try again or cancel." \
8 60
    continue;
   fi
 
   dialog --title "Creating list..." --infobox "" 2 30
   sleep 1

    ( cd $PACKAGE_DIR && find * -type f -maxdepth 0 -name "*.deb" -o \
      -name "*.rpm" -o -name "*.tar.gz" -o -name "*.tgz" -o -name "*.tar.bz2" \
      -o -name "*.zip" -o -name "*.ZIP" 2>/dev/null > $TMP/packages )

   if [ ! -s "$TMP/packages" ]; then
    dialog --title "DIRECTORY EMPTY" --msgbox "The directory \n$PACKAGE_DIR \n\
does not contain any rpm, deb, tgz, tar.gz, tar.bz2, or zip packages. \n\
Please try again or cancel." 9 78
    continue;
   fi
    break;
done

   echo 'dialog --menu "Please select the package you wish to list." 22 60 15 \' > $TMP/packages.n
   cat $TMP/packages | sed -e 's/.*/"&" "" \\/' >> $TMP/packages.n
   echo "2> $TMP/return" >> $TMP/packages.n
   
    while [ 0 ]; do
    . $TMP/packages.n
    if [ ! "`cat $TMP/return`" = "" ]; then
   
     package="`cat $TMP/return`"
     dialog --title "Listing package..." --infobox "$package" 3 50
     sleep 1
     echo "PACKAGE NAME: $package" >$TMP/tmpmsg
     echo "PACKAGE SIZE: `du -k $package | cut -f1`K" >>$TMP/tmpmsg
     echo "" >>$TMP/tmpmsg

   if echo `cat $TMP/return` | grep -q "\.tar.bz2$" ; then
     bzip2 -dc $PACKAGE_DIR/`cat $TMP/return` | tar -tvf - >>$TMP/tmpmsg || exit
   elif echo `cat $TMP/return` | grep -q -E "\.tgz|\.tar.gz$" ; then
     tar -tzvvf $PACKAGE_DIR/`cat $TMP/return` >>$TMP/tmpmsg || exit
   elif echo `cat $TMP/return` | grep -q "\.rpm$" ; then
     unrpm < $PACKAGE_DIR/`cat $TMP/return` | gzip -d | cpio -tv --quiet >>$TMP/tmpmsg || exit
   elif echo `cat $TMP/return` | grep -q "\.deb$" ; then
     undeb -p $PACKAGE_DIR/`cat $TMP/return` control.tar.gz | tar -xzO "*control" -f - >>$TMP/tmpmsg 
     echo "" >> $TMP/tmpmsg
     undeb -p $PACKAGE_DIR/`cat $TMP/return` data.tar.gz | tar -tzvf - >>$TMP/tmpmsg || exit
     elif echo `cat $TMP/return` | grep -q -E "\.zip$|\.ZIP$" ; then
     unzip -v $PACKAGE_DIR/`cat $TMP/return` >>$TMP/tmpmsg || exit
   fi

     dialog --title "CONTENTS" --textbox "$TMP/tmpmsg" \
     22 78 2> /dev/null

cat << EOF > $TMP/save

Should i save the content listing to a file named 
'`cat $TMP/return`.txt' in the current directory?

EOF
     dialog --title "SAVE CONTENT LISTING:" --yesno "`cat $TMP/save`" 8 78 2> /dev/null

   if [ $? = 0 ]; then
    mv $TMP/tmpmsg `cat $TMP/return`.txt
   fi 

   else
   break
   fi
   done
   continue
fi

  if [ "$REPLY" = "Configure" ]; then

cat << EOF > $TMP/tmpmsg
You can create a database in "$LOG_DIR/configure" with the
configure and make rules you want for a particular source package. 

Then when you want to configure and make a package start
'pkgtool2', and choose 'Configure' then 'Run' from the menu.

In the next box you'll have the option of running one of the configure
rules files in the database, creating one, or reading/editing one.

Continue?
EOF

    dialog --title "CONFIGURE README" --yesno "`cat $TMP/tmpmsg`" 15 78
   if [ ! $? = 0 ]; then
    rm -f $TMP/tmpmsg
    reset 2>/dev/null
    exit
   fi 

while :
do
cat << EOF > $TMP/tmpmsg

Do you want to run an existing configure rules file
for a package, create one, or read/edit/url one?

EOF

dialog --title "CONFIGURE OPTIONS" --menu "`cat $TMP/tmpmsg`" 12 70 3 \
"Run" "Read, Edit, or extract URLs from configure rules file" \
"Create" "configure rules file" \
"Exit" "configure options" 2>$TMP/reply
  if [ ! $? = 0 ]; then
    rm -f $TMP/tmpmsg $TMP/reply
    break;
  fi 
 
  reply=`cat $TMP/reply`

  if [ "$reply" = "Exit" ]; then
    rm -f $TMP/tmpmsg $TMP/reply
    break;
  fi 
 
  if [ "$reply" = "Run" ]; then

   dialog --title "Creating list..." --infobox "" 2 30
   sleep 1

   ( cd $LOG_DIR/configure 2>/dev/null && find * -type f -maxdepth 0 2>/dev/null > $TMP/packages )

   if [ ! -s "$TMP/packages" ]; then
    dialog --title "DIRECTORY EMPTY" --msgbox "The directory ($LOG_DIR/configure) \
does not seem to contain any configure listings." 8 70
    continue;
   fi
  
   echo 'dialog --menu "Please select a configure rules file." 22 45 15 \' > $TMP/packages.n
   cat $TMP/packages | sed -e 's/.*/"&" "" \\/' >> $TMP/packages.n
   echo "2> $TMP/return" >> $TMP/packages.n
   
    . $TMP/packages.n
      package="`cat $TMP/return`"
    if [ ! "$package" = "" ]; then
      rm -f $TMP/tmpmsg $TMP/reply $TMP/packages* $TMP/return

  dialog --title "CONFIGURE RULES FILE VIEWER" --textbox \
  "$LOG_DIR/configure/$package" 20 78

while :
do   
  dialog --title "CONFIGURE RULES OPTIONS" --menu \
  "$package" 12 68 5 \
"Edit" "configure rules file" \
"Read" "configure rules file" \
"Run" "configure rules file" \
"Url" "connect to any (https, http, ftp) URLs found in file" \
"Exit" "configure rules options" 2>$TMP/reply
  if [ ! $? = 0 ]; then
    rm -f $TMP/tmpmsg $TMP/reply
    break;
  fi 
 
   reply=`cat $TMP/reply`

   if [ "$reply" = "Read" ]; then
     dialog --title "CONFIGURE RULES FILE VIEWER" --textbox \
     "$LOG_DIR/configure/$package" 20 78
     continue;
 
   elif [ "$reply" = "Run" ]; then
while :
do
 dialog --title "CONFIGURE RUN OPTIONS" --menu \
  "$package" 14 50 7 \
"Run_1" "./configure" \
"Run_2" "./configure ; make" \
"Run_3" "./configure ; make ; make install" \
"Run_4" "make" \
"Run_5" "make install" \
"Run_6" "make ; make install" \
"Exit" "configure run options" 2>$TMP/reply
  if [ ! $? = 0 ]; then
    rm -f $TMP/tmpmsg $TMP/reply
    break;
  fi 

   reply=`cat $TMP/reply`

  if [ "$reply" = "Exit" ]; then
    rm -f $TMP/tmpmsg $TMP/reply
    break;
  fi
  
error() {
     echo -en "\n\tPress enter to continue. "     
     read enter
     continue;
}

   if [ "$reply" = "Run_1" ]; then
     bash $LOG_DIR/configure/$package Configure || error
   elif [ "$reply" = "Run_2" ]; then
     bash $LOG_DIR/configure/$package Configure Make || error
   elif [ "$reply" = "Run_3" ]; then
     bash $LOG_DIR/configure/$package Configure Make || error
     monitor "sh $LOG_DIR/configure/$package Make_install"
   elif [ "$reply" = "Run_4" ]; then
     bash $LOG_DIR/configure/$package Make || error
   elif [ "$reply" = "Run_5" ]; then
     monitor "sh $LOG_DIR/configure/$package Make_install"
   elif [ "$reply" = "Run_6" ]; then
     bash $LOG_DIR/configure/$package Make || error
     monitor "sh $LOG_DIR/configure/$package Make_install"
   fi 
   error
done
   
   elif [ "$reply" = "Edit" ]; then
while :
do
cat << EOF > $TMP/tmpmsg

Please enter the name of the editor you want to use.

EOF

   dialog --title "EDITOR" --inputbox "`cat $TMP/tmpmsg`" 9 60 2> $TMP/editor
   if [ ! $? = 0 ]; then
     break;
   fi 
        editor="`cat $TMP/editor`"

   if [ "$editor" = "" ]; then
     break;
   fi
   
   if [ ! -x "`type -path $editor`" ]; then
     dialog --title "NOT FOUND" --msgbox "$editor" 5 30 
     continue;
   else
        clear 2>/dev/null
        cat $LOG_DIR/configure/$package > $TMP/$package
        $editor $LOG_DIR/configure/$package || exit
        dialog --title "DONE" --infobox "Returning to menu." 3 30
	sleep 1
        old_date="`head -n 1 $LOG_DIR/configure/$package 2>/dev/null`"
	if echo $old_date | grep -q "Configure and make rules for" ; then
	if ! cmp $TMP/$package $LOG_DIR/configure/$package 1>/dev/null 2>/dev/null ; then
	  sed "s/$old_date/# Configure and make rules for \'$package\'.  `date +%m-%d-%Y 2>/dev/null`/g" \
	  $LOG_DIR/configure/$package | sed 's/[ 	]*$//g' > $TMP/$package
          skip=yes
	fi
	fi
	if [ ! "$skip" = "yes" ]; then
  	  sed 's/[ 	]*$//g' $LOG_DIR/configure/$package > $TMP/$package
	  unset skip
	fi
	mv $TMP/$package $LOG_DIR/configure/$package
        chmod 644 $LOG_DIR/configure/$package
        chown root.root $LOG_DIR/configure/$package
	rm -f $TMP/tmpmsg $TMP/reply
        break;
        fi
  done
  elif [ "$reply" = "Url" ]; then

get_url() {
###########################################################################
# Configurable section
###########################################################################
#
# Any entry in the lists of programs that urlview handler will try out will
# be made of /path/to/program + ':' + TAG where TAG is one of
# XW: XWindows program
# XT: Launch with an xterm if possible or as VT if not
# VT: Launch in the same terminal

# The lists of programs to be executed are
https_prgs="$2"
http_prgs="$2"
ftp_prgs="$2"

# Program used as an xterm (if it doesn't support -T you'll need to change
# the command line in getprg)
XTERM=/usr/X11R6/bin/xterm

###########################################################################
# Change bellow this at your own risk
###########################################################################
function getprg()
{
    local ele tag prog

    for ele in $*
    do
	tag=${ele##*:}
	prog=${ele%%:*}
	if [ -x "`type -path $prog`" ]; then
	    case $tag in
	    XW)
		[ -n "$DISPLAY" ] && echo "X:$prog" && return 0
		;;
	    XT)
		[ -n "$DISPLAY" ] && [ -x "$XTERM" ] && \
		    echo "X:$XTERM -e $prog" && return 0
		echo "$prog" && return 0
		;;
	    VT)
		echo "$prog" && return 0
		;;
	    esac
	fi
    done
}

URL=$1
url=$1; shift

type=${url%%:*}

if [ "$url" = "$type" ]; then
    type=${url%%.*}
    case $type in
    www|web)
	type=http
	;;
    esac
    url=$type://$url
fi

case $type in
https)
    prg=`getprg $https_prgs`
    ;;
http)
    prg=`getprg $http_prgs`
    ;;
ftp)
    prg=`getprg $ftp_prgs`
    ;;
*)
    dialog --title "Unknown URL type" --msgbox "$URL" 5 70 
    ;;
esac

if [ -n "$prg" ]; then
   if [ "${prg%:*}" = "X" ]; then
    ${prg#*:} $url 2>/dev/null &
   else
    $prg $url || ( echo -en "\n\tPress enter to continue. " ; read enter )
   fi
fi
}

   if ! grep -q -E "http://|https://|ftp://|www.|ftp." $LOG_DIR/configure/$package ; then
     dialog --title "NO URLs FOUND" --msgbox "$package" 5 30 
     continue;
   else
     dialog --title "Extracting URLs from:" --infobox "$package" 3 40
     sleep 1
       rm -f $TMP/url*
       cat $LOG_DIR/configure/$package | sed "s/#//g" | \
       grep -E "http://|https://|ftp://|www.|ftp." | crunch | \
       while read file ; do
       for field in 1 2 3 4 5 ; do
       echo $file | cut -d ' ' -f $field | \
       grep -E "http://|https://|ftp://|www.|ftp." >>$TMP/url.a
     done
     done
       cat $TMP/url.a | sed "/^$/d" | sed "s/^<//g" | \
       sed "s/>$//g" | sed "s/>.$//g" | sed "s/>,$//g" >$TMP/url.b
       if grep -q -E "\.gz$|\.tgz$|\.zip$|\.ZIP$|\.bz2$|\.rpm$|\.deb$" $TMP/url.b ; then
       cat $TMP/url.b | while read file ; do
       if echo $file | grep -q -E "\.gz$|\.tgz$|\.zip$|\.ZIP$|\.bz2$|\.rpm$|\.deb$" ; then
       echo `dirname $file` >> $TMP/url.c
       fi
       done
       cat $TMP/url.c >> $TMP/url.b
       fi
       sort -u $TMP/url.b | sed "/^$/d" >$TMP/url.d
	 
   echo 'dialog --menu "Please select the URL you wish to connect to." 17 78 10 \' > $TMP/urls.n
   cat $TMP/url.d | sed -e 's/.*/"&" "" \\/' >> $TMP/urls.n
   echo "2> $TMP/return" >> $TMP/urls.n

while :
do   
    . $TMP/urls.n
    if [ ! "`cat $TMP/return`" = "" ]; then
      url="`cat $TMP/return`"

cat << EOF > $TMP/tmpmsg

Which program do you want to use to connect to the URL?
$url

EOF

  dialog --title "CONNECT PROGRAMS" --menu "`cat $TMP/tmpmsg`" 15 76 6 \
"ftp" "ftp program" \
"lftp" "ftp program" \
"ncftp" "ftp program" \
"lynx" "https, http, ftp, program" \
"netscape" "https, http, ftp, program" \
"Not Listed" "Another not listed" 2>$TMP/reply
  if [ ! $? = 0 ]; then
    rm -f $TMP/tmpmsg $TMP/reply
    break;
  fi 

    reply=`cat $TMP/reply`

   if [ ! "$reply" = "Not Listed" ]; then
   if [ ! -x "`type -path $reply`" ]; then
     dialog --title "NOT FOUND" --msgbox "$reply" 5 40
     continue;
   fi
   fi

   if [ "$reply" = "ftp" ]; then
      get_url $url "ftp:XT"
      continue;
   elif [ "$reply" = "lftp" ]; then
      get_url $url "lftp:XT"
      continue;
   elif [ "$reply" = "ncftp" ]; then
      get_url $url "ncftp:XT"
      continue;
   elif [ "$reply" = "lynx" ]; then
      get_url $url "lynx:XT"
      continue;
   elif [ "$reply" = "netscape" ]; then
      get_url $url "netscape:XW"
      continue;
   elif [ "$reply" = "Not Listed" ]; then
cat << EOF > $TMP/tmpmsg

Please enter the name of the program in the box below.

If it's a x windows program like 'netscape' put a x
after it's name.  Example: program_name x

EOF

   dialog --title "CONNECT PROGRAM" --inputbox "`cat $TMP/tmpmsg`" 12 60 2> $TMP/program
  if [ ! $? = 0 ]; then
      continue;
  fi 
 
  program=`cat $TMP/program`

  if [ "$program" = "" ]; then
      continue;
  fi    
      X=":XT"
      echo $program | grep -q -i " x" && X=":XW"
      program=`echo $program | cut -d' ' -f1`

    if [ ! -x "`type -path $program`" ]; then
      dialog --title "NOT FOUND" --msgbox \
     "$program" 5 40
      continue;
    fi

      get_url $url $program$X
      continue;
    else
      continue;
    fi
      continue;
    fi 
      break;
    done
    fi
      continue;

   elif [ "$reply" = "Exit" ]; then
        rm -f $TMP/tmpmsg $TMP/reply
        break;
   fi
      done
    else
      rm -f $TMP/tmpmsg $TMP/reply $TMP/packages* $TMP/return
      continue;  
    fi
  
  elif [ "$reply" = "Create" ]; then

  if [ ! -d $LOG_DIR/configure ]; then
    mkdir -m 755 -p $LOG_DIR/configure
    chown root.root $LOG_DIR/configure
  fi

cat << EOF > $TMP/tmpmsg
This is an example of a configure rules file, you supply the
configure, make, and make install information.

Put a '\\' at the end of each configure or make line if there's more than
one, except the last.  You can put some notes after '#' if you want.

# Configure and make rules for 'fetchmail'.  11-27-2001
Configure() {
rm -f config.log config.cache
# Put configure rules here.
./configure --prefix=/usr --sysconfdir=/etc --disable-nls \\
--enable-SDPS --enable-RPA --enable-NTLM --with-ssl=/usr/lib
# Exit if configure returns an error.
[ \$? = 0 ] || exit
}

Make() {
# Put make rules here.
make CFLAGS="-Os -march=i386 -Wall" \\
CXXFLAGS="-Os -march=i386 -Wall" \\
LDFLAGS="-s"
# Exit if make returns an error.
[ \$? = 0 ] || exit
}

Make_install() {
# Put make install rules here.
make install
# Copy any docs you want to /usr/doc/package_name etc.
doc_dir="/usr/doc/\`basename \$PWD\`"
mkdir -p \$doc_dir
chmod 755 \$doc_dir
chown root.root \$doc_dir
cp FAQ README* \$doc_dir
chmod 644 \$doc_dir/*
chown root.root \$doc_dir/*
}

# http://www.tuxedo.org/~esr/fetchmail
# ftp://ftp.ccil.org/pub/esr/fetchmail
#
#

for run in \$1 \$2 \$3 ; do
\$run
done
EOF

   dialog --title "CREATE CONFIGURE RULES FILE" --textbox "$TMP/tmpmsg" 22 78 2> $TMP/editor

while :
do
unset skip
cat << EOF > $TMP/tmpmsg

Please enter a name for the package.

EOF

   dialog --title "CONFIGURE PACKAGE NAME" --inputbox "`cat $TMP/tmpmsg`" 9 42 2> $TMP/name
   if [ ! $? = 0 ]; then
     rm -f $TMP/name $TMP/tmpmsg
     skip=yes
     break;  
   fi 

    NAME="`cat $TMP/name`"

  if [ "$NAME" = "" ]; then
    rm -f $TMP/name $TMP/tmpmsg $TMP/rules
    continue;  
  fi 

 if [ -s "$LOG_DIR/configure/$NAME" ]; then

cat << EOF > $TMP/tmpmsg

There's already a configure rules file
named $NAME.

Continue anyway and overwrite?

EOF

   dialog --title "FOUND" --yesno "`cat $TMP/tmpmsg`" 10 50 2> /dev/null
   if [ $? = 0 ]; then
     rm -f $TMP/name $TMP/tmpmsg
     break;  
   fi 
else
break;
fi
done

if [ ! "$skip" = "yes" ]; then
while :
do
cat << EOF > $TMP/tmpmsg

Please put the name of the editor
you want to use in the box below.

EOF
   dialog --title "EDITOR" --inputbox "`cat $TMP/tmpmsg`" 10 40 2> $TMP/editor
   if [ ! $? = 0 ]; then
     rm -f $TMP/name $TMP/tmpmsg $TMP/editor
     break;  
   fi 

    editor="`cat $TMP/editor`"
  
  if [ "$editor" = "" ]; then
     rm -f $TMP/name $TMP/tmpmsg $TMP/editor
     continue;  
  fi

  if [ ! -x "`type -path $editor`" ]; then
     dialog --title "NOT FOUND" --msgbox "$editor" 5 30 
     rm -f $TMP/name $TMP/tmpmsg $TMP/editor
     continue;  
  else
      echo "# Configure and make rules for '$NAME'.  `date +%m-%d-%Y 2>/dev/null`" > $LOG_DIR/configure/$NAME
      echo "Configure() {" >> $LOG_DIR/configure/$NAME
      echo "rm -f config.log config.cache" >> $LOG_DIR/configure/$NAME
      echo "# Put configure rules here." >> $LOG_DIR/configure/$NAME
      echo "./configure" >> $LOG_DIR/configure/$NAME
      echo "# Exit if configure returns an error." >> $LOG_DIR/configure/$NAME
      echo '[ $? = 0 ] || exit' >> $LOG_DIR/configure/$NAME
      echo "}" >> $LOG_DIR/configure/$NAME
      echo "" >> $LOG_DIR/configure/$NAME
      echo "Make() {" >> $LOG_DIR/configure/$NAME
      echo "# Put make rules here." >> $LOG_DIR/configure/$NAME
      echo "make" >> $LOG_DIR/configure/$NAME
      echo "# Exit if make returns an error." >> $LOG_DIR/configure/$NAME
      echo '[ $? = 0 ] || exit' >> $LOG_DIR/configure/$NAME
      echo "}" >> $LOG_DIR/configure/$NAME
      echo "" >> $LOG_DIR/configure/$NAME
      echo "Make_install() {" >> $LOG_DIR/configure/$NAME
      echo "# Put make install rules here." >> $LOG_DIR/configure/$NAME
      echo "make install" >> $LOG_DIR/configure/$NAME
      echo "# Copy any docs you want to /usr/doc/package_name etc." >> $LOG_DIR/configure/$NAME
      echo '#doc_dir="/usr/doc/`basename $PWD`"' >> $LOG_DIR/configure/$NAME
      echo '#mkdir -p $doc_dir' >> $LOG_DIR/configure/$NAME
      echo '#chmod 755 $doc_dir' >> $LOG_DIR/configure/$NAME
      echo '#chown root.root $doc_dir' >> $LOG_DIR/configure/$NAME
      echo '#cp README* $doc_dir' >> $LOG_DIR/configure/$NAME
      echo '#chmod 644 $doc_dir/*' >> $LOG_DIR/configure/$NAME
      echo '#chown root.root $doc_dir/*' >> $LOG_DIR/configure/$NAME
      echo "}" >> $LOG_DIR/configure/$NAME
      echo "" >> $LOG_DIR/configure/$NAME
      echo "#" >> $LOG_DIR/configure/$NAME
      echo "#" >> $LOG_DIR/configure/$NAME
      echo "#" >> $LOG_DIR/configure/$NAME
      echo "#" >> $LOG_DIR/configure/$NAME
      echo "" >> $LOG_DIR/configure/$NAME
      echo 'for run in $1 $2 $3 ; do' >> $LOG_DIR/configure/$NAME
      echo '$run' >> $LOG_DIR/configure/$NAME
      echo "done" >> $LOG_DIR/configure/$NAME
      clear 2>/dev/null
      $editor $LOG_DIR/configure/$NAME || exit
      dialog --title "DONE" --infobox "Returning to menu." 3 30
      sleep 1
      sed 's/[ 	]*$//g' $LOG_DIR/configure/$NAME > $TMP/$NAME
      mv $TMP/$NAME $LOG_DIR/configure/$NAME
      chmod 644 $LOG_DIR/configure/$NAME
      chown root.root $LOG_DIR/configure/$NAME
      rm -f $TMP/name $TMP/tmpmsg $TMP/editor $TMP/$NAME*
      break;  
  fi
done  
fi
fi
done
fi

  if [ "$REPLY" = "Monitor" ]; then

cat << EOF > $TMP/tmpmsg
Yow can monitor a command like 'make install' and create a record.

The record will be put in $LOG_DIR/packages/package_name,
and can be removed like any installed package.

Usually when you compile a package you do this, more or less.
# ./configure --prefix=/usr ; make ; make install

Instead of 'make install' do 'pkgtool2' and choose monitor from the menu.
# ./configure --prefix=/usr ; make ; pkgtool2

You'll have the option of creating a slackware tgz, debian deb, or
rpm package of the install, after the install or later using
$LOG_DIR/packages/package_name as the guide.

NOTE: Before you do 'pkgtool2' you can do a test run 'make install -n',
      to see if there are any problems.

If you don't want to install a package on the system you can
install it to a temporary directory, and create a tgz, rpm, or
deb package from it.

This won't work for all packages, only packages that
use automake/autoconf and have a 'configure' file.

This example below assumes you put /usr as the prefix when
you did \`./configure --prefix=/usr'.
 
Instead of 'make install' do this.

make install prefix=/temp/usr DESTDIR=/temp

Then run 'pkgtool2' and choose 'Create' from the menu, then
choose 'directory' and put '/temp'.

Create tgz, rpm, or deb package from contents of file or directory

EOF

    dialog --title "MONITOR README" --textbox "$TMP/tmpmsg" 22 78
   if [ ! $? = 0 ]; then
    rm -f $TMP/tmpmsg
    reset 2>/dev/null
    exit
   fi 
   monitor
fi

  if [ "$REPLY" = "Convert" ]; then

rm -f $TMP/current_done

while :
do
cat << EOF > $TMP/tmpmsg

Please enter the name of the directory that contains 
the rpm and or deb packages you want to convert:  

Press enter on empty box for current directory.
EOF

   dialog --title "SELECT PACKAGE DIRECTORY" --inputbox "`cat $TMP/tmpmsg`" 12 60 2> $TMP/pkgdir
   if [ $? = 1 ]; then
    rm -f $TMP/pkgdir $TMP/SeT*
    break;
   fi 

   PACKAGE_DIR="`cat $TMP/pkgdir`"
   rm -f $TMP/pkgdir

   if [ "$PACKAGE_DIR" = "" ]; then
   PACKAGE_DIR=$PWD
   elif [ ! -d $PACKAGE_DIR ]; then
    dialog --title "DIRECTORY NOT FOUND" --msgbox "The directory ($PACKAGE_DIR) \
does not seem to exist. Please try again or cancel." 8 60
    continue;
   fi

   dialog --title "Creating list..." --infobox "" 2 30
   sleep 1

   ( cd $PACKAGE_DIR && find * -type f -maxdepth 0 -name "*.deb" -o -name \
     "*.rpm" 2>/dev/null | grep -v "\.src.rpm$" 2>/dev/null > $TMP/packages )

   if [ ! -s "$TMP/packages" ]; then
    dialog --title "DIRECTORY EMPTY" --msgbox "The directory \n$PACKAGE_DIR \n\
does not contain any rpm or deb packages. Please try again or cancel." \
9 78
    continue;
   fi

error_install() {
cat << EOF > $TMP/tmpmsg

Could not install '`cat $TMP/current_done`' 
for some reason.

`cat $TMP/error`

EOF
dialog --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
}

   echo 'dialog --menu "Please select the package you wish to convert." 22 55 15 \' > $TMP/packages.n
   cat $TMP/packages | sed -e 's/.*/"&" "" \\/' >> $TMP/packages.n
   echo "2> $TMP/return" >> $TMP/packages.n
   >$TMP/done
   
    while [ 0 ]; do
   if [ -s $TMP/current_done ]; then 

cat << EOF > $TMP/tmpmsg
Do you want to install this converted package?

`cat $TMP/current_done`

It's generally not a good idea to install rpm or
deb packages on a slackware system, unless you
know what you're doing.
EOF

  dialog --title "INSTALL PACKAGE" --menu "`cat $TMP/tmpmsg`" 16 60 3 \
"Install" "Install package" \
"List" "List contents of package" \
"Exit" "Exit" 2> $TMP/reply

    reply=`cat $TMP/reply`

    if [ "$reply" = "Install" ]; then
     dialog --title "Installing..." --infobox "`cat $TMP/current_done`" 4 60 
     sleep 2
     installpkg `cat $TMP/current_done` >/dev/null 2>$TMP/error || error_install
    elif [ "$reply" = "List" ]; then
     dialog --title "Creating list..." --infobox "" 2 30
     sleep 1
     tar -tzvvf `cat $TMP/current_done` >$TMP/tmpmsg 
     if grep -q "install/doinst.sh" $TMP/tmpmsg ; then
     echo "" >>$TMP/tmpmsg 
     echo "This is the contents of the 'install/doinst.sh' script." >>$TMP/tmpmsg 
     echo "" >>$TMP/tmpmsg 
     tar -xzO "install/doinst.sh" -f `cat $TMP/current_done` >>$TMP/tmpmsg 
     fi
     dialog --title "CONTENTS: `cat $TMP/current_done`" --textbox "$TMP/tmpmsg" 22 78 2>/dev/null
     continue
    fi
   fi

     if [ -s $TMP/done ]; then
     if grep -q "^\"" $TMP/packages.n ; then
       remove=`cat $TMP/done`
       cat $TMP/packages.n | sed "/^\"$remove\"/d" > $TMP/packages.a
       mv $TMP/packages.a $TMP/packages.n
     if ! grep -q "^\"" $TMP/packages.n ; then
       break
     fi     
     else
       break;
     fi
    fi

    >$TMP/done
    >$TMP/current_done
    . $TMP/packages.n

   if [ ! "`cat $TMP/return`" = "" ]; then
    convert_package $PACKAGE_DIR/`cat $TMP/return`
   else
   break
   fi
   done
   done
   continue
fi
done
