#!/bin/sh
##############################################################################
#         $Id: checkinstall,v 1.6.0.21 2003/03/13 19:36:45 izto Exp $ 
#                           ########################                         
#                                                                            
#
#                             CheckInstall v1.6.0beta2
#
#  Installs a compiled program from the program's source directory using     
#  "make install" or any other command supplied on checkinstall's command  
#  line. checkinstall will create a Slackware, RPM or Debian compatible package
#  named after the source directory's name and install it using your standard  
#  package administration utilities.                                      
#                                                                            
#  This version of checkinstall needs enough free space on the partition     
#  holding the temp dir (see BASE_TEMP_DIR below) to write there a temporary 
#  copy of the package.              
#                                                                            
#  Copyright (C) 2002 Felipe Eduardo Sanchez Diaz Duran <izto@asic-linux.com.mx>
#  
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
############################################################################


# Trap the INT signal (ctrl-c, for example)
trap trapint 2

CHECKINSTALL_VERSION=1.6.0beta2




# gettext variables
export TEXTDOMAINDIR=/usr/local/lib/checkinstall/locale
export TEXTDOMAIN=checkinstall

# Vefify that we have gettext available

gettext "Bye." &> /dev/null

if [ $? -gt 0 ]; then
   alias gettext=echo
   alias echog=gettext
else
   alias echog="gettext -s --"
fi


#############################################################################
                           # Function definitions #
                           ########################


function ckversion {
   echo
   echo "checkinstall $CHECKINSTALL_VERSION, Copyright 2002 Felipe Eduardo Sanchez Diaz Duran"
   echog "           This software is released under the GNU GPL."
}



function usage() {

# If the user has a default pager defined in the PAGER environment variable,
# we'll use that. If not, we'll use "more".

! [ "$PAGER" ] && PAGER=more


   (
   ckversion
   echo
   echog "Usage: checkinstall [options] [command [command arguments]]"
   echog "Options:"
   echo
   echog "*Package type selection*"
   echo
   echog "-t,--type=<slackware|rpm|debian> Choose packaging system"
   echog "-S                               Build a Slackware package"
   echog "-R                               Build a RPM package"
   echog "-D                               Build a Debian package"
   echo
   echog "*Install options*"
   echo
   echog "--install=<yes|no>             Toggle created package installation"
   echo
   echog "*Scripting options*"
   echo
   echog "-y, --default                  Accept default answers to all questions"
   echog "--pkgname=<name>               Set name"
   echog "--pkgversion=<version>         Set version"
   echog "-A, --arch, --pkgarch=<arch>   Set architecture"
   echog "--pkgrelease=<release>         Set release"
   echog "--pkglicense=<license>         Set license"
   echog "--pkggroup=<group>             Set software group"
   echog "--pkgsource=<source>           Set source location"
   echog "--pkgaltsource=<altsource>     Set alternate source location"
   echog "--pakdir=<directory>           The new package will be saved here"
   echog "--maintainer=<email addr>      The package maintainer (.deb)"
   echog "--provides=<list>              Features provided by this package (.rpm)"
   echog "--requires=<list>              Features required by this package (.rpm)"
   echog "--rpmflags=<flags>             Pass this flags to the rpm installer"
   echog "--rpmi                         Use the -i flag for rpm when installing a .rpm"
   echog "--rpmu                         Use the -U flag for rpm when installing a .rpm"
   echog "--dpkgflags=<flags>            Pass this flags to the dpkg installer"
   echog "--spec=<path>                  .spec file location"
   echog "--nodoc                        Do not include documentacion files"
   echo
   echog "*Info display options*"
   echo
   echog "-d<0|1|2>                      Set debug level"
   echog "-si                            Run an interactive install command"
   echog "--showinstall=<yes|no>         Toggle interactive install command"
   echog "-ss                            Run an interactive Slackware installation script"	
   echog "--showslack=<yes|no>           Toggle interactive Slackware installation script"
   echo
   echog "*Package tuning options*"
   echo
   echog "--autodoinst=<yes|no>          Toggle the creation of a doinst.sh script"
   echog "--strip=<yes|no>               Strip any ELF binaries found inside the package"
   echog "--stripso=<yes|no>             Strip any ELF binary libraries (.so files)" 
   echog "--gzman=<yes|no>               Compress any man pages found inside the package"
   echog "--docdir=<path>                Where to put documentation files"
   echog "--umask=<mask>                 Set the umask value"
   echog "--exclude=<file|dir[,...]>     Exclude these files/directories from the package"
   echog "--include=<listfile>           Force the inclusion in the package of the"
   echog "--inspect                      Inspect the package's file list"
   echog "--review-spec                  Review the spec file before creating a .rpm"
   echog "--review-control               Review the control file before creating a .deb"
   echog "                               files/dirs listed in \"listfile\""
   echog "--newslack                     Use the new (8.1+) Slackware description format"
   echog "                               (\"--newslack\" implies \"-S\")"
   echog "--with-tar=/path/to/tar        Manually set the path to the tar binary" 
   echog "                               in this system"
   echo
   echog "*Cleanup options*"
   echo
   echog "--deldoc=<yes|no>              Delete doc-pak upon termination"
   echog "--deldesc=<yes|no>             Delete description-pak upon termination"
   echog "--delspec=<yes|no>             Delete spec file upon termination"
   echog "--bk                           Backup any overwritten files"
   echog "--backup=<yes|no>              Toggle backup"
   echo
   echog "*About CheckInstall*"
   echo
   echog "--help, -h                     Show this message"
   echog "--copyright                    Show Copyright information"
   echog "--version                      Show version information"
   echo
   ) | $PAGER
   exit 1
}

function help_notice() {
 
echo
echog "Use --help or -h to get more information"
echo
exit 1 

}

function boolean_usage() {
   echo
   echo "$2 `gettext  'is an invalid value for'` $1"
   help_notice
   exit 1
}


# 001117-BaP: Define a function or two

function yorn {


   if [ "$1" ]; then     # If there is no default value specified
       DEFAULTYN="$1"     # then the default is "y"
    else
       DEFAULTYN="y"
    fi
    DEFAULTYN=`echo $DEFAULTYN | tr 'A-Z' 'a-z'`

    if [ "$DEFAULTYN" = "y" ]; then  
       echo -n " [y]: "              # Print the default option
    else
       echo -n " [n]: "
    fi
    
if [ $ACCEPT_DEFAULT -eq 0 ]; then     # Should we accept all the defaults?

    read YN
    YN=`echo $YN | tr 'A-Z' 'a-z'`
    ! [ "$YN" ] && YN=$DEFAULTYN          # If the user pressed ENTER then 

else
   YN=$DEFAULTYN
   echo $YN
fi

    if [ "$YN" = "y" ] ;then    # We return something useful for a 
       return 0                 # simpler sintax ahead (12/dic/2000-Izto)
    else
       return 1
    fi


}



# dec/10/2000-Izto
# Prints OK or FAILED! depending on previous command return value

function okfail () {
 if [ $? -gt 0 ]; then
    echo ' FAILED!'
    return 1
 else
    echo 'OK'
    return 0
 fi
}

