#!/bin/bash
#
# source: http://stabellini.net/requiredbuilder-0.16.5.tar.gz
#
# maintainer: gmg (g.monpontet@gmail.com)
#

soft='requiredbuilder'
softversion='0.16.5'
packageversion='66.1'

arch='i486'
cpu='i686'

buildir=$(pwd)
srcpkg="$buildir/$soft-$softversion.tar.gz"
src="$buildir/$soft-$softversion"
package="$soft-$softversion-$arch-$packageversion"
PKG="$buildir/$package"

cd $buildir
rm -rf $src
tar -xvzf $srcpkg
rm -f $PKG.tgz
rm -rf $PKG
mkdir -p $PKG/etc
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/doc/$soft-$softversion

cd $src
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

cd $src
./install.sh $PKG

find $PKG | xargs file | grep -e "shared object" -e "executable" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

gzip -9 $PKG/usr/man/man1/*

# Documentation
mkdir -p $PKG/usr/doc/$soft-$softversion
cp -a AUTHORS ChangeLog COPYING INSTALL README $PKG/usr/doc/$soft-$softversion

# Description file

mkdir -p $PKG/install
cat << EODESC >> $PKG/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 ':'.

               |-----handy-ruler-------------------------------------------------------|
requiredbuilder: requiredbuilder - is a finder dependencies tool in the package
requiredbuilder:
requiredbuilder: RequiredBuilder is a simple tool to help Slackware packages developers
requiredbuilder: finding dependencies. For example it can be used to write Zenwalk
requiredbuilder: style depfiles or slack-required files for slapt-get's dependency
requiredbuilder: checking.
requiredbuilder:
requiredbuilder:
requiredbuilder:
requiredbuilder:
requiredbuilder:
EODESC

cat << "DOINST" >> $PKG/install/doinst.sh
#!/bin/bash
config() {
        NEW="$1"
        OLD="`dirname $NEW`/`basename $NEW .new`"
        # If there's no config file by that name, mv it over:
        if [ ! -r $OLD ]; then
                mv $NEW $OLD
        elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then
                # toss the redundant copy
                rm $NEW
        fi
        # Otherwise, we leave the .new copy for the admin to consider...
}

config etc/requiredbuilder.conf.new
DOINST

mkdir -p $PKG/usr/src/$soft-$softversion
cp $buildir/build-$soft.sh $PKG/usr/src/$soft-$softversion

# Packaging
cd $PKG
makepkg -l y -c n $PKG.txz

cd $buildir
md5sum $package.txz > $package.md5