#!/bin/bash
#
# Copyright 2005, Kent Robotti <robotti@godmail.com> (8-7-2005)
#    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`
   XDIALOG=`type -path Xdialog`
   if [ "$DISPLAY" = ":0.0" ]; then
   if [ ! -x "$DIALOG" -a ! -x "$XDIALOG" ]; then
       echo "This script requires the \`dialog' program to run."
       exit 1
   fi
   elif [ ! -x "$DIALOG" ]; then
       echo "This script requires the \`dialog' program to run."
       exit 1
   fi

root=root.root

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

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

#############################################################################

   extract_links() {
     grep " ln -sf " | while read file ; do
         dir=`echo $file | awk '{print $3}'`
         link=`echo $file | awk '{print $8}'`
         echo "$dir/$link"
     done
   }
   extract_links_to() {
     grep " ln -sf " | while read file ; do
         dir=`echo $file | awk '{print $3}'`
         link=`echo $file | awk '{print $8}'`
         link_to=`echo $file | awk '{print $7}'`
         echo "$dir/$link $link_to"
     done
   }

trigger() {
PKGNAME=$1
  if grep -q "^\.\/$" $LOG_DIR/packages/$PKGNAME 2>/dev/null; then
      TRIGGER="^\.\/$"
  elif grep -q "PACKAGE CONTENTS:" $LOG_DIR/packages/$PKGNAME 2>/dev/null; then
      TRIGGER="PACKAGE CONTENTS:"
  else
      rm -rf $TMP
      exit 1
  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 70 

if [ "$skip" = "yes" ]; then
    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
}

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
}

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=`du -k $file | awk '{print $1}'`  
  echo "$file" | sed -e 's/.*/"&" "('$doc_size'K)" 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 $TMP/$1$doc_dir/$file
  done
 fi 
fi
fi
}

# Install package.
install_package() {	

package_dir="$LOG_DIR/packages"
package="$1"
shortname="$2"
version="$3"
  
  echo "PACKAGE NAME: $shortname" > $package_dir/$shortname
  echo "PACKAGE VERSION: $version" >> $package_dir/$shortname
  COMPRESSED=`gzip -l $package | grep -v uncompressed_name | awk '{print $1}'`
  UNCOMPRESSED=`gzip -l $package | grep -v uncompressed_name | awk '{print $2}'`
  echo "COMPRESSED PACKAGE SIZE: `expr $COMPRESSED / 1024`K" >> $package_dir/$shortname
  echo "UNCOMPRESSED PACKAGE SIZE: `expr $UNCOMPRESSED / 1024`K" >> $package_dir/$shortname
  echo "INSTALL COMMAND: tar -xzUlpvf `basename $package` -C /" >> $package_dir/$shortname
  echo "INSTALL DATE: `date +%c 2>/dev/null`" >> $package_dir/$shortname
  echo "DESCRIPTION:" >> $package_dir/$shortname 
  echo "$shortname: Put description here!" >> $package_dir/$shortname 
  echo "$shortname:" >> $package_dir/$shortname 
 
  tar -xzUlpvf $package -C / >> $TMP/$shortname 2>/dev/null

          cat $TMP/$shortname | sed "/^$/d" | sed "/^\.\/$/d" | \
          sed "s/^\.\///g" | sed "s/^\///g" > $TMP/$shortname.d 

    rm -f $TMP/dep*
    if [ -x "`type -path ldd`" -a -x "`type -path fmt`" -a -x "`type -path awk`" -a -x "`type -path file`" ]; then
    cat $TMP/$shortname.d | while read file; do
    if [ -x "/$file" -a ! -d "/$file" ]; then
     if file "/$file" 2>/dev/null | grep -q " ELF "; then
         ldd "/$file" 2>/dev/null | grep "lib" | awk '{print $1}' 1>> $TMP/deps
     fi
    fi
    done
        
        sort -u $TMP/deps | grep -v -E "dynamic|statically|:$" > $TMP/deps1
	 
	cat $TMP/deps1 | while read file; do
            echo -n "$file, " >> $TMP/deps2
        done

	cat $TMP/deps2 | sed "s/, $//g" > $TMP/deps3
	cat $TMP/deps3 | fmt -w 70 > $TMP/deps4
	
	cat $TMP/deps4 | while read file; do
 	    echo "LIB DEPENDS: $file" >> $package_dir/$shortname 
        done
    fi

    if [ -f /install/doinst.sh ]; then
         (cd / ; sh install/doinst.sh 2>/dev/null)
	 cp /install/doinst.sh $LOG_DIR/scripts/$shortname
         rm -f /install/doinst.sh
         rmdir /install 2>/dev/null
       echo "SYMBOLIC LINK SCRIPT: $LOG_DIR/scripts/$shortname" >> $package_dir/$shortname 
       chmod 755 $LOG_DIR/scripts/$shortname
       chown $root $LOG_DIR/scripts/$shortname
    fi 
          echo "PACKAGE CONTENTS:" >> $package_dir/$shortname
	  cat $TMP/$shortname.d | sed "/^$/d" | sed "s/^\///g" | sort -u >> $package_dir/$shortname 
	  rm -f $TMP/$shortname.d $TMP/$shortname

          ldconfig 2>/dev/null
          hash -r

cat << EOF > $TMP/tmpmsg

Package Log: $LOG_DIR/packages/$shortname

EOF

   $DIALOG --title "DONE" --msgbox "`cat $TMP/tmpmsg`" 7 76 
}
# End of install package.

# Remove package.
remove_package() {

delete_links() {
 while read LINK ; do
  if [ -L "/$LINK" ]; then
     echo "  --> Deleting symlink "/$LINK"" >> $TMP/removed.$package
     rm -f "/$LINK"
  else
     echo "  --> No such symlink "/$LINK"" >> $TMP/removed.$package
  fi
 done
}

delete_files() {
 while read FILE ; do
  if [ ! -d "/$FILE" ]; then
   if [ -f "/$FILE" ]; then
      echo "  --> Deleting file /$FILE" >> $TMP/removed.$package
      rm -f "/$FILE"
   else
      echo "  --> No such file /$FILE" >> $TMP/removed.$package
  fi
  fi
 done
}

delete_dirs() {
 sort -r | \
 while read DIR ; do
  if [ -d "/$DIR" ]; then
      if [ `ls "/$DIR" | wc -l` -eq 0 ]; then
         echo "  --> Deleting empty directory "/$DIR"" >> $TMP/removed.$package
         rmdir "/$DIR" 2>/dev/null
      fi
  fi
 done
}

remove_packages() {

PKGNAME=$1

    if [ -r $LOG_DIR/scripts/$PKGNAME ]; then
       extract_links < $LOG_DIR/scripts/$PKGNAME | sort -u > $TMP/del_link_list
       delete_links < $TMP/del_link_list
    fi
    
    trigger $PKGNAME
    sed -n "/$TRIGGER/,/^$/p" < $LOG_DIR/packages/$PKGNAME | \
    grep -F -v "$TRIGGER" | sort -u > $TMP/delete_list
    delete_files < $TMP/delete_list
    delete_dirs < $TMP/delete_list
     
    rm -f $TMP/delete_list $TMP/del_link_list 

    for DIR in $LOG_DIR/removed_packages $LOG_DIR/removed_scripts ; do
        if [ ! -d $DIR ] ; then mkdir -m 755 -p $DIR ; chown $root $DIR ; fi
    done
        mv $LOG_DIR/packages/$PKGNAME $LOG_DIR/removed_packages
    if [ -r $LOG_DIR/scripts/$PKGNAME ]; then
        mv $LOG_DIR/scripts/$PKGNAME $LOG_DIR/removed_scripts
    fi
}

remove_packages $1 2>/dev/null
}
# End of remove package.

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 [ -n "$pkgname"]; then
     package_name=$pkgname
   else
     package_name=$package
   fi 