function restore_backup {
 # If we have translation turned on then we didn't do a backup
 if [ "${TRANSLATE}" = "1" ]; then return 0; fi

 # Else, restore the backup if it exists
 
 rm -rf ${TMP_DIR}/BACKUP/no-backup &> /dev/null

 ls ${TMP_DIR}/BACKUP/*  &> /dev/null
 if [ $? -eq 0 ]; then
    echo -n `gettext "Restoring overwritten files from backup..."`
    cd ${TMP_DIR}/BACKUP
       $TAR -cpf - . | $TAR -f - -xvpC / &> /dev/null
    okfail
    echo
 fi
} 


function trapint {
 echo
 echo
 echog "*** SIGINT received ***"
 cleanup
}

function cleanup {
 echo
 restore_backup
 echo -n `gettext "Cleaning up..."`
 cd "$DIRECTORIO_FUENTE"
 rm -rf ${TMP_DIR}
 rm -f "$BUILDROOT"
 rm -f "${RPMSOURCEDIR}/SOURCES/${PKG_BASENAME}.tgz"
 rm -f checkinstall-debug*
 true; okfail
 echo
 echog "Bye."
 echo
 exit 1
}

# Function to copy a directory with permissions, honours setuid, setgid,
# sticky bits, owner, group etc.
#
# A bit lengthy and slow but very few directories in the largest of
# packages have to be copied like this.
 
function  dircopy() {
   src_dir="$1" dest_dir="$2"
   getflags() {
     col=$1 dir="$2"
     permissions="`ls -ld "$dir" | awk '{print $1}'`"
     mask="."
     bit=2
     while [ $bit -lt $col ]; do mask="$mask."; bit=`expr $bit + 1`; done
     mask="$mask\(.\)"
     bit=$col
     while [ $bit -lt 10 ]; do mask="$mask."; bit=`expr $bit + 1`; done
     permission="`echo $permissions | sed "s/$mask/\1/" | grep -v -- '-'`"
     case "$permission" in
       s) echo "xs";; S) echo "s";; t) echo "xt";; T) echo "t";;
       *) echo $permission;;
     esac
   }
   col=2 chmod_args=""
   set=u flags=""
   while [ $col -le 4 ]; do
     flags="$flags`getflags $col "$src_dir"`"; col=`expr $col + 1`
   done
   [ -n "$flags" ] && {
     chmod_args="$set+$flags"
   }
   set=g flags=""
   while [ $col -le 7 ]; do
     flags="$flags`getflags $col "$src_dir"`"; col=`expr $col + 1`
   done
   [ -n "$flags" ] && {
     [ -n "$chmod_args" ] && chmod_args="$chmod_args,"
     chmod_args="$chmod_args$set+$flags"
   }
   set=o flags=""
   while [ $col -le 10 ]; do
     flags="$flags`getflags $col "$src_dir"`"; col=`expr $col + 1`
   done
   [ -n "$flags" ] && {
     [ -n "$chmod_args" ] && chmod_args="$chmod_args,"
     chmod_args="$chmod_args$set+$flags"
   }
   [ -d "$dest_dir" ] || {
     mkdir -p "$dest_dir"
     chmod 700 "$dest_dir"
   }
   [ -n "$chmod_args" ] && chmod $chmod_args "$dest_dir"
   chown "`ls -ld "$src_dir" | awk '{print $3}'`" "$dest_dir"
   chgrp "`ls -ld "$src_dir" | awk '{print $4}'`" "$dest_dir"
 }
 


                      #################################
                      # Function definition ends here #
#############################################################################

# Show the version information
ckversion
echo

if ! [ -f /usr/local/lib/checkinstall/checkinstallrc ]; then
   echog "The checkinstallrc file was not found at:"
   echog "/usr/local/lib/checkinstall/checkinstallrc"
   echo
   echog "Assuming default values."
else
# Get our default settings from the rc file
source /usr/local/lib/checkinstall/checkinstallrc
fi


# Arguments parsing

CKNAME=`basename $0`
PARAMS=`getopt -a -n $CKNAME -o +d:DA:t:RShHy -l arch:,type:,si,showinstall::,ss,showslack::,deldoc::,delspec::,deldesc::,strip::,install::,stripso::,gzman::,bk,backup::,autodoinst::,spec:,exclude:,include:,pkgname:,pkgversion:,pkgrelease:,pkglicense:,pkggroup:,pkgsource:,pkgaltsource:,pakdir:,docdir:,requires:,provides:,maintainer:,dpkgflags:,rpmflags:,pkgarch:,umask:,with-tar:,inspect,review-spec,review-control,newslack,help,nodoc,rpmi,rpmu,version,copyright,default -- "$@"`

[ $? -gt 0 ] && help_notice

eval set -- $PARAMS

while [ "$1" != "--" ]; do
   case "$1" in
      -h|-H|--help)
         usage;;
      -d)
         shift
         case `eval echo $1` in
            0) DEBUG=0;;
            1|'') DEBUG=1;;
            2) DEBUG=2;;
            3) DEBUG=3;;
            4) DEBUG=4;;
            *)
               boolean_usage "-D" $1
         esac
         ;;
      -A|--arch|--pkgarch)
         shift
         ARCHITECTURE=`eval echo $1`
         ;;
      --umask)
         shift
         CKUMASK=`eval echo $1`
         ;;
      --pkgname)
         shift
         NAME=`eval echo $1`
         ;;
      --pkgversion)
         shift
         VERSION=`eval echo $1`
         ;;
      --pkgrelease)
         shift
         RELEASE=`eval echo $1`
         ;;
      --pkglicense)
         shift
         LICENSE=`eval echo $1`
         ;;
      --pkggroup)
         shift
	 # note: we use PKG_GROUP instead of GROUP since (t)csh sets GROUP.
         PKG_GROUP=`eval echo $1`
         ;;
      --pkgsource)
         shift
         SOURCE=`eval echo $1`
         ;;
      --pkgaltsource)
         shift
         ALTSOURCE=`eval echo $1`
         ;;
      --pakdir)
         shift
         PAK_DIR=`eval echo $1`
         ;;
      --with-tar)
         shift
         TAR=`eval echo $1`
         ;;
      --docdir)
         shift
         DOC_DIR=`eval echo $1`
         ;;
      --provides)
         shift
         PROVIDES=`eval echo $1`
         ;;
      --requires)
         shift
         REQUIRES=`eval echo $1`
         ;;
      --maintainer)
         shift
         MAINTAINER=`eval echo $1`
         ;;
      --dpkgflags)
         shift
         DPKG_FLAGS=`eval echo $1`
         ;;
      --rpmflags)
         shift
         RPM_FLAGS=`eval echo $1`
         ;;
      -t|--type)
         shift
         INSTYPE=`echo $1 | tr 'a-z' 'A-Z'`
         case `eval echo $INSTYPE` in
            RPM|R)
               INSTYPE=R;;
            SLACKWARE|S)
               INSTYPE=S;;
            DEBIAN|D)
               INSTYPE=D;;
            *)
               echo
               echo $1 is not a valid packaging system. Please use \'rpm\', \'slackware\' or \'debian\'
               echo 
               echo
               exit 1
          esac
          ;;
       -R)
          INSTYPE=R;;
       -S)
          INSTYPE=S;;
       -D)
          INSTYPE=D;;
       --install)
          shift
          case `eval echo $1` in
             1|yes|'')
                INSTALL=1;;
             0|no)
                INSTALL=0;;
             *)
                boolean_usage "--install" $1
          esac
          ;;
       --si)
          SHOW_INSTALL=1;;
       --showinstall)
          shift
          case `eval echo $1` in
             1|yes|'')
                SHOW_INSTALL=1;;
             0|no)
                SHOW_INSTALL=0;;
             *)
                boolean_usage "--showinstall" $1
          esac
          ;;
       --ss)
          SHOW_SLACK_INSTALL=1;;
       --showslack)
          shift
          case `eval echo $1` in
             1|yes|'')
                SHOW_SLACK_INSTALL=1;;
             0|no)
                SHOW_SLACK_INSTALL=0;;
             *)
                boolean_usage "--showslack" $1
          esac
          ;;
       --deldoc)
          shift
          case `eval echo $1` in
             1|yes|'')
                DEL_DOCPAK=1;;
             0|no)
                DEL_DOCPAK=0;;
             *)
                boolean_usage "--deldoc" $1
          esac
          ;;
       --delspec)
          shift
          case `eval echo $1` in
             1|yes|'')
                DEL_SPEC=1;;
             0|no)
                DEL_SPEC=0;;
             *)
                boolean_usage "--delspec" $1
          esac
          ;;
       --deldesc)
          shift
          case `eval echo $1` in
             1|yes|'')
                DEL_DESC=1;;
             0|no)
                DEL_DESC=0;;
             *)
                boolean_usage "--deldesc" $1
          esac
          ;;
       --strip)
          shift
          case `eval echo $1` in
             1|yes|'')
                STRIP_ELF=1;;
             0|no)
                STRIP_ELF=0;;
             *)
                boolean_usage "--strip" $1
          esac
          ;;
       --stripso)
          shift
          case `eval echo $1` in
             1|yes|'')
                STRIP_SO_ELF=1
                ;;
             0|no)
                STRIP_SO_ELF=0;;
             *)
                boolean_usage "--stripso" $1
             esac
          ;;
       --gzman)
          shift
          case `eval echo $1` in
             1|yes|'')
                COMPRESS_MAN=1;;
             0|no)
                COMPRESS_MAN=0;;
             *)
                boolean_usage "--gzman" $1
          esac
          ;;
       --bk)
          BACKUP=1;;
       --backup)
          shift
          case `eval echo $1` in
             1|yes|'')
                BACKUP=1;;
             0|no)
                BACKUP=0;;
             *)
                boolean_usage "--backup" $1
          esac
          ;;
       --default)
          ACCEPT_DEFAULT=1;;
       -y)
          ACCEPT_DEFAULT=1;;
       --nodoc)
          NODOC=1;;
       --rpmi)
          RPM_IU=i;;
       --rpmu)
          RPM_IU=U;;
       --newslack)
          NEW_SLACK=1;;
       --inspect)
          CK_INSPECT=1;;
       --review-spec)
          REVIEW_SPEC=1;;
       --review-control)
          REVIEW_CONTROL=1;;
       --spec)
	  shift
	  SPEC_PATH=`eval echo $1`
          ;;
       --copyright|--version)
          cat << EOF

Copyright (C) 2002 Felipe Eduardo Sanchez Diaz Duran <izto@asic-linux.com.mx>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

EOF
         exit 0;;
       --autodoinst)
          shift
          case `eval echo $1` in
             1|yes|'')
                AUTODOINST=1;;
             0|no)
                AUTODOINST=0;;
             *)
                boolean_usage "--autodoinst" $1
          esac
          ;;
      --exclude)
         shift
         EXCLUDE=`eval echo $1`
         ;;
      --include)
         shift
         CK_INCLUDE_FILE=`eval echo $1`
         ;;
   esac
   shift
done

# See if we have and install command
shift

[ "$1" = "" ] && set -- "make install"

# End of argument parsing

#############################################################################
                   # We initialize some useful variables #
                   #######################################

################################################################
# User-configurable variables were moved to the checkinstallrc #
# file which is probably found at /usr/local/lib/checkinstall  #          
#                                                              #
#                  DO NOT modify them here!!                   #
################################################################


# Debug level
! [ "$DEBUG" ] && DEBUG=0

INSTALLWATCH_PREFIX="/usr/local"
INSTALLWATCH=${INSTALLWATCH_PREFIX}/bin/installwatch

# Which makepkg to use
! [ "$MAKEPKG" ] && MAKEPKG=/usr/local/sbin/makepak

# Default architecture type
! [ "$ARCHITECTURE" ] && ARCHITECTURE=""

# Default package type
! [ "$INSTYPE" ] && INSTYPE=""

# Interactively show the results of the install command
# This is useful for interactive installation commands
! [ "$SHOW_INSTALL" ] && SHOW_INSTALL=1

# Show Slackware package installation script while it runs? Again, useful if
# it's an interactive script
! [ "$SHOW_SLACK_INSTALL" ] && SHOW_SLACK_INSTALL=0

# Automatic deletion of "doc-pak" upon termination
! [ "$DEL_DOCPAK" ] && DEL_DOCPAK=1

# Automatic deletion of the spec file
! [ "$DEL_SPEC" ] && DEL_SPEC=1

# Automatic deletion of "description-pak" 
! [ "$DEL_DESC" ] && DEL_DESC=1


# Automatically strip all ELF binaries
! [ "$STRIP_ELF" ] && STRIP_ELF=1

# Don't automatically strip all ELF binaries
! [ "$STRIP_SO_ELF" ] && STRIP_SO_ELF=0

# Automatically compress all man pages
! [ "$COMPRESS_MAN" ] && COMPRESS_MAN=1

# Backup all files modified by the install command supplied by the user
! [ "$BACKUP" ] && BACKUP=1 

# Write description installing code to doinst.sh
! [ "$AUTODOINST" ] && AUTODOINST=1 

# We won't include anything under this directories
! [ "$EXCLUDE" ] && EXCLUDE=""

# We will include anything listed in this file
! [ "$CK_INCLUDE_FILE" ] && CK_INCLUDE_FILE=""

# Accept the default answer for all the questions
! [ "$ACCEPT_DEFAULT" ] && ACCEPT_DEFAULT=0

# Use the default doc directory of /usr/doc
! [ "$DOC_DIR" ] && DOC_DIR=/usr/doc

# Do not include common documentation
! [ "$NODOC" ] && NODOC=0

# Use "-U" flag in rpm by default when installing a rpm package
# This tells rpm to (U)pdate the package instead of (i)nstalling it.
! [ "$RPM_IU" ] && RPM_IU=U

# Use the new (8.1+) Slackware description format
! [ "$NEW_SLACK" ] && NEW_SLACK=0

# Inspect the file list before creating the package
! [ "$CK_INSPECT" ] && CK_INSPECT=0

# Review the .spec file before creating a .rpm
! [ "$REVIEW_SPEC" ] && REVIEW_SPEC=0

# Review the control file before creating a .deb
! [ "$REVIEW_CONTROL" ] && REVIEW_CONTROL=0

# Set the umask
! [ "$CKUMASK" ] && CKUMASK=0022

# No real installation if not explicitly asked
! [ "$INSTALL" ] && INSTALL=0

####################
# Non-configurable #
####################

# Existing configuration files are always preserved
[ -f description-pak ] && DEL_DESC=0
[ -d doc-pak ] && DEL_DOCPAK=0


DIRECTORIO_FUENTE=`pwd`
PKG_BASENAME="`basename $DIRECTORIO_FUENTE`"



INSTALLCMD="$@"
: ${INSTALLCMD:='make install'}

# The editor we use to review the file list and .spec or control file
if ! [ -x "$VISUAL" ]; then
   if [ -x "$EDITOR" ]; then
      VISUAL=$EDITOR
   else
      VISUAL=`which vi`
   fi
fi

# pkgtools in Slackware 8.1+ don't like the default tar binary and instead
# use "tar-1.13". We'll use that too if we can find it.

BADTAR=0

if [ "$TAR" ]; then
   ! [ -x "$TAR" ] && BADTAR=1 
else
   TAR=`which tar-1.13 2> /dev/null`
fi

if ! [ -x "$TAR" ]; then
   TAR="tar"
fi

# If the user gave us a bad path to tar then we should warn him/her about it
if [ $BADTAR -gt 0 ]; then
   echo
   echog "Warning: The path to tar specified in the command line is invalid."
   echog "Warning: It has been set to $TAR"
   echo
fi

[ $DEBUG -gt 0 ] && echo "debug: TAR=$TAR"
[ $DEBUG -gt 0 ] && echo "debug: VISUAL=$VISUAL"

                      #################################
                      # Variable definition ends here #
#############################################################################



if [ ! -x $INSTALLWATCH ]; then
   echo
   echog "I can't find $INSTALLWATCH."
   echo
   echog "I can't continue. Either install installwatch or"
   echog "modify the INSTALLWATCH variable in this script,"
   echog "then run checkinstall again."
   echo
   exit 1
fi

echo

# If the user asked "--newslack" then we also set "-S"
[ $NEW_SLACK -gt 0 ] && INSTYPE="S"

# Set the umask. If not specified with "--umask" then we'll use 0022, a 
# standard, non-paranoid reasonable value.

if [ $DEBUG -gt 0 ] ;then 
   echo "debug: Setting umask => $CKUMASK"
fi
umask $CKUMASK


# Find a safe TMP_DIR

TMP_DIR=${BASE_TMP_DIR}/`awk 'BEGIN { srand(); for (i=1;i<21;i++) { a=95; while (a > 90 && a < 97) { a=65+int(50*rand())}; printf("%c", a) } }'`
[ -e "$TMP_DIR" ] && rm -rf $TMP_DIR
if [ -e "$TMP_DIR" ]; then 
   echo
   echog "My temp dir exists already."
   echog "This looks like a symlink attack!"
   echo 
   echog "*** Aborting"
   echo
   exit 1
fi

if [ "$TMP_DIR" = "$BASE_TMP_DIR" -o "$TMP_DIR" = "/" ]; then
  echo 
  echo "\"\$TMP_DIR\" `gettext 'is an unacceptable value for the temp dir. Please'`"
  echo "`gettext 'edit the variable definition for'` $TMP_DIR `gettext 'and try again.'`"
  echo
  echog "*** Aborting"
  echo
  exit 1
fi


mkdir $TMP_DIR
chmod 700 $TMP_DIR
RETURN=$?

if [ $RETURN -gt 0 ]; then
   echo
   echog "**** Failed to create temp dir!"
   echog "**** Do you have write permission for ${BASE_TMP_DIR}?"
   echo
   echog "**** Aborting installation."
   echo
   exit  $RETURN
fi

BUILD_DIR=${TMP_DIR}/package
mkdir $BUILD_DIR

if [ $DEBUG -gt 0 ] ;then 
   echo "debug: The temporary directory is: [ $TMP_DIR ]"
   echo
fi


# 001117-BaP: We can create a default set of docs on the fly . . .
#   The list I've included should cover most packages adequately. If not,
#   then you should really create the package doc set *manually*


# Check if --nodoc was specified
if [ "$NODOC" = "0" ]; then 

if  ! [ -d "$DIRECTORIO_FUENTE/doc-pak" ]; then
    echog "The package documentation directory ./doc-pak does not exist."
    echo -n `gettext "Should I create a default set of package docs? "`
    if  yorn ; then
        echo
        echo -n `gettext "Preparing package documentation..."`
        mkdir doc-pak
         for f in ABOUT ABOUT-NLS ANNOUNCE AUTHORS *BUGS* CHANGES CONFIGURATION *COPYING* *COPYRIGHT* CREDITS ChangeLog Changelog CHANGELOG CONTRIBUTORS *FAQ* FEATURES FILES HACKING History HISTORY INSTALL* LICENSE LSM MANIFEST NEWS *README* *Readme* SITES *RELEASE* RELNOTES THANKS TIPS TODO VERSION CONFIGURATION* GPL License ; do
            if [ -f $f ]; then                                                 
                cp -a $f doc-pak
            fi
        done
        okfail
        DOCS=`ls doc-pak`
        if ! [ "$DOCS" ]; then
           echo
           echog "*** No known documentation files were found. The new package"
           echog "*** won't include a documentation directory."
           rm -rf doc-pak                 # If doc-pak is empty then we
        fi                                # don't need it
    fi                                    
    
fi

fi # End of NODOC



#
# We run the program installation from the source directory       
#

# Write a small script to run the install command. This way the LD_PRELOAD
# environment variable will be available to it, wich among other things
# will allow proper detection of new symbolic links and files created by
# subshells.

[ $DEBUG -gt 0 ] && echo "debug: CK_INCLUDE_FILE = $CK_INCLUDE_FILE"

TMP_SCRIPT=${TMP_DIR}/installscript.sh

cat << EOF > $TMP_SCRIPT
#!/bin/sh

# Copy the documentation files           

if [ -d "$DIRECTORIO_FUENTE/doc-pak" ]; then             # Are there any files? 
   echo
   echo `gettext "Copying documentation directory..."`
   mkdir -p "${DOC_DIR}/${PKG_BASENAME}"
   cd $DIRECTORIO_FUENTE/doc-pak
   cp -a . ${DOC_DIR}/${PKG_BASENAME}

   [ \$? -gt 0 ] && exit 1
   chown -R root.root "${DOC_DIR}/${PKG_BASENAME}"
fi

# If we have a --include list file, copy the files listed there to /
# If translation is on, it will catch these too.

if [ -f "${DIRECTORIO_FUENTE}/${CK_INCLUDE_FILE}" ]; then
   cat "${DIRECTORIO_FUENTE}/${CK_INCLUDE_FILE}" | while read include_file; do
      $TAR -cpf - "\$include_file" | $TAR -f - -xp -C /
   done
fi

#
cd "$DIRECTORIO_FUENTE"
$INSTALLCMD

# Report success or failure
if [ \$? -eq 0 ]; then
   exit 0
else
   exit 1
fi

EOF

# No one needs to see what we are doing. It's safer this way.
chmod 700 $TMP_SCRIPT


echo
echo -n "`gettext 'Installing with'` \"$INSTALLCMD\"..."


# Are we going to do a "fake" install?
TRANSLATE=1

#
#   We exclude as many "dangerous" directories as possible from translation.
# installwatch excludes itself some directories, but we put them all here,
# to be explicit.
#
IEXCLUDE="${DIRECTORIO_FUENTE},/dev,/proc,/tmp,/var/tmp"
  

# Run the install command, showing the results interactively if we were asked
# to do so in the configuration section (see the SHOW_INSTALL switch above)
INSTALL_FAILED=0
if [ $SHOW_INSTALL -eq 0 ]; then

   $INSTALLWATCH --logfile=${TMP_DIR}/newfiles.tmp --exclude="${IEXCLUDE}" \
   --root=${TMP_DIR} --transl=${TRANSLATE} --backup=${BACKUP} --dbglvl=$DEBUG\
   $TMP_SCRIPT &> /${TMP_DIR}/install.log

   okfail
   INSTALL_FAILED=$?
else
   echo
   echo
   echog "========================= Installation results ==========================="
   $INSTALLWATCH --logfile=${TMP_DIR}/newfiles.tmp --exclude="${IEXCLUDE}" \
   --root=${TMP_DIR} --transl=${TRANSLATE} --backup=${BACKUP} --dbglvl=$DEBUG\
   $TMP_SCRIPT 2>&1
   if [ $? -eq 0 ]; then 
      echo
      echog "======================== Installation successful =========================="
   else        
      INSTALL_FAILED=1
   fi
fi

unset LD_PRELOAD


VIEWLOG="n"
if [ $INSTALL_FAILED -gt 0 ]; then 
   echo
   echog "****  Installation failed. Aborting package creation."
   VIEWLOG="y"
fi

if [ $SHOW_INSTALL -eq 0 ]; then
   echo
   echo -n `gettext "Do you want to view the installation log file? "`

   if  yorn $VIEWLOG ; then

      (echo
       echo ' ***************************************************************'
       echo '         Installation results. You can find them in'
       echo "       ${TMP_DIR}/install.log"
       echo ' ***************************************************************'
       echo
       cat /${TMP_DIR}/install.log)  | less
   fi
fi

if [ $INSTALL_FAILED -gt 0 ]; then 
   cleanup
   exit $INSTALL_FAILED
fi

# If translation was active, the installed files are under /transl
TRANSLROOT="/"
if [ "${TRANSLATE}" = "1" ]; then
        TRANSLROOT="${TMP_DIR}/TRANSL"
fi


#
# Extract the relevant files from the modified files list
#

# Find regular files first
[ $DEBUG -gt 0 ] && echo "debug: BASE_TMP_DIR: $BASE_TMP_DIR"
cat /${TMP_DIR}/newfiles.tmp | egrep -v '^[-0-9][0-9]*[       ]*unlink' | cut -f 3 | egrep -v "^(/dev|$BASE_TMP_DIR|/tmp)" | sort -u > /${TMP_DIR}/newfiles

# symlinks are next
cat /${TMP_DIR}/newfiles.tmp | egrep -v '^[-0-9][0-9]*[       ]*unlink' | cut -f 4 | egrep -v "^(/dev|$BASE_TMP_DIR|/tmp)" | grep -v "#success" | sort -u  >> /${TMP_DIR}/newfiles

# OK, now we clean it up a bit
mv /${TMP_DIR}/newfiles.tmp /${TMP_DIR}/newfiles.installwatch
sort -u <  /${TMP_DIR}/newfiles | uniq  | while read file; do
   if [ -e "${TRANSLROOT}${file}" ]; then
      echo $file >> /${TMP_DIR}/newfiles.tmp
   else
      FILE_SYM=`file "${TRANSLROOT}${file}" | grep 'symbolic link'`
      [ "${FILE_SYM}" != "" ] && echo $file >> /${TMP_DIR}/newfiles.tmp
   fi
done

cp /${TMP_DIR}/newfiles.tmp /${TMP_DIR}/newfiles

# Don't include anything under the directories specified with "--exclude"

[ $DEBUG -gt 0 ] && echo "debug: EXCLUDE=$EXCLUDE"

for exclude in `echo $EXCLUDE | awk '{ split ($0, files,","); for(i=1; files[i] != ""; i++) print files[i];}'`; do
   if [ -d $exclude ]; then  # If it's a directory, ignore everything below it
      egrep -v "^$exclude" < /${TMP_DIR}/newfiles > /${TMP_DIR}/newfiles.tmp
   else
      if [ -f $exclude ]; then  # If it's a file, ignore just this one
         egrep -v "^$exclude$" < /${TMP_DIR}/newfiles > /${TMP_DIR}/newfiles.tmp
      fi
   fi
   cp /${TMP_DIR}/newfiles.tmp /${TMP_DIR}/newfiles
done


## Find any files created in `pwd`. We probably don't want them here

grep "`pwd`" ${TMP_DIR}/newfiles > /${TMP_DIR}/unwanted 
if [ $? = 0 ]; then 
   echo
   echog "Some of the files created by the installation are inside the build"
   echog "directory: `pwd`"
   echo
   echog "You probably don't want them to be included in the package,"
   echog "especially if they are inside your home directory."
   echo -n `gettext "Do you want me to list them? "`
   if  yorn "n"; then
      less ${TMP_DIR}/unwanted
   fi
   echo -n `gettext "Should I exclude them from the package? (Saying yes is a good idea) "`
   if  yorn ; then
      grep -v `pwd` ${TMP_DIR}/newfiles > /${TMP_DIR}/newfiles.tmp
      mv /${TMP_DIR}/newfiles.tmp /${TMP_DIR}/newfiles
   fi
fi

# If requested with "--inspect" we run an editor on the newfiles list
if [ $CK_INSPECT -gt 0 ]; then
   echo
   echog "You requested to review the list of files that will be"
   echog "included in this package."
   echo
   echog "Now you'll have the chance to review and optionally modify it."
   echo
   echog "Press ENTER to continue."
   read junk
   $VISUAL ${TMP_DIR}/newfiles

   # Check if new files were added by the user
   cat /${TMP_DIR}/newfiles | while read file; do 
      if ! [ -e "${TRANSLROOT}${file}" ]; then
         ($TAR -C / -cpf - ".${file}"| $TAR -f - -xvpC ${BUILD_DIR}) >> /${TMP_DIR}/updated-by-user.log 2>&1
      fi
   done

fi


# Copy the files to the temporary directory
#

echo
echo -n `gettext "Copying files to the temporary directory..."`

cd /

( cat /${TMP_DIR}/newfiles | while read i; do 
   if ! [ -d "${TRANSLROOT}${i}" ]; then
      echo ".${i}"
   fi
done ) > /${TMP_DIR}/newfiles-tar

TAR_FILES=`cat /${TMP_DIR}/newfiles-tar`
($TAR -C "${TRANSLROOT}" -cpf - $TAR_FILES | $TAR -C ${BUILD_DIR} -xpf - ) >> /${TMP_DIR}/checkinstall.log 2>&1


okfail

cd /

##############################################################################
                            # Strip ELF binaries # 
                            ######################


#
# Note: The code in this section was taken from the brp-strip script included 
# in rpm-4.0.2, which is distributed under the GNU GPL. The copyright
# for this code belongs to its owner. See the file COPYING for details.
#

if [ $STRIP_ELF -eq 1 ]; then
   echo
   if [ $STRIP_SO_ELF -eq 1 ]; then
       echo -n `gettext "Striping ELF binaries and libraries..."`
   else
       echo -n `gettext "Striping ELF binaries..."`
   fi
   for f in `find ${BUILD_DIR} -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \)` ; do

       if [ $STRIP_SO_ELF -eq "0" -a "`file $f | grep -v ' shared object,'`" = "" ]; then
       #if this is a *.so* file and we don't have to strip it, then filter 'em
           continue
       fi
       if [ "`file $f | sed -n -e 's/^\(.*\):[  ]*ELF.*, not stripped/\1/p'`" = "" ]; then
           continue
       fi
       strip -p $f || :
   done
   okfail
fi
                             ###################
                             # End of striping #
##############################################################################



##############################################################################
                            # Compress man pages #
                            ######################

#
# Note: The code in this section is based on the brp-compress script
# included in rpm-4.0.2, which is distributed under the GNU GPL. The
# copyright for this code belongs to its owner. See the file COPYING for
# details.
#

if [ $COMPRESS_MAN -eq 1 ]; then
 echo
 echo -n `gettext "Compressing man pages..."`
 cd $BUILD_DIR
 
 # Compress man pages
 COMPRESS="gzip -9"
 COMPRESS_EXT=.gz
 
 for d in ./usr/local/man/man* ./usr/local/man/*/man* ./usr/local/info \
 	./usr/local/share/man/man* ./usr/local/share/man/*/man* \
 	./usr/local/share/info \
 	./usr/local/kerberos/man \
 	./usr/local/share/doc/*/man/man* ./usr/local/lib/*/man/man* \
 	././usr/man/man* ./usr/man/*/man* ./usr/info \
 	./usr/share/man/man* ./usr/share/man/*/man* ./usr/share/info \
 	./usr/kerberos/man ./usr/X11R6/man/man* ./usr/lib/perl5/man/man* \
 	./usr/share/doc/*/man/man* ./usr/lib/*/man/man*
 do
     [ -d $d ] || continue
     for f in `find $d -type f`
     do
        [ -f "$f" ] || continue
 	[ "`basename $f`" = "dir" ] && continue

         # decompress manpage by "cat"ing it, this allows decompression of
         # hardlinked manpages and allows gunzip of a compressed page which is
         # actually called "man.1" and not "man.1.gz", something it won't do
         # when operating directly on file.
         # this overcomes installs that create (un)compressed pages with names
         # that don't correspond with the compression.
         # this is done to a temporary file so we can detect hardlinks to
         # original file.
       b=`echo $f | sed -e 's/\.Z$//;s/\.gz$//;s/\.bz2$//'`
         cp -p $f $b.tmp # setup up permissions on our new file
         cat $f | gunzip -c 2>/dev/null >$b.tmp || {
           cat $f | bunzip2 -c 2>/dev/null >$b.tmp || {
             cat $f >$b.tmp
           }
         }
 
       $COMPRESS $b.tmp
         # find any hard links to the old manpage and link to the new one..
         inode=`ls -i $f | awk '{ print $1 }'`
         others=`find $d -type f -inum $inode`
       if [ -n "$others" ]; then
         for afile in $others ; do
           [ "$afile" = "$f" ] && continue
             rm -f ${TRANSLROOT}/${afile} ${afile}
             afile=`echo $afile | sed -e 's/\.Z$//;s/\.gz$//;s/\.bz2$//'`
           ln $b.tmp$COMPRESS_EXT $afile$COMPRESS_EXT
         done
         fi
         rm -f ${TRANSLROOT}/${f} ${f}
         mv $b.tmp$COMPRESS_EXT $b$COMPRESS_EXT
    done
 
     for f in `find $d -type l`
     do
 	l=`ls -l $f | awk '{ print $11 }' | sed -e 's/\.gz$//' -e 's/\.bz2$//' -e 's/\.Z$//'`
 	rm -f ${TRANSLROOT}/${f}
 	b=`echo $f | sed -e 's/\.gz$//' -e 's/\.bz2$//' -e 's/\.Z$//'`
 	ln -sf $l$COMPRESS_EXT $b$COMPRESS_EXT
     done
 done
 okfail
fi

                       ################################
                       # End of man pages compressing #
##############################################################################


# Now we get the TRUE list of files from the directory listing (kind of)
# of the BUILD_DIR. This is the true one simply because THESE are the files
# that will actually get packaged.


echo
echo -n  `gettext "Building file list..."`
rm ${TMP_DIR}/newfiles
cd $BUILD_DIR
$TAR -cpf -  . | $TAR -f - -t 2>&1 > ${TMP_DIR}/newfiles.tmp
okfail

# Remove the directories from the listing (RPM doesn't like directories listed
# inside the spec file)

cat ${TMP_DIR}/newfiles.tmp | while read line; do
   ! [ -d "${BUILD_DIR}/${line}" ] && echo /${line} >> ${TMP_DIR}/newfiles
done


#
# Find out the packaging method to use
#

if ! [ "$INSTYPE" ]; then
   echo
   echog "Please choose the packaging method you want to use."
   echo -n `gettext "Slackware [S], RPM [R] or Debian [D]? "`
   read INSTYPE
fi
echo

       case $INSTYPE in
         S|s) 
            INSTYPE=S
            CK_SLACKWARE=1
            ;;
         R|r)
            INSTYPE=R
            CK_REDHAT=1
            ;;
         D|d)
            INSTYPE=D
            CK_DEBIAN=1
            ;;
         *)
           echo Invalid type.
           exit 1
           ;;
       esac


## Do we have a package description file? If we don't then 
## we should write one

cd "$DIRECTORIO_FUENTE"


if [ $ACCEPT_DEFAULT -eq 0 ]; then  # If --default is given, we skip this 
 if ! [ -r description-pak ]; then
   DESCRIPTION="Package created with checkinstall $CHECKINSTALL_VERSION"
   echo
   echo -n `gettext "Please write a description for the package."`

   # If we selected Slackware, then give the pkgtool warning

   if [ "$CK_SLACKWARE" ]; then 
      echog " Remember that pkgtool shows"
      echo -n  `gettext "only the first one when listing packages so make that one descriptive."`
   fi
   echo
   echog "End your description with an empty line or EOF."
   while [ "$DESCRIPTION" ]; do
      echo -n ">> " 
   INSTYPE=`echo $INSTYPE | tr a-z A-Z`
      read DESCRIPTION
      [ "$DESCRIPTION" ] && echo "$DESCRIPTION" >> description-pak
   done
 fi
fi

# We still don't have it??
! [ -r description-pak ] && echo "Package created with checkinstall $CHECKINSTALL_VERSION" > description-pak


# Warn the user if we're using the new Slackware description format
# and the description is bigger than 11 lines

if [ $NEW_SLACK -gt 0 ]; then
   if [ `wc -l < description-pak` -gt 11 ]; then
      echo
      echog "Warning: Your package description is bigger than 11 lines."
      echog "Warning: The Slackware 8.1+ pkgtools might not like it."
      echo
   fi
fi



########## Acquire some info about the package ##########


# Figure out what kind of machine are we running on

if ! [ "$ARCHITECTURE" ]; then
   ARCHITECTURE=`uname -m`
   echo $ARCHITECTURE | grep -e "i[3456]86" &> /dev/null
   [ $? -eq 0 ] && ARCHITECTURE=i386  # Arch will be "i386" for any of
fi                                    # i386, i486, i586 or i686.
                                      # You can change this with "--arch"

   # Fix the PowerPC architecture description if we're on Debian
   
   if [ "$CK_DEBIAN" = "1" ] && [ "$ARCHITECTURE" = "ppc" ]; then
      ARCHITECTURE="powerpc"
   fi
      

   OPTION=junk
   while [ "$OPTION" ]; do

      # Some sanity checks
      ! [ "$SUMMARY" ] && SUMMARY=`head -1 description-pak`
      ! [ "$NAME" ] && NAME=`echo "$PKG_BASENAME" | rev | cut -f2- -d"-" | rev`

      if [ "$CK_DEBIAN" ]; then       # Convert name to lower case
      echo $NAME | grep -e "[A-Z]" &> /dev/null
         if [ $? -eq 0 ]; then
            echo
            echog "`gettext '*** Warning: The package name'` \"${NAME}\" `gettext 'contains upper case'`"
            echog "*** Warning: letters. dpkg might not like that so I changed"
            echog "*** Warning: them to lower case."
            NAME=`echo $NAME | tr 'A-Z' 'a-z'`
         fi
      fi


      ! [ "$VERSION" ] && VERSION=`echo "$PKG_BASENAME" | rev | cut -f1 -d"-" | rev`

      ## Did we get a usable version?

      if [  "$VERSION" == "$PKG_BASENAME" ]; then

         # If we did not then try getting it from the config.log file
         if [ -f "$DIRECTORIO_FUENTE/config.log" ]; then
            VERSION=`grep '#define VERSION' config.log| awk -F \" '{print $ 2}'`

         else

            # We use the current that if everything else fails
            VERSION=`date +%Y%m%d` 
         fi
      fi


      if [ "$CK_DEBIAN" ]; then       # Check for a valid version for dpkg
         echo $VERSION | grep -e "[0-9]" &> /dev/null
         if [ $? -gt 0 ]; then
            echo
            echog "*** Warning: The package version \"${VERSION}\" does not"
            echog "*** Warning: contain any digits. dpkg might not like that."
         fi
      fi
      ! [ "$RELEASE" ] && RELEASE="1"
      ! [ "$LICENSE" ] && LICENSE="GPL"
      ! [ "$PKG_GROUP" ] && [ "$CK_REDHAT" ] && PKG_GROUP="Applications/System"
      ! [ "$PKG_GROUP" ] && [ "$CK_DEBIAN" ] && PKG_GROUP="checkinstall"
      ! [ "$PKG_GROUP" ] && [ "$CK_SLACKWARE" ] && PKG_GROUP="Applications/System"

      ! [ "$ARCHITECTURE" ] && ARCHITECTURE="i386"
      ! [ "$SOURCE" ] && SOURCE="$PKG_BASENAME"
      ! [ "$ALTSOURCE" ] && ALTSOURCE=""
      ! [ "$PROVIDES" ] && PROVIDES="$NAME"
      ! [ "$REQUIRES" ] && REQUIRES=""
      # bond: added this so it is easy to change the Maintainer: field
      # just by setting the MAINTAINER environment variable
      ! [ "$MAINTAINER" ] && MAINTAINER=${LOGNAME:-root}@`hostname -f`
      
      
      echo
      echog "This package will be built according to these values: "
      echo
      # Debian maintainers use the Maintainer: field and want to be able
      # to change it. If we are not on debian we don't need the field...
      [ "$CK_DEBIAN" ] && echo "`gettext '0 -  Maintainer:'` [ $MAINTAINER ]"

      echo "`gettext '1 -  Summary:'` [ $SUMMARY ]"
      echo "`gettext '2 -  Name:'`    [ $NAME ]"
      echo "`gettext '3 -  Version:'` [ $VERSION ]"
      echo "`gettext '4 -  Release:'` [ $RELEASE ]"
      echo "`gettext '5 -  License:'` [ $LICENSE ]"
      echo "`gettext '6 -  Group:'`   [ $PKG_GROUP ]"
      echo "`gettext '7 -  Architecture:'` [ $ARCHITECTURE ]"
      echo "`gettext '8 -  Source location:'` [ $SOURCE ]"
      echo "`gettext '9 -  Alternate source location:'` [ $ALTSOURCE ]"
      [ "$CK_REDHAT" ] && echo "`gettext '10 - Provides:'` [ $PROVIDES ]"
      [ "$CK_REDHAT" ] && echo "`gettext '11 - Requires:'` [ $REQUIRES ]"

      echo
      echo -n `gettext "Enter a number to change any of them or press ENTER to continue: "`
      if [ $ACCEPT_DEFAULT -eq 1 ]; then
         echo
         OPTION=""
      else
         read OPTION
      fi

      case $OPTION in
         1)
            echog "Enter new summary: "
            echo -n ">> "
            read SUMMARY
            ;;
         2)
            echog "Enter new name: "
            echo -n ">> "
            read NAME
            ;;
         3)
            echog "Enter new version: "
            echo -n ">> "
            read VERSION
            ;;
         4)
            echog "Enter new release number: "
            echo -n ">> "
            read RELEASE
            ;;
         5)
            echog "Enter the license type: "
            echo -n ">> "
            read LICENSE
            ;;
         6)
            echog "Enter the new software group: "
            echo -n ">> "
            read PKG_GROUP
            ;;
         7)
            echog "Enter the architecture type: "
            echo -n ">> "
            read ARCHITECTURE
            ;;
         8)
            echog "Enter the source location: "
            echo -n ">> "
            read SOURCE
            ;;
         9)
            echog "Enter the alternate source location: "
            echo -n ">> "
            read ALTSOURCE
            ;;
         0) # bond: again, debian-specific
	    [ "CK_DEBIAN" ] && {
            echog "Enter the maintaner's name and e-mail address: "
            echo -n ">> "
            read MAINTAINER
	    }
            ;;
         10)
            # 01-12-06 UKo: new feature
            echog "Enter the provided features: "
            echo -n ">> "
            read PROVIDES
            ;;
         11)
            [ "CK_REDHAT" ] && {
            echog "Enter the additional requirements: "
            echo -n ">> "
            read REQUIRES
            }
            ;;
      esac
   done
   
   # The PKG_BASENAME is adjusted to reflect any changes
   # in the NAME and VERSION of the package
   # NOTE: on debian we use NAME alone, instead - see below

   PKG_BASENAME="$NAME-$VERSION" 

FAILED=0

case $INSTYPE in

##############################################################################
                    # Create Slackware compatible tarball #
                    #######################################


s|S)  


echo
echog "********************************************"
echog "**** Slackware package creation selected ***"
echog "********************************************"
echo

# The new Slackware naming scheme
SLACK_PKG_BASENAME="${PKG_BASENAME}-${ARCHITECTURE}-${RELEASE}"

FAILED=0

# Verify that we have the "installpkg" command in our path

INSTALLPKG_PATH=`which installpkg 2> /dev/null`

if ! [ -x "$INSTALLPKG_PATH" ]; then
   echo
   echog "*** The \"installpkg\" program is not in your PATH!"
   echo
   echog "*** Slackware package creation aborted"
   FAILED=1
fi
   
if ! [ $FAILED -gt 0 ]; then

# Create the Slackware installation script

echo -n `gettext "Preparing Slackware install directory..."`
mkdir -p ${BUILD_DIR}/install
touch ${BUILD_DIR}/install/doinst.sh
okfail

echo
echo -n `gettext "Writing package description..."`

# If we're building for Slackware 8.1+, then we use the new description
# file format

if [ $NEW_SLACK -gt 0 ]; then

cat << EOF > ${BUILD_DIR}/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|' on
# the right side marks the last column you can put a character in. You must make
# exactly 11 lines for the formatting to be correct.  It's also customary to
# leave one space after the ':'.

EOF

# Calculate the spaces before the "handy-ruler"
HR_SPACES=`echo $NAME | wc -c| awk '{ for (i=1;substr ($0,i,1) ~ /[[:blank:]]/ ;i++); print substr ($0,i);}'`


awk -v S=$HR_SPACES 'BEGIN {ORS=""; for (i=1; i < S; i++) print " ";}' >> ${BUILD_DIR}/install/slack-desc

echo "|-----handy-ruler------------------------------------------------------|" >> ${BUILD_DIR}/install/slack-desc


# Add the prefixed description
 cat description-pak | while read line; do
    echo "$NAME: $line" >> ${BUILD_DIR}/install/slack-desc
 done

okfail

# End of NEW_SLACK block

else 

#
# Generate the description-installing doinst.sh
#

if [ $AUTODOINST -eq 1 ]; then
   echo "PACKAGE DESCRIPTION:" > ${BUILD_DIR}/install/description
   cat description-pak | while read line; do
      echo $line >> ${BUILD_DIR}/install/description
   done
   okfail 

   echo
   echo -n `gettext "Writing Slackware install script..."`
   cat << EOF >> ${BUILD_DIR}/install/doinst.sh
# 
# doinst.sh, auto-generated by checkinstall-${CHECKINSTALL_VERSION}
#

echo 
cat /install/description

sed '/PACKAGE LOCATION/r /install/description' < "/var/log/packages/$SLACK_PKG_BASENAME" > description.tmp
mv description.tmp "/var/log/packages/${SLACK_PKG_BASENAME}"
rm /install/description

EOF
   okfail
fi

fi # End of description creation

# If we have a Slackware install script already, add it to this one
# and force an interactive slackware install script
for file in postinstall-pak install-pak; do
   if [ -f $file ]; then
      INSTALL_PAK=${file}
   fi
done
if [ -f "$INSTALL_PAK" ]; then 
   echo
   echo -n `gettext "Appending your script to the main install script..."`
   cat $INSTALL_PAK >> ${BUILD_DIR}/install/doinst.sh
   okfail
   SHOW_SLACK_INSTALL=1
fi


echo
echo -n `gettext "Creating package ${SLACK_PKG_BASENAME}..."`

cd $BUILD_DIR
chmod 755 $BUILD_DIR

# Make the "TAR" variable available to MAKEPKG
export TAR

$MAKEPKG "${SLACK_PKG_BASENAME}.tgz" &> /dev/null
okfail
mv "${SLACK_PKG_BASENAME}.tgz" "$DIRECTORIO_FUENTE"

#
# If requested, we install the package to register it in Slackware's
# installed packages list so we can list it's contents with pkgtool 
# or remove it with removepkg
#

if [ "$INSTALL" == "1" ]; then

   # Go back to where we started
   cd "$DIRECTORIO_FUENTE"
   
   echo
   
   if [ $SHOW_SLACK_INSTALL -eq 0 ]; then
      echo -n `gettext "Installing package..."`
      installpkg "${SLACK_PKG_BASENAME}.tgz" &> ${TMP_DIR}/slackinstall.log
      okfail
      [ $? -gt 0 ] && FAILED=1
   
   else
   echog "========================= Installation results ==========================="
      echo
      installpkg "${SLACK_PKG_BASENAME}.tgz" 
      if [ $? -gt 0 ]; then
         FAILED=1
      else
         echo
      echog "======================== Installation successful =========================="
      fi
   fi
   
   if [ $FAILED -eq 1 ]; then
      echo
      echog "*** Failed to install the package"
      echo
      if [ $SHOW_SLACK_INSTALL -eq 0 ]; then
         echo -n `gettext "Do you want to see the log file? "` 
         if yorn ; then
            less ${TMP_DIR}/slackinstall.log
            cleanup
            exit 1
         fi
      fi
   fi

else
        echo
        echog "NOTE: The package will not be installed."
fi
 

PKG_LOCATION="${DIRECTORIO_FUENTE}/${SLACK_PKG_BASENAME}.tgz"
INSTALLSTRING="installpkg ${SLACK_PKG_BASENAME}.tgz"
REMOVESTRING="removepkg ${SLACK_PKG_BASENAME}"

fi
;;


                       #################################
                       # End Slackware package section #
##############################################################################


##############################################################################
                          # RPM package installation #
                          ############################


r|R)

echo
echog "**************************************"
echog "**** RPM package creation selected ***"
echog "**************************************"

FAILED=0

# Verify that we have the rpm command in our path

RPM_PATH=`which rpm 2> /dev/null`

if ! [ -x "$RPM_PATH" ]; then
   echo
   echog "*** The \"rpm\" program is not in your PATH!"
   echo
   echog "*** RPM package creation aborted"
   FAILED=1
fi
   
if ! [ $FAILED -gt 0 ]; then

# Identify the rpm version

RPM_VERSION=`rpm --version | awk '{ print $3 }'`
RPM_MAJOR_VERSION=`echo $RPM_VERSION | cut -f1 -d"."`
RPM_MINOR_VERSION=`echo $RPM_VERSION | cut -f2 -d"."`
RPM_PATCH_LEVEL=`echo $RPM_VERSION | cut -f3 -d"."`

if [ "$RPM_PATCH_LEVEL" = "" ]; then 
   RPM_PATCH_LEVEL=0
fi


if [ $DEBUG -gt 0 ]; then
   echo "debug: RPM_VERSION=${RPM_VERSION}"
   echo "debug: RPM_MAJOR_VERSION=$RPM_MAJOR_VERSION"
   echo "debug: RPM_MINOR_VERSION=$RPM_MINOR_VERSION"
   echo "debug: RPM_PATCH_LEVEL=$RPM_PATCH_LEVEL"
   echo "debug: RPM_IU=$RPM_IU"
fi

# AFAIK, the only rpm versions that accept
# "--target=arch" instead of "--target arch"
# are 3.x < version < 4.0.3

if [ $RPM_MAJOR_VERSION -eq 4 -a $RPM_PATCH_LEVEL -lt 3 ]; then
   RPM_TARGET_FLAG="--target="
else
   RPM_TARGET_FLAG="--target "
fi

# rpmbuild seems to exist only in RPM 4

if [ $RPM_MAJOR_VERSION -eq 4 ]; then
   RPMBUILD="rpmbuild"
else
   RPMBUILD="rpm"
fi



# Find out the RPM source directory path

if ! [ "$RPMSOURCEDIR" ]; then
   if [ $RPM_MAJOR_VERSION -eq 3 -a $RPM_PATCH_LEVEL -ge 3 -o $RPM_MAJOR_VERSION -gt 3 ]; then
      RPMSOURCEDIR="`rpm --eval=%_topdir`"
   else
      RPMSOURCEDIR="NOT-FOUND"
      for directory in packages OpenLinux redhat RedHat rpm RPM "" ; do
         [ -d /usr/src/${directory}/SOURCES ] && RPMSOURCEDIR="/usr/src/${directory}"
      done
   fi
fi

[ $DEBUG -gt 0 ] && echo "debug: RPMSOURCEDIR=$RPMSOURCEDIR"


while ! [ -d "$RPMSOURCEDIR/SOURCES" ]; do
   echo
   echog "$RPMSOURCEDIR has no SOURCES directory. Please write the path to"
   echo -n `gettext "the RPM source directory tree: "`
   read RPMSOURCEDIR
   ! [ "$RPMSOURCEDIR" ] && RPMSOURCEDIR="NOT-FOUND"
done

   
# We'll write a basic spec file

if ! [ -f "$PKG_BASENAME.spec" ]; then

# If the buildroot path has spaces then we'll use the BUILD_DIR instead.
# rpm can't have a buildroot path with spaces in it (Bug?)
#
# This is obviously a hack, I hope we can do it the right way when rpm is
# fixed.

if ( echo $DIRECTORIO_FUENTE | grep " " &> /dev/null ); then
   BROOTPATH=$BUILD_DIR
else
   BROOTPATH="${DIRECTORIO_FUENTE}/buildroot"
fi

# Here comes the .spec file:

   cat > "$PKG_BASENAME.spec" << EOF
Summary:   $SUMMARY
Name:      $NAME
Version:   $VERSION
Release:   $RELEASE
Copyright: $LICENSE
Packager:  checkinstall-$CHECKINSTALL_VERSION
Group:     $PKG_GROUP          
BuildRoot: $BROOTPATH
Provides:  $PROVIDES
Requires:  $REQUIRES,/bin/sh

%description
EOF
   cat description-pak >> "$PKG_BASENAME.spec"


# Use the preinstall-pak script if we have it
if [ -f preinstall-pak ]; then
   echo  >> "$PKG_BASENAME.spec" 
   echo '%pre' >> "$PKG_BASENAME.spec" 
   cat preinstall-pak  >> "$PKG_BASENAME.spec"
fi

# Use the postinstall-pak script if we have it
if [ -f postinstall-pak ]; then
   echo >> "$PKG_BASENAME.spec" 
   echo '%post' >> "$PKG_BASENAME.spec" 
   cat postinstall-pak  >> "$PKG_BASENAME.spec"
fi

# Use the preremove-pak script if we have it
if [ -f preremove-pak ]; then
   echo >> "$PKG_BASENAME.spec" 
   echo '%preun' >> "$PKG_BASENAME.spec" 
   cat preremove-pak  >> "$PKG_BASENAME.spec"
fi

# Use the postremove-pak script if we have it
if [ -f postremove-pak ]; then
   echo >> "$PKG_BASENAME.spec" 
   echo '%postun' >> "$PKG_BASENAME.spec" 
   cat postremove-pak  >> "$PKG_BASENAME.spec"
fi

   cat >> "$PKG_BASENAME.spec" << EOF

%files
EOF

# Append the file list to the .spec file
cat ${TMP_DIR}/newfiles | while read line; do 
                             echo "\"${line}\""  >> "$PKG_BASENAME.spec"
                          done
fi


# If requested with --review-spec, we run an editor on the .spec file
if [ $REVIEW_SPEC -gt 0 ]; then
   echo
   echog "You requested to review the .spec file for this package."
   echo
   echog "Now you'll have the chance to review and optionally modify this file."
   echo
   echog "Press ENTER to continue."
   read junk
   $VISUAL "$PKG_BASENAME.spec" 
fi

BUILDROOT=`egrep '^[Bb]uild[Rr]oot' < "$PKG_BASENAME.spec" | cut -f2 -d:| sed 's/^ *//g'| sed 's/ *$//g'` # The sed commands remove leading/trailing whitespaces

# We make sure that we have a valid RELEASE number

! [ "$RELEASE" ] && RELEASE=`egrep '^Release:' < "$PKG_BASENAME.spec"|cut -f2 -d: | awk '{ for (i=1;substr ($0,i,1) ~ /[[:blank:]]/ ;i++); print substr ($0,i); }'`

[ $DEBUG -gt 0 ] && echo "debug: BUILDROOT=$BUILDROOT"

! [ -d "$BUILDROOT" ] &&  ln -s "$BUILD_DIR" "$BUILDROOT"

# We make sure that the architecture directory exists

! [ -d ${RPMSOURCEDIR}/RPMS/${ARCHITECTURE} ] && mkdir -p ${RPMSOURCEDIR}/RPMS/${ARCHITECTURE}


mkdir "${TMP_DIR}/$PKG_BASENAME"
cd $TMP_DIR
$TAR -cz "$PKG_BASENAME" -f "${RPMSOURCEDIR}/SOURCES/${PKG_BASENAME}.tgz"
rm -rf "${TMP_DIR}/$PKG_BASENAME"
cd "$DIRECTORIO_FUENTE"

echo
echo -n `gettext "Building RPM package..."`
$RPMBUILD -bb ${RPM_TARGET_FLAG}${ARCHITECTURE} "$PKG_BASENAME.spec" &> ${TMP_DIR}/rpmbuild.log
okfail

if [ $? -gt 0 ]; then
   echo
   echog "*** Failed to build the package"
   echo
   echo -n `gettext "Do you want to see the log file? "`
   if yorn ; then
      less ${TMP_DIR}/rpmbuild.log
   fi
   FAILED=1
fi
 
RPMPKG="${RPMSOURCEDIR}/RPMS/${ARCHITECTURE}/${PKG_BASENAME}-${RELEASE}.${ARCHITECTURE}.rpm"

# Check for the old RPMFLAGS
if [ "$RPMFLAGS" ]; then
   echo
   echog "Warning: the use of RPMFLAGS is deprecated."
   echog "Warning: You should now use RPM_FLAGS,"
   echog "Warning: please update your checkinstallrc file."
fi

if ! [ $FAILED -gt 0 ]; then 
   if [ "$INSTALL" == "1" ]; then

      echo
      echo -n `gettext "Installing RPM package..."`
      rpm -$RPM_IU $RPM_FLAGS ${RPMPKG} &>  ${TMP_DIR}/rpminstall.log
      okfail
      if [ $? -gt 0 ]; then
         echo
         echog "*** Failed to install the package"
         echo
         echo -n `gettext "Do you want to see the log file? "`
         if yorn ; then
            less ${TMP_DIR}/rpminstall.log
         fi
         FAILED=1
      fi
   else
      echo
      echog "NOTE: The package will not be installed"
   fi
fi
   
if ! [ $FAILED -gt 0 ]; then
   PKG_LOCATION=$RPMPKG
   INSTALLSTRING="rpm -i `basename $RPMPKG`"
   REMOVESTRING="rpm -e ${PKG_BASENAME}-${RELEASE}"
fi
   
   
fi  # End of the "no rpm in the path" if

;;

                       #################################
                       #    End RPM package section    #
##############################################################################



##############################################################################
                        # Debian  package installation #
                        ################################


d|D)

