#!/bin/bash -e
# model.SlackBuild
# Copyright (C) 2006 Olivier Mehani <shtrom@ssji.net>
# 
# $Id: model.SlackBuild 18 2006-03-19 14:16:54Z shtrom $
# Default SlackBuild model.
# 
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# Set initial variables:
PACKAGE=
VERSION=
BUILD=1sh
ARCHIVE=$PACKAGE-$VERSION.tar.bz2
ARCHIVEURL="http://$ARCHIVE"
TARSUBDIR=$PACKAGE-$VERSION
PATCHES=""
DOCS="AUTHORS BUGS CHANGES COMPAT COPYING INSTALL* MANIFEST NEWS NOTES README* Y2K"
CONFS=""
ARCH=i486
CPU=`uname -m`
CONFIGURE_FLAGS="--prefix=/usr $ARCH-slackware-linux"
CFLAGS="-O2 -march=$ARCH -mcpu=$CPU"
MFLAGS="-j2"

# Do the job...
CWD=`pwd`
PKGNAME=$CWD/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
if [ ! -f $ARCHIVE ]; then
	echo "Downloading $ARCHIVEURL..."
	wget "$ARCHIVEURL"
fi

echo -n "Checking directories:"
if [ "$SRC" = "" ]; then
	SRC=/usr/src
fi
if [ ! -d $SRC ]; then
	mkdir -p $SRC # location to build the source
fi
echo -n " $SRC"

cd $SRC
rm -rf $TARSUBDIR
echo -n " $SRC/$TARSUBDIR"

if [ "$TMP" = "" ]; then
	TMP=/tmp
fi
echo -n " $TMP"

PKG=$TMP/$PACKAGE-$VERSION-$ARCH-$BUILD
rm -rf $PKG
mkdir -p $PKG
echo -n " $PKG"

echo "."

echo "Extracting $ARCHIVE..."
cd $SRC
case ${ARCHIVE##*.} in
	"gz"|"tgz"|"tar.gz")
	tar xzf $CWD/$ARCHIVE
	;;
	"bz2"|"tbz"|"tar.bz2")
	tar xjf $CWD/$ARCHIVE
	;;
	*)
	echo "Don't know what to do with $ARCHIVE."
	exit 1
	;;
esac

cd $TARSUBDIR

#chown -R root.root .
#find . -perm 664 | xargs chmod 644
#find . -perm 775 | xargs chmod 755

if [ ! -z $PATCHES ]; then
	echo -n "Applying patches:"
	for patch in $PATCHES ; do
		echo -n " $patch"
		if [ "${patch##*.}" = "gz" ]; then
			zcat $CWD/patches/$patch | patch -p1 -s
		else
			patch -p1 -s < $CWD/patches/$patch 
		fi
	done
	echo "."
fi

echo "Running CFLAGS=$CFLAGS ./configure $CONFIGURE_FLAGS..."
CFLAGS="$CFLAGS" \
./configure $CONFIGURE_FLAGS
echo "Building with $MFLAGS..."
make $MFLAGS
echo "Installing..."
make $MFLAGS install DESTDIR=$PKG

if [ ! -z "$DOCS" ]; then
	echo "Copying documentation..."
	for file in $DOCS; do
		mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
		cp -ra $file $PKG/usr/doc/$PACKAGE-$VERSION
	done
fi

echo "Copying distfiles..."
mkdir -p $PKG/install
cp $CWD/$0 $PKG/install
if [ -e $CWD/doinst.sh.gz ]; then
	zcat $CWD/doinst.sh.gz >> $PKG/install/doinst.sh
fi
cat $CWD/slack-desc > $PKG/install/slack-desc
[ -f $CWD/slack-required ] && cat $CWD/slack-required > $PKG/install/slack-required
[ -f $CWD/slack-conflicts ] && cat $CWD/slack-conflicts > $PKG/install/slack-conflicts
[ -f $CWD/slack-suggests ] && cat $CWD/slack-suggests > $PKG/install/slack-suggests

if [ ! -z "$CONFS" ]; then
	echo "Preparing default configuration files..."
	for file in $CONFS; do
		mv $PKG/$file $PKG/$file.new
		cat >> $PKG/install/doinst.sh <<- EOF
		( test ! -e $file && mv $file.new $file || echo "Not replacing $file" )
		EOF
	done
fi

if find $PKG -name *.so  2>/dev/null | grep -q "\.so$"; then
	echo "Updating installation script to update shared library links..." 
	cat >> $PKG/install/doinst.sh <<- 'EOF' 
	( echo "Updating shared library links..." ; /sbin/ldconfig )
	EOF
fi

if [ -d $PKG/usr/share/man ]; then
	echo "Moving manpages from /usr/share to /usr..."
	mkdir -p $PKG/usr/man
	mv $PKG/usr/share/man/* $PKG/usr/man
	rmdir $PKG/usr/share/man
fi
if [ -d $PKG/usr/man ]; then
	echo "Gzipping manpages..."
	find $PKG/usr/man -type f -exec gzip -9 \{\} \;
	echo "Updating installation script to update the whatis database..."
	cat >> $PKG/install/doinst.sh <<- 'EOF' 
	( echo "Updating whatis database..." ; cd usr/man ; makewhatis -u `pwd` 2>&1)
	EOF
fi

if [ -e $PKG/usr/info ]; then
	echo "Cleaning infodir..."
	rm -f $PKG/usr/info/dir
	find  ./usr/info -type f -name "*info" -exec gzip -9 \{\} \;
	find  ./usr/info -type f -name "*info-?" -exec gzip -9 \{\} \;
	echo "Updating installation script to recreate infodir..."
	cat >> ./install/doinst.sh <<- 'EOF' 
	( echo "Updating infodir..." ; cd usr/info ; rm dir ; for file in * ; do install-info $file dir 2>/dev/null ; done )
	EOF
fi


cd $PKG

# Allows for early tests as non root user
set +e
echo "Changing permissions..."
echo "	./"
chown -R root:root .
if [ -d ./usr/bin ]; then
	echo "	./usr/bin"
	chgrp -R bin ./usr/bin
fi
if [ -d ./usr/sbin ]; then
	echo "	./usr/sbin"
	chgrp -R bin ./usr/sbin
fi
set -e

echo "Stripping binaries..."
find . -name *.so -exec strip \{\} \;
find . -name *.so.* -exec strip \{\} \;
find . -type f -perm +111  -exec strip \{\} \;

echo "Packaging..."
/sbin/makepkg -l y -p -c n $PKGNAME

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
	echo "Cleaning up"
	rm -rf $SRC/$TARSUBDIR
	rm -rf $PKG
fi

echo "done."
