#!/bin/sh
# Copyright 1994, 1998  Patrick Volkerding, Moorhead, Minnesota USA 
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Wed Mar 18 15:32:33 CST 1998
# Patched to avoid possible symlink attacks in /tmp.
#
# Sat Dec 09 2000 - Felipe Sanchez <izto@asic-linux.com.mx>
# Removed the code for the questions about symlinks and sane permissions
# They are useless to CheckInstall
#
# Fri Jul 20 2001 - Felipe Sanchez <izto@asic-linux.com.mx>
# Included the patch made to makepkg in the Slackware 8.0 release.
#
# Sat Aug 14 2004 = Eko M. Budi
# patch to simplify symlinks creation using readlink (needs coreutil 5.0 up)
# add symlinks to the preexisting doinst.sh (assume it is created by checkinstall) 
#
# Sat Sept 18 2004 = Eko M. Budi
# Add dependency tracking
# Add txt creation

usage() {
 cat << EOF

Usage: makepkg package_name.tgz

Makes a Slackware compatible "*.tgz" package containing the contents of the 
current and all subdirectories. If symbolic links exist, they will be removed
and an installation script will be made to recreate them later. This script
will be called "install/doinst.sh". You may add any of your own ash-compatible
shell scripts to this file and rebuild the package if you wish.

EOF
}

find_dependency()
{
    for DDNAME in $*; do
	for DD in /var/log/packages/$DDNAME-*; do
	    DDBASE=`basename $DD`
	    if [ "$DDNAME" = "`pkgname $DDBASE`" ]; then
		if ! grep -qx "$DDBASE" install/dependency; then
	    	    echo $DDBASE >> install/dependency
	    	    echo "$DDBASE"
        	fi
	    fi
	done
    done
}

TMP=/tmp # This can be a hole, but I'm going to be careful about file
         # creation in there, so don't panic. :^)

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

if [ ! $# = 1 -a ! $# = 0 ]; then
 usage;
 exit
fi
echo
echo "Slackware package maker, version 1.1-CheckInstall"
if [ $# = 0 ]; then
 usage;
 echo "You have not provided a name for this package."
 echo -n "What would you like to call it? "
 read PACKAGE_NAME;
else
 PACKAGE_NAME=$1
fi
echo
TARGET_NAME="`dirname $PACKAGE_NAME`"
PACKAGE_NAME="`basename $PACKAGE_NAME`"
TAR_NAME="`basename $PACKAGE_NAME .tgz`"
PLONG=$PACKAGE_NAME
PSHORT=$TAR_NAME
PNAME=`pkgname $PLONG`

mkdir -p install

echo "Searching for symbolic links:"
# Using readlink to read the link
# Old method using ls -l was broken since coreutil 5.0
find . -type l -print | while read LINE; do
      DIRNAME="`dirname $LINE`" 
      LINKNAME="`basename $LINE`"
      LINKTO="`readlink $LINE`"
      echo "( cd $DIRNAME ; rm -rf $LINKNAME; ln -sf $LINKTO $LINKNAME )" >> install/doinst.sh
      rm -f $LINE
done

echo "Creating dependency ..."
if [ ! -f "install/dependency" ]; then
    echo "# dependency list" > install/dependency
fi

# find the executables and track the required packages
find . -perm +1 -print | while read LINE; do
     [ ! -f $LINE ] && continue
     echo "$LINE:"
     if ! ldd $LINE &>/dev/null; then
        # not a dynamic, possibly script
	FTYPE=`file $LINE`
	if echo $FTYPE | grep -q "Bourne shell" ; then
	    find_dependency bash
	elif echo $FTYPE | grep -q "C shell" ; then
	    find_dependency tcsh
	elif echo $FTYPE | grep -qw "perl" ; then
	    find_dependency perl
	elif echo $FTYPE | grep -qw "python" ; then
	    find_dependency python
	elif echo $FTYPE | grep -qw "ruby" ; then
	    find_dependency ruby
	elif echo $FTYPE | grep -qw "expect"; then
	    find_dependency expect tclx tcl
	elif echo $FTYPE | grep -qw "tcl"; then
	    find_dependency tcl
	fi
     else 
	ldd $LINE | while read LINE1; do
    	    LIB=`echo $LINE1 | cut -f1 -d ' '`
	    grep $LIB /var/log/packages/* | while read LINE2; do
		DNAME=`echo $LINE2 | cut -f1 -d:`
		DBASE=`basename $DNAME`
		[ "`pkgname $DBASE`" = "aaa_elflibs" ] && continue
		if ! grep -qx "$DBASE" install/dependency; then
	    	    echo $DBASE >> install/dependency
	    	    echo "$DBASE"
        	fi
	    done
	    if [ $? != 0 ]; then
		echoc "ERROR : Missing dependency" red
		echoc "ERROR : Cannot find the package of $LIB" red
		exit 1
	    fi
	done
     fi
done
     
echo
echo "Creating tar file $TAR_NAME.tar..."
echo
$TAR -cvf $TAR_NAME.tar .
# Warn of zero-length files:
find . -type f -size 0c | while read file ; do
  echo "WARNING: zero length file $file"
done
find . -type f -name '*.gz' -size 20c | while read file ; do
  echo "WARNING: possible empty gzipped file $file"
done
echo
echo "Gzipping $TAR_NAME.tar..."
gzip -9 $TAR_NAME.tar
echo
echo "Renaming $TAR_NAME.tar.gz to $PACKAGE_NAME..."
mv $TAR_NAME.tar.gz $PACKAGE_NAME
if [ ! "$TARGET_NAME" = "." ]; then
  echo
  echo "Moving $PACKAGE_NAME to $TARGET_NAME..."
  mv $PACKAGE_NAME $TARGET_NAME
fi

echo "Package $PACKAGE_NAME has been created"

# Create Text description with dependency
TXTNAME="$TARGET_NAME/${TAR_NAME}.txt"

## Add description
rm -f $TXTNAME
if [ -f "install/slack-desc" ]; then
    grep -ve "^#" install/slack-desc | while read line; do 
       echo "$line" >> $TXTNAME
    done
else
    echo "WARNING: slack-desc is not exist"
    echo "$PNAME: $PNAME" >> $TXTNAME
fi

## Add dependency
if [ -f install/dependency ]; then
    echo "-------------------------------------" >> $TXTNAME
    grep -ve "^#" install/dependency | while read line; do 
       echo "dependency: $line" >> $TXTNAME
    done
else
    echo "WARNING: dependency is not exist"
fi
     
echo "Description $TXTNAME has been created"

echo
echo "Package creation complete."
echo