# as we said before:
PKG_BASENAME=$NAME
# maybe PKG_BASENAME should be defined locally for all the install
# types, and not only on debian...

echo
echog "*****************************************"
echog "**** Debian package creation selected ***"
echog "*****************************************"

FAILED=0


# Verify that we have the dpkg command in our path

DPKG_PATH=`which dpkg 2> /dev/null`

if ! [ -x "$DPKG_PATH" ]; then
   echo
   echog "*** The \"dpkg\" program is not in your PATH!"
   echo
   echog "*** Debian package creation aborted"
   FAILED=1
fi
   
if ! [ $FAILED -gt 0 ]; then

cd "$DIRECTORIO_FUENTE"

# We'll write a basic Debian control file


mkdir $BUILD_DIR/DEBIAN

cat << EOF >> $BUILD_DIR/DEBIAN/control
Package: $PKG_BASENAME
Priority: extra
Section: $PKG_GROUP
Installed-Size: `du -s $BUILD_DIR | awk '{print $1}'`
Maintainer: $MAINTAINER
Architecture: $ARCHITECTURE
Version: ${VERSION}-${RELEASE}
Description: $SUMMARY
EOF

# Add the description
cat "$DIRECTORIO_FUENTE/description-pak"| egrep -v "$SUMMARY|^[ 	]*$" | while read line; do
   echo " "$line >> $BUILD_DIR/DEBIAN/control