if [ ! "$1" = "" ]; then
version=$1
elif [ "$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 [ -n "$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 -R -p 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 -R -p 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 -R -p 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 -R -p 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 -o usr/share/doc ]; then
     if [ -d /usr/share/doc/packages ]; then
     if [ ! -d usr/share/doc/packages ]; then
        mkdir -p usr/share/doc/packages
        cp -R -p usr/doc/* usr/local/doc/* usr/share/doc/* usr/share/doc/packages 2>/dev/null 
     fi
     elif [ ! -d usr/share/doc ]; then
        mkdir -p usr/share/doc
        cp -R -p usr/doc/* usr/local/doc/* usr/share/doc 2>/dev/null 
     fi
        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 -R -p 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_slackware_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
}

make_generic_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 2>/dev/null )"
  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
  if [ "$3" = "slackware" ]; then
  make_slackware_install_script $TMP/links >> install/doinst.sh
  elif [ "$3" = "generic" ]; then
  make_generic_install_script $TMP/links >> install/doinst.sh
  fi  
  find . -type l -exec rm -f {} \;
  chmod 644 install/doinst.sh
  chown $root install/doinst.sh
fi
 if [ "$2" = "y" ]; then
   find . -type d -exec chmod 755 {} \; 
   find . -type d -exec chown $root {} \;
   find . -type f -exec chown $root {} \;
 fi
tar -cpf $1 * 2>$TMP/error || error
gzip -9 $1 2>$TMP/error || error
}

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

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

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

Press enter, when ready to continue.
EOF

   $DIALOG --title "PAUSE" --msgbox "`cat $TMP/pause`" 14 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 1
    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 [ "$2" = "generic" ]; then
mku=".mku"
fi

if echo $1 | grep -q "\.rpm$" ; then
if [ ! -x "`type -path rpm2cpio`" ]; then
$DIALOG --title "NOT FOUND" --msgbox "Can't find 'rpm2cpio' 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
rpm2cpio $1 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.rpm

$D.rpm -> $D$mku.tgz

EOF

$DIALOG --title "CONVERTING" --infobox "`cat $TMP/tmpmsg.rpm`" 5 78
sleep 1

error() {
cd $dir && rm -rf $TMP/build_rpm
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 
rpm2cpio $1 | 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][-._]//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.rpm`" 5 78
sleep 1

if [ "$2" = "generic" ]; then
mkpkg $D.tar y generic
mv $D.tar.gz $dir/$D.mku.tgz && cd $dir && rm -rf $TMP/build_tgz
$DIALOG --title "DONE" --msgbox "$D.mku.tgz" 5 50
else
mkpkg $D.tar y slackware
mv $D.tar.gz $dir/$D.tgz && cd $dir && rm -rf $TMP/build_tgz
$DIALOG --title "DONE" --msgbox "$D.tgz" 5 50
fi
echo $D.rpm > $TMP/done 
if [ "$2" = "generic" ]; then
echo $D.mku.tgz > $TMP/current_done 
else
echo $D.tgz > $TMP/current_done 
fi
continue
elif echo $1 | grep -q "\.deb$" ; then
if [ ! -x "`type -path ar`" ]; then
$DIALOG --title "NOT FOUND" --msgbox "Can't find 'ar' 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
ar -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.deb

$D.deb -> $D$mku.tgz

EOF
$DIALOG --title "CONVERTING" --infobox "`cat $TMP/tmpmsg.deb`" 5 78
sleep 1

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="`ar -p $1 control.tar.gz | tar -xzO "*control" | \
grep "^Version: " | cut -d: -f2- | cut -d: -f2 | sed "s/^[ ]*//g"`"
ar -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.deb`" 5 78
sleep 1

if [ "$2" = "generic" ]; then
mkpkg $D.tar y generic
mv $D.tar.gz $dir/$D.mku.tgz && cd $dir && rm -rf $TMP/build_tgz
$DIALOG --title "DONE" --msgbox "$D.mku.tgz" 5 50
else
mkpkg $D.tar y slackware
mv $D.tar.gz $dir/$D.tgz && cd $dir && rm -rf $TMP/build_tgz
$DIALOG --title "DONE" --msgbox "$D.tgz" 5 50
fi
echo $D.deb > $TMP/done 
if [ "$2" = "generic" ]; then
echo $D.mku.tgz > $TMP/current_done 
else
echo $D.tgz > $TMP/current_done 
fi
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 an error." >> $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 -rf $TMP
    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 -rf $TMP
    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.

If it's some kind of install script that requires you
to answer questions, put \`script' after the command. 

Example: ./install.sh script
EOF

   $DIALOG --title "ENTER COMMAND" --inputbox "`cat $TMP/tmpmsg`" 20 60 2> $TMP/command
   if [ ! $? = 0 ]; then
    rm -rf $TMP
    reset 2>/dev/null
    exit
   fi 

    COMMAND="`cat $TMP/command`"

    if [ -z "$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 [ -n "$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
num=12
else
cat << EOF > $TMP/tmpmsg

Please enter a name in the box for the package. 

If you leave the box blank, the name
'$(basename $(pwd) | sed "s/[-._][0-9].*//g")' will be used.

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

For example, 'fooboo' not 'fooboo-1.0.0'.

EOF
num=17
fi

   $DIALOG --title "PACKAGE NAME" --inputbox "`cat $TMP/tmpmsg`" $num 70 2> $TMP/pkgname
   if [ ! $? = 0 ]; then
    rm -f $TMP/pkgname $TMP/SeT*
    continue;
   fi 
 
   pkgname="`cat $TMP/pkgname`" 
   
   unset skip
   if [ -z "$pkgname" ]; then
   if [ "$upgrade" = "yes" ]; then
   package="$upgrade_package"
   skip=yes
   else
   package="$(basename $(pwd) | sed "s/[-._][0-9].*//g")"
   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.

Choose another name, or cancel.

EOF
   $DIALOG --title "PACKAGE NAME EXIST" --inputbox "`cat $TMP/tmpmsg`" 12 70 2> $TMP/pkgname
   if [ ! $? = 0 ]; then
    rm -rf $TMP
    reset 2>/dev/null
    exit
   fi 

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

version="$(basename $(pwd) | 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")"
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 [ -n "$new_version" ]; then
     version=$new_version
   else
     version=$version
   fi 

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 70 2> $TMP/backup
   if [ $? = 0 ]; then
   backup="$LOG_DIR/backups/$package" 
   mkdir -p $backup && export INSTALLWATCH_BACKUP_PATH=$backup
   fi 

    if echo $COMMAND | grep -q " script" ; then   
      COMMAND=`echo $COMMAND | sed "s/ script//g"`  
      script=yes
    fi

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

    export INSTALLWATCHFILE="$TMP/$package"
    >$INSTALLWATCHFILE
    export LD_PRELOAD="/usr/lib/installwatch.so"
    
    if [ "$script" = "yes" ]; then   
      $COMMAND || exit
      echo -n "Press enter to continue. "
      read enter
    else
      $COMMAND 1>$TMP/output 2>$TMP/error || error 
    fi
    
    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 Record..." --infobox "$package" 3 50
    sleep 2
    
    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
    rm -rf $TMP
    exit 1
    fi

    rm -f $TMP/$package.b $TMP/$package.bb 
    cat $TMP/$package.a | while read file ; do
    if [ ! -L "$file" ]; then
    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
    fi
    done
    
    if [ ! -s $TMP/$package.bb ]; then
    $DIALOG --title "There was a problem. I can't continue!" --msgbox "" 4 50
    rm -rf $TMP
    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" | \
    sed "s/^\///g" | sort -u > $TMP/$package.s
    rm -f $TMP/$package.script 
    cat $TMP/$package.s | while read file; do
    points_to="`ls -l "/$file" | awk '{print $11}'`"
    echo "$file $points_to" >> $TMP/$package.ss
    done
    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 "PACKAGE VERSION: $version" >> $logdir/$package 
    echo "INSTALL COMMAND: $COMMAND" >> $logdir/$package 
    echo "PACKAGE DESCRIPTION:" >> $logdir/$package 
    echo "Put description here!" >> $logdir/$package 

    rm -f $TMP/deps*
    if [ -x "`type -path ldd`" -a -x "`type -path fmt`" -a -x "`type -path awk`" -a -x "`type -path file`" ]; then
    cat $TMP/$package.b | while read file; do
    if [ -x "/$file" -a ! -d "/$file" ]; then
     if file "/$file" 2>/dev/null | grep -q " ELF "; then
       ldd "/$file" 2>/dev/null | grep "lib" | awk '{print $1}' 1>> $TMP/deps
     fi
    fi
    done
    
        sort -u $TMP/deps | grep -v -E "dynamic|statically|:$" > $TMP/deps1
	 
	cat $TMP/deps1 | while read file; do
        echo -n "$file, " >> $TMP/deps2
        done

	cat $TMP/deps2 | sed "s/, $//g" > $TMP/deps3
	cat $TMP/deps3 | fmt -w 70 > $TMP/deps4
	
 	echo "LIBRARY DEPENDENCIES:" >> $logdir/$package 
	cat $TMP/deps4 | while read file; do
 	  echo "$file" >> $logdir/$package 
        done
    fi
    
    echo "DATE: `date +%c 2>/dev/null`" >> $logdir/$package 
    
    if [ -s $TMP/$package.script ]; then 
    mv $TMP/$package.script $LOG_DIR/scripts/$package
    chmod 755 $LOG_DIR/scripts/$package
    chown $root $LOG_DIR/scripts/$package
    echo "SYMBOLIC LINK SCRIPT: $LOG_DIR/scripts/$package" >> $logdir/$package
    fi

    echo "PACKAGE CONTENTS:" >> $logdir/$package 
    cat $TMP/$package.b | sed "/^$/d" | sed "s/^\///g" | sort -u >> $logdir/$package

 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 "PACKAGE CONTENTS:" > $LOG_DIR/packages/$package-delete
  comm -13 $TMP/$package-new-upgraded $TMP/$package-old-upgraded | \
  sed "/^$/d" >> $LOG_DIR/packages/$package-delete
 
  OLD_delete="`grep -v -E "PACKAGE CONTENTS:|^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
     remove_package $package-delete 1>/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, Debian, RPM, or