done


# Use the preinstall-pak script if we have it
if [ -f preinstall-pak ]; then
   cat preinstall-pak > $BUILD_DIR/DEBIAN/preinst
   chmod 755 $BUILD_DIR/DEBIAN/preinst
fi

# Use the postinstall-pak script if we have it
if [ -f postinstall-pak ]; then
   cat postinstall-pak > $BUILD_DIR/DEBIAN/postinst
   chmod 755 $BUILD_DIR/DEBIAN/postinst
fi

# Use the preremove-pak script if we have it
if [ -f preremove-pak ]; then
   cat preremove-pak > $BUILD_DIR/DEBIAN/prerm
   chmod 755 $BUILD_DIR/DEBIAN/prerm
fi

# Use the postremove-pak script if we have it
if [ -f postremove-pak ]; then
   cat postremove-pak > $BUILD_DIR/DEBIAN/postrm
   chmod 755 $BUILD_DIR/DEBIAN/postrm
fi


# The package will be saved here:
DEBPKG="${DIRECTORIO_FUENTE}/${NAME}_${VERSION}-${RELEASE}_${ARCHITECTURE}.deb"
# This one is for 2.2 "Potato" (or older) style packages
#DEBPKG="${DIRECTORIO_FUENTE}/${NAME}_${VERSION}-${RELEASE}.deb"

if [ $DEBUG -gt 0 ]; then
  echo
  echo debug: PKG_BASENAME=${PKG_BASENAME}
  echo debug: BUILD_DIR=${BUILD_DIR}
  echo debug: DEBPKG=${DEBPKG}
  echo debug: dpkg command:
  echo "   \"dpkg-deb -b $BUILD_DIR $DEBPKG &> ${TMP_DIR}/dpkgbuild.log\""
fi

# If requested with --review-control, we run an editor on the control file
if [ $REVIEW_CONTROL -gt 0 ]; then
   echo
   echog "You requested to review the control file for this package."
   echo
   echog "Now you'll have the chance to review and optionally modify this file."
   echo
   echog "Press ENTER to continue."
   read junk
   $VISUAL ${BUILD_DIR}/DEBIAN/control
fi

echo
echo -n `gettext "Building Debian package..."`
dpkg-deb -b $BUILD_DIR "$DEBPKG" &> ${TMP_DIR}/dpkgbuild.log
okfail
      
if [ $? -gt 0 ]; then
   echo
   echog "*** Failed to build the package"
   echo
   echo -n `gettext "Do you want to see the log file? "`
   if yorn ; then
      less ${TMP_DIR}/dpkgbuild.log
   fi
   FAILED=1
fi


if ! [ $FAILED -gt 0 ]; then
   if [ "$INSTALL" == "1" ]; then

      echo
      echo -n `gettext "Installing Debian package..."`
      dpkg -i $DPKG_FLAGS "$DEBPKG" &>  ${TMP_DIR}/dpkginstall.log
      okfail
      if [ $? -gt 0 ]; then
         echo
         echog "*** Failed to install the package"
         echo
         echo -n `gettext "Do you want to see the log file? "`
         if yorn ; then
            less ${TMP_DIR}/dpkginstall.log
         fi
         FAILED=1
      fi
   else
      echo
      echog "NOTE: The package will not be installed"
   fi