Generic tgz package from what you just installed?

EOF

  $DIALOG --title "TGZ/RPM/DEB/GENERIC" \
  --menu "`cat $TMP/tmpmsg`" 14 70 5 \
"Debian" "Create Debian package" \
"Slackware" "Create Slackware package" \
"Rpm" "Create RPM package" \
"Generic" "Create generic tgz package (Exact copy of install!)" \
"None" "Don't Create a package" 2> $TMP/reply
    if [ ! $? = 0 ]; then
      EXIT=YES     
    fi
    
     REPLY="`cat $TMP/reply`"
     
     if [ "$REPLY" = "Slackware" -o "$REPLY" = "Generic" ]; then
     pkg_name $version

     if [ "$REPLY" = "Slackware" ]; then
     $DIALOG --title "Creating Slackware package..." --infobox "$package_name-$version.tgz" 3 50
     else
     $DIALOG --title "Creating Generic package..." --infobox "$package_name-$version.mku.tgz" 3 50
     fi
     
     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
     if [ "$REPLY" = "Slackware" ]; then
     make_slackware_install_script $TMP/$package.ss >> install/doinst.sh
     else
     cp $LOG_DIR/scripts/$package install/doinst.sh
     fi
     chmod 644 install/doinst.sh
     chown $root install/doinst.sh
     fi

     package="$package_name"
    
     if [ "$REPLY" = "Slackware" ]; then
     make_slackware_compatible
     fi

     if echo "$COMMAND" | grep "make " | grep -q " install" ; then
     if [ "$REPLY" = "Slackware" ]; then
       cd $dir
       docs build_tgz /usr/doc $package-$version
       cd $TMP/build_tgz
     else
       cd $dir
       docs build_tgz /usr/doc $package-$version
       cd $TMP/build_tgz
     fi
     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;
}

     if [ "$REPLY" = "Slackware" ]; then
     $DIALOG --title "Creating Slackware package..." --infobox "$package-$version.tgz" 3 50
     else
     $DIALOG --title "Creating Generic package..." --infobox "$package-$version.mku.tgz" 3 50
     fi
     find . -type d -exec chmod 755 {} \;
     find . -type d -exec chown $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      
     if [ "$REPLY" = "Slackware" ]; then
     mv $package-$version.tar.gz $dir/$package-$version.tgz
     else
     mv $package-$version.tar.gz $dir/$package-$version.mku.tgz
     fi
     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 $version
     $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
       if [ -d /usr/share/doc/packages ]; then
       docs build_rpm /usr/share/doc/packages $package
       else
       docs build_rpm /usr/share/doc $package-$version
       fi
       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 {} \;
     
     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 $EDITOR 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ -n "$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
       
         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
    
     pkg_name $version
     $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

        if [ -s $TMP/deps ]; then
        sort -u $TMP/deps | grep -v -E "dynamic|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`
        else
          Depends="Depends:"
        fi
       
    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 $EDITOR 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ -n "$editor" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      $DIALOG --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      clear 2>/dev/null
      $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 {} \;

     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-$version.tgz * ) 
    rm -rf $backup
    mkdir -m 755 -p $backup
    chown $root $backup
    mv $LOG_DIR/backups/$package-$version.tgz $backup