fi
 
if ! [ $FAILED -gt 0 ]; then
   PKG_LOCATION=$DEBPKG
   INSTALLSTRING="dpkg -i `basename $DEBPKG`"
   REMOVESTRING="dpkg -r ${PKG_BASENAME}"
fi


fi

;;


                       ################################
                       #  End Debian package section  #
##############################################################################


*)
   echo
   echog "*** No method was selected, I won't build any package."
   echog "*** The installation command \"$INSTALLCMD\""
   echog "*** has already been executed."
   restore_backup
   FAILED=1
;;

esac

# If we have a package repository set, move the package there

if ! [ $FAILED -gt 0 ]; then

   if [ "$PAK_DIR" ]; then
      if ! [ -d "$PAK_DIR" ]; then
         echo
         echo "`gettext 'The package storage directory'` [ $PAK_DIR ]"
         echo -n `gettext "doesn't exist. Do you want to create it?"`
         if yorn ; then
            echo
            echo -n `gettext "Creating package storage directory..."`
            mkdir -p "$PAK_DIR" &> ${TMP_DIR}/mkpakdir.log
            okfail 
            if [ $? -gt 0 ]; then
               echo 
               echog "*** Unable to create $PAK_DIR"
               echo "*** `cat ${TMP_DIR}/mkpakdir.log`"
               echo
            fi
         fi
      fi
      if [ -d "$PAK_DIR" ]; then 
         echo
         echo -n "`gettext 'Transferring package to'` $PAK_DIR..."
         mv $PKG_LOCATION ${PAK_DIR} &> $TMP_DIR/transfer.log
         okfail
         if [ $? -gt 0 ]; then
            echo
            echo "*** Transfer failed: `cat $TMP_DIR/transfer.log`"
            echo
         else
            # Update the package location
            PKG_LOCATION=${PAK_DIR}/`basename $PKG_LOCATION`
         fi
      else 
         echo
         echog "There's no package storage directory, the package"
         echog "will be stored at the default location."
      fi
   fi