cat << EOF >> $TMP/tmpmsg
The backup is here:
'$backup/$package-$version.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`" 10 70 

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

 while [ 0 ]; do
  $DIALOG --title "Make uninstall v2.2.1 Kent Robotti <robotti@godmail.com>" \
--menu "\nWhich option would you like?\n" 19 78 10 \
"Convert" "RPM or DEB package to Slackware or generic tgz package" \
"List_1" "contents of rpm/deb/tgz/tar.gz/tar.bz2/zip package" \
"List_2" "contents of packages in $LOG_DIR/packages" \
"Configure" "& make rules for packages (run/create/read/edit/url)" \
"Monitor" "make install and create record of install" \
"Install" "generic created X.mku.tgz package" \
"Remove" "package listed in $LOG_DIR/packages" \
"Restore" "backup for $LOG_DIR/packages/package_name" \
"Create" "tgz/rpm/deb package from contents of file or directory" \
"Exit" "Exit make uninstall" 2> $TMP/reply
  if [ ! $? = 0 ]; then
   rm -rf $TMP
   reset 2>/dev/null
   exit
  fi
  REPLY="`cat $TMP/reply`"
  rm -f $TMP/reply
  if [ "$REPLY" = "Exit" ]; then
   rm -rf $TMP
   reset 2>/dev/null
   exit
  fi

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

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

    $DIALOG --title "Creating List..." --infobox "" 2 30
    sleep 1

   echo '$DIALOG --menu "Please select the package you wish to remove." 22 52 15 \' > $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`"
     $DIALOG --title "Removing..." --infobox "$package" 3 40
     sleep 1
     remove_package $package 2>/dev/null 
     $DIALOG --title "REMOVED: $package" --textbox "$TMP/removed.$package" 22 78 2> /dev/null
     rm -f $TMP/removed.$package
     if grep -q "^\"" $TMP/packages ; then
     cat $TMP/packages | sed "/^\"$package\"/d" > $TMP/packages.a
     mv $TMP/packages.a $TMP/packages
     if ! grep -q "^\"" $TMP/packages ; then
     break
     fi     
     else
     break;      
     fi
     else
     break;
     fi
    done

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

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

Please enter the name of the directory that contains 
the generic x.mku.tgz package, you want to install. 

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
     rm -rf $TMP
     exit
   fi 

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

   if [ -z "$directory" ]; then
    directory=$PWD
   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

   $DIALOG --title "Creating List..." --infobox "" 2 30
   sleep 1

    ( cd $directory && find * -type f -maxdepth 0 -name "*.mku.tgz" 2>/dev/null > $TMP/packages )

   if [ ! -s "$TMP/packages" ]; then
    $DIALOG --title "DIRECTORY EMPTY" --msgbox "The directory \n$directory \n\
does not contain any generic x.mku.tgz packages. \n\
Please try again or cancel." 9 78
    continue;
   fi
  break;
done

   echo '$DIALOG --menu "Please select the package you wish to install." 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
    unset skip
     
     if [ "$remove" = "yes" ]; then   
     if grep -q "^\"" $TMP/packages.n ; then
     package="`cat $TMP/return`"
     cat $TMP/packages.n | sed "/^\"$package\"/d" > $TMP/packages.a
     mv $TMP/packages.a $TMP/packages.n
     if ! grep -q "^\"" $TMP/packages.n ; then
     break
     fi     
     else
     break;
     fi
     fi

     unset remove
    . $TMP/packages.n
    if [ ! "`cat $TMP/return`" = "" ]; then
    package="`cat $TMP/return`"
   
    $DIALOG --title "Checking tgz package for errors..." --infobox "$package" 3 50
    sleep 1
    gzip -t $directory/$package 1>/dev/null 2>/dev/null || error=yes
    if [ "$error" = "yes" ]; then 
cat << EOF > $TMP/tmpmsg
	
There's something wrong with the '$package'
package, you probably shouldn't install it. 

EOF
    $DIALOG --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 8 70
    continue;
    fi
    else
    skip=yes
    break;
    fi

     if [ ! "$skip" = "yes" ]; then
    
     pkgname="`basename $package .mku.tgz | sed "s/[-._][0-9].*//g"`"

     if echo $package | grep -q "[0-9]mdk.i[3-6]86" ; then
     version="`basename $package .mku.tgz | 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
     version="`basename $package .mku.tgz | 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
     if [ "$version" = "" ]; then
     version=0.0.0
     fi

  if [ -s "$LOG_DIR/packages/$pkgname" ]; then
while :
do
 
cat << EOF > $TMP/tmpmsg

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

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

Choose another name or cancel.

EOF

   $DIALOG --title "PACKAGE NAME EXIST" --inputbox "`cat $TMP/tmpmsg`" 16 64 2> $TMP/pkgname
   if [ ! $? = 0 ]; then
    rm -f $TMP/pkgname $TMP/SeT*
    skip=yes
    break;
   fi 

   pkgname="`cat $TMP/pkgname`" 
   
   if [ -z "$pkgname" ]; then
     continue
   else
     pkgname="$pkgname"
     break;
   fi
done
fi

     if [ ! "$skip" = "yes" ]; then
       $DIALOG --title "Installing package..." --infobox "$package" 3 50
       sleep 1
       install_package "$directory/$package" $pkgname $version 
       remove=yes
     fi
fi
done
     elif [ "$REPLY" = "List_2" ]; then

cat << EOF > $TMP/tmpmsg

Do you want to list a installed package in
$LOG_DIR/packages, or a uninstalled
package in $LOG_DIR/removed_packages?
 
EOF

  $DIALOG --title "LIST PACKAGES" \
  --menu "`cat $TMP/tmpmsg`" 13 60 2 \