fi

#
# Remove trash from TMP_DIR
#

echo 
echo -n `gettext "Erasing temporary files..."`

# Preserve the Debian control file if debug is on
if [ $DEBUG -gt 0 ]; then
   if [ -d ${BUILD_DIR}/DEBIAN ]; then
       mv ${BUILD_DIR}/DEBIAN $TMP_DIR
   fi
fi

[ $DEBUG -lt 2 ] && rm -rf ${BUILD_DIR}
rm -f checkinstall-debug*
rm -f "$BUILDROOT"
rm -f "${RPMSOURCEDIR}/SOURCES/${PKG_BASENAME}.tgz"
okfail


# Delete doc-pak directory
if [ $DEL_DOCPAK -gt 0 ]; then
   echo
   echo -n `gettext "Deleting doc-pak directory..."`
   rm -rf doc-pak
   okfail
fi


# Preserve the spec file if debugging is on
[ $DEBUG -gt 0 ] && [ -f "${PKG_BASENAME}.spec" ] && cp "${PKG_BASENAME}.spec" $TMP_DIR

# Delete spec file
 [ $DEL_SPEC -gt 0 ] && rm -f "$PKG_BASENAME.spec"

# Delete the package description file
 [ $DEL_DESC -gt 0 ] && rm -f description-pak

# If we have a backup, pack it up

rm -rf ${TMP_DIR}/BACKUP/no-backup &> /dev/null
ls ${TMP_DIR}/BACKUP/* &> /dev/null
if [ $? -eq 0 ]; then
   cd ${TMP_DIR}/BACKUP
   echo
   echo -n `gettext "Writing backup package..."`
   $TAR -cpf - . | gzip -9 > "${DIRECTORIO_FUENTE}/backup-`date +%m%d%Y%H%M`-pre-${PKG_BASENAME}.tgz"
   okfail
fi


if [ $DEBUG -eq 0 ]; then
   echo
   echo -n `gettext "Deleting temp dir..."`
   rm -rf ${TMP_DIR}
   okfail
   echo
else
   echo
   echo -n `gettext "Building debug information package..."`
   cd ${TMP_DIR}
   echo `uname -a` > sysinfo
   echo `rpm --version` >> sysinfo
   $TAR -cpzvf "${DIRECTORIO_FUENTE}/checkinstall-debug.$$.tgz" * &> /dev/null
   okfail
fi


if ! [ $FAILED -gt 0 ]; then

   if [ "${INSTALL}" = "1" ]; then

      echo
      echo '**********************************************************************'
      echo
      echog " Done. The new package has been installed and saved to"
      echo
      echo " $PKG_LOCATION"
      echo
      echog " You can remove it from your system anytime using: "
      echo
      echo "      $REMOVESTRING"
      echo
      echo '**********************************************************************'
      echo

   else

      echo
      echo '**********************************************************************'
      echo
      echog " Done. The new package has been saved to"
      echo
      echo " $PKG_LOCATION"
      echo
      echog " You can install it in your system anytime using: "
      echo
      echo "      $INSTALLSTRING"
      echo
      echo '**********************************************************************'
      echo

   fi
      
fi