"Installed" "$LOG_DIR/packages" \
"Uninstalled" "$LOG_DIR/removed_packages" 2> $TMP/reply
  if [ ! $? = 0 ]; then
    continue;
  fi

     REPLY="`cat $TMP/reply`"

  if [ "$REPLY" = "Installed" ]; then
    dir="$LOG_DIR/packages"
    script="$LOG_DIR/scripts"
  else
    dir="$LOG_DIR/removed_packages"
    script="$LOG_DIR/removed_scripts"
  fi  
  
   $DIALOG --title "Creating List..." --infobox "" 2 30
   sleep 1

    rm -f $TMP/packages    
   ( cd $dir 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 \n$LOG_DIR/packages \n\
does not contain any package listings." 8 60
    continue;
   fi

   echo '$DIALOG --menu "Please select the package you wish to view." 22 50 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`"

    cat $dir/$package > $TMP/$package
    if [ -s "$script/$package" ]; then
    echo >> $TMP/$package
    echo "This is the symbolic link script for the package." >> $TMP/$package
    echo >> $TMP/$package
    cat $script/$package >> $TMP/$package
    fi
    
     $DIALOG --title "CONTENTS OF PACKAGE: `cat $TMP/return`" --textbox \
     "$TMP/$package" 22 78 2> /dev/null
     rm -f $TMP/$package
     continue;
     else
     break;
    fi
   done
 elif [ "$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

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

   if [ ! -s "$TMP/packages" ]; then
    $DIALOG --title "DIRECTORY EMPTY" --msgbox "The directory \n$LOG_DIR/packages \n\
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`"

   version="`grep "PACKAGE VERSION: " $LOG_DIR/packages/$package | cut -d' ' -f3`"
  
   if [ ! -f "$LOG_DIR/backups/$package/$package-$version.tgz" ]; then
     $DIALOG --title "NOT FOUND" --msgbox \
     "The backup package ($LOG_DIR/backups/ \n$package/$package-$version.tgz) \ndoes not seem to exist." 8 60
     continue;
   fi
   $DIALOG --title "Removing package..." --infobox "$package" 4 50 
     sleep 1
     remove_package $package 2>/dev/null 
   $DIALOG --title "Restoring package..." --infobox "$package-$version.tgz" 4 50 
     sleep 1
     tar -Uxzplf $LOG_DIR/backups/$package/$package-$version.tgz -C / 2>/dev/null
    if [ -f "$LOG_DIR/removed_scripts/$package" ]; then
        grep " ln -s " $LOG_DIR/removed_scripts/$package | while read links ; do
        dir=`echo $links | awk '{print $3}'`
        file=`echo $links | awk '{print $16}'`
        link=`echo $links | awk '{print $17}'`
    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-$version.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, RPM, or generic tgz 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/GENERIC package" \
  --menu "`cat $TMP/tmpmsg`" 13 74 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
     rm -rf $TMP
     exit
   fi 

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

   if [ -z "$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" 5 78
    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 Slackware tgz, RPM, DEB,
or generic tgz package based on those contents?

EOF

  $DIALOG --title "TGZ/RPM/DEB/GENERIC" \
  --menu "`cat $TMP/tmpmsg`" 16 60 5 \
"Debian" "Create Debian package" \
"Slackware" "Create Slackware package" \
"Rpm" "Create RPM package" \
"Generic" "Create generic tgz package (Exact copy!)" \
"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" -o "$REPLY" = "Generic" ]; then

     if [ "$REPLY" = "Slackware" ]; then
     package="slackware-$$"
     else
     package="generic-$$"
     fi    

    pkg_name
    package="$package_name"
    if [ "$REPLY" = "Slackware" ]; then
    $DIALOG --title "Creating Slackware package..." --infobox "$package-$version.tgz" 3 50
    else
    $DIALOG --title "Creating Generic package..." --infobox "$package-$version.mku.tgz" 3 50
    fi
    sleep 1
        
    dir=$PWD
    cd $directory

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

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

`cat $TMP/error`

EOF
$DIALOG --title "ERROR" --msgbox "`cat $TMP/tmpmsg`" 18 78
continue;
}
    
    if [ "$REPLY" = "Slackware" ]; then
    mkpkg $package-$version.tar n slackware 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
    else
    mkpkg $package-$version.tar n generic 2>$TMP/error
    mv $package-$version.tar.gz $dir/$package-$version.mku.tgz 2>$TMP/error || error .mku
    $DIALOG --title "DONE" --msgbox "$package-$version.mku.tgz" 5 50
    fi

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

        if [ -s $TMP/deps ]; then
        sort -u $TMP/deps | grep -v -E "dynamic|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`
        else
          Depends="Depends:"
        fi
    
    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 $EDITOR 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ -n "$editor" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      $DIALOG --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      clear 2>/dev/null
      $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 $EDITOR 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ -n "$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
       rm -f $package.spec
       mv $rpm_dir/$package-$version-$release.$arch.rpm $dir || err_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 70 
     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 Slackware tgz, RPM, DEB,
or generic tgz package based on those contents?

EOF

  $DIALOG --title "TGZ/RPM/DEB/GENERIC" \
  --menu "`cat $TMP/tmpmsg`" 17 60 5 \
"Debian" "Create Debian package" \
"Slackware" "Create Slackware package" \
"Rpm" "Create RPM package" \
"Generic" "Create generic tgz package (Exact copy!)" \
"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" -o "$REPLY" = "Generic" ]; then

    pkg_name
    if [ "$REPLY" = "Slackware" ]; then
    $DIALOG --title "Creating Slackware package..." --infobox "$package_name-$version.tgz" 3 50
    else
    $DIALOG --title "Creating Generic package..." --infobox "$package_name-$version.mku.tgz" 3 50
    fi
    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
    if [ "$REPLY" = "Slackware" ]; then
     $DIALOG --title "Creating Slackware package..." --infobox "$package_name-$version.tgz" 3 50
    else
     $DIALOG --title "Creating Generic package..." --infobox "$package_name-$version.mku.tgz" 3 50
    fi
     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
     if [ "$REPLY" = "Slackware" ]; then
       extract_links_to < $LOG_DIR/scripts/$package > $TMP/links 
       make_slackware_install_script $TMP/links >> install/doinst.sh
     else
       cp $LOG_DIR/scripts/$package install/doinst.sh
     fi
       chmod 644 install/doinst.sh
       chown $root install/doinst.sh
     fi

     package="$package_name"

     if [ "$REPLY" = "Slackware" ]; then
     make_slackware_compatible
     fi
     pause "$TMP/build_tgz" 
     strip_binaries

    if [ "$REPLY" = "Slackware" ]; then
    $DIALOG --title "Creating Slackware package..." --infobox "$package-$version.tgz" 3 50
    else
    $DIALOG --title "Creating Generic package..." --infobox "$package-$version.mku.tgz" 3 50
    fi
    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$1.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 {} \;
    if [ "$REPLY" = "Slackware" ]; then
    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
    else
    tar -cpf $package-$version.tar * 2>$TMP/error || error .mku
    gzip -9 $package-$version.tar 2>$TMP/error || error .mku
    mv $package-$version.tar.gz $dir/$package-$version.mku.tgz
    fi    
    cd $dir && rm -rf $TMP/build_tgz

    if [ "$REPLY" = "Slackware" ]; then
    $DIALOG --title "DONE" --msgbox "$package-$version.tgz" 5 50
    else
    $DIALOG --title "DONE" --msgbox "$package-$version.mku.tgz" 5 50
    fi    
    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

        if [ -s $TMP/deps ]; then
        sort -u $TMP/deps | grep -v -E "dynamic|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`
        else
          Depends="Depends:"
        fi
    
    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 $EDITOR 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ -n "$editor" ]; then
    if [ ! -x "`type -path $editor`" ]; then
      $DIALOG --title "NOT FOUND" --msgbox "$editor" 5 30
      continue;
    else
      clear 2>/dev/null
      $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 {} \;

     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 {} \;
     
     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 $EDITOR 2> $TMP/editor
   
    editor="`cat $TMP/editor`"
    
    if [ -n "$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

       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
       rm -f $TMP/$package_name.s $TMP/$package_name  
     else
     break;
  fi  
done
break;
   else
   break;
   fi
   done
fi
fi

  if [ "$REPLY" = "List_1" ]; 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 -rf $TMP
    reset 2>/dev/null
    exit
   fi 

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

   if [ -z "$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
     rpm2cpio $PACKAGE_DIR/`cat $TMP/return` | cpio -tv --quiet >>$TMP/tmpmsg || exit
   elif echo `cat $TMP/return` | grep -q "\.deb$" ; then
     ar -p $PACKAGE_DIR/`cat $TMP/return` control.tar.gz | tar -xzO "*control" >>$TMP/tmpmsg 
     echo "" >> $TMP/tmpmsg
     ar -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
'mku' 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 -rf $TMP
    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/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 \n$LOG_DIR/configure \n\
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 [ -n "$package" ]; then
      rm -f $TMP/tmpmsg $TMP/reply $TMP/packages* $TMP/return

while :
do   
  $DIALOG --title "CONFIGURE RULES OPTIONS" --menu \
  "$package" 12 70 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 $EDITOR 2> $TMP/editor
   if [ ! $? = 0 ]; then
     break;
   fi 
        editor="`cat $TMP/editor`"

   if [ -z "$editor" ]; then
     break;
   fi
   
   if [ ! -x "`type -path $editor`" ]; then
     $DIALOG --title "NOT FOUND" --msgbox "$editor" 5 30 
     continue;
   else
        clear
        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 $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`" 16 76 7 \
"ftp" "ftp program" \
"lftp" "ftp program" \
"ncftp" "ftp program" \
"links" "https, http, 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" = "links" ]; then
      get_url $url "links: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 [ -z "$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 $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 \$doc_dir
cp FAQ README* \$doc_dir
chmod 644 \$doc_dir/*
chown $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 [ -z "$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 $EDITOR 2> $TMP/editor
   if [ ! $? = 0 ]; then
     rm -f $TMP/name $TMP/tmpmsg $TMP/editor
     break;  
   fi 

    editor="`cat $TMP/editor`"
  
  if [ -z "$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 $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 'mku', and choose monitor from the menu.
# ./configure --prefix=/usr ; make ; mku

You'll have the option of creating a Slackware, Debian, RPM, or
generic X.mku.tgz package of the install, after the install or later
using $LOG_DIR/packages/package_name as the guide.

NOTE: Before you do 'mku' 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 'mku' 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
    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 [ ! $? = 0 ]; then
    rm -f $TMP/pkgdir $TMP/SeT*
    break;
   fi 

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

   if [ -z "$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." \
8 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
   
    while [ 0 ]; do
   if [ -s $TMP/current_done ]; then 

if [ "$generic" = "generic" ]; 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
else
  $DIALOG --title "LIST PACKAGE" --menu "`cat $TMP/current_done`" 9 50 2 \
"List" "List contents of package" \
"Exit" "Exit" 2> $TMP/reply
fi   
    reply=`cat $TMP/reply`

    if [ "$reply" = "Install" ]; then
     $DIALOG --title "Installing..." --infobox "`cat $TMP/current_done`" 4 60 
     sleep 2
     pkg="$PACKAGE_DIR/`cat $TMP/current_done`"
     short_pkg="`basename $pkg .mku.tgz | sed "s/[-._][0-9].*//g"`"
     if echo $pkg | grep -q "[0-9]mdk.i[3-6]86" ; then
     version="`basename $pkg .mku.tgz | 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
     version="`basename $pkg .mku.tgz | 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
     if [ "$version" = "" ]; then
     version=0.0.0
     fi
     install_package $pkg $short_pkg $version >/dev/null 2>$TMP/error || error_install
    elif [ "$reply" = "List" ]; then
     $DIALOG --title "Listing package..." --infobox "`cat $TMP/current_done`" 3 50
     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 
     gzip -dc `cat $TMP/current_done` | tar -xO "install/doinst.sh" >>$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
cat << EOF > $TMP/tmpmsg

Do you want to create a Slackware
tgz or a Generic tgz package?

EOF

  $DIALOG --title "TGZ/GENERIC" \
  --menu "`cat $TMP/tmpmsg`" 12 48 3 \
"Slackware" "Create Slackware tgz package" \
"Generic" "Create generic tgz package" \
"None" "Don't Create a package" 2> $TMP/reply
    if [ ! $? = 0 ]; then
      break     
    fi

    reply=`cat $TMP/reply`

    if [ "$reply" = "Generic" ]; then
      generic=generic
    elif [ "$reply" = "Slackware" ]; then
      generic=slackware
    else
      break
    fi

   convert_package $PACKAGE_DIR/`cat $TMP/return` $generic
   else
   break
   fi
   done
   done
   continue
fi
done
