#!/bin/sh
# dmraid SlackBuild
#
# written by bloodlust ( bloodlust24 AT gmail DOT com ) on 2006/10/17

# set initial variables
CWD=`pwd`
if ["$TMP" = ""]; then
    TMP=/tmp
fi
NAME=dmraid
VERSION=1.0.0.rc13
ARCH=${ARCH:-i486}
BUILD=1bl
URL=http://people.redhat.com/~heinzm/sw/dmraid/src

PKG=$TMP/package-$NAME

if [ ! -d $TMP ]; then
    mkdir -p $TMP
fi

# set compiler flags
if [ "$ARCH" = "i386" ]; then
    SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
    SLKCFLAGS="-O2 -march=i486 -mtune=i686"
fi

echo "** Starting SlackBuild for $NAME-$VERSION"
cd $TMP

# do proper cleanup (if executed with --cleanup)
if [ "$1" = "--cleanup" ]; then
    echo "Cleaning up ..."
    rm -rf $TMP/$NAME/$VERSION
    rmdir $TMP/$NAME
    rm -rf $PKG
    exit
fi

# cleaning for previous builds
rm -rf $PKG
mkdir -p $PKG
rm -rf $TMP/$NAME

# download sources if not found locally
# (this is not found in standard SlackBuilds but
#  I think it can be helpful)
if [ ! -f $CWD/$NAME-$VERSION.tar.bz2 ]; then
   cd $CWD
   wget $URL/$NAME-$VERSION.tar.bz2 || exit
   cd $TMP
fi

# extract application sources
tar jxf $CWD/$NAME-$VERSION.tar.bz2

cd $NAME/$VERSION

# change permission on sources
chown -R root.root .
find . -type d -exec chmod 755 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 771 -exec chmod 755 {} \;
find . -perm 661 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;

# configure and compile sources
CFLAGS="$SLKCFLAGS" \
  ./configure \
#  --enable-static_link \
#  --enable-shared_libs \
  $ARCH-slackware-linux

# compile and exits if errors
make || exit

# install into package dir
make install DESTDIR=$PKG || exit

# strip binaries and libraries
( cd $PKG
    find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
    find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# create documentation directory
mkdir -p $PKG/usr/doc/$NAME-$VERSION

cp -a \
CHANGELOG CREDITS KNOWN_BUGS LICENSE LICENSE_GPL LICENSE_LGPL README TODO doc/dmraid_design.txt \
$PKG/usr/doc/$NAME-$VERSION

# gzip manpages
if [ -d $PKG/usr/man ]; then
    gzip -9 $PKG/usr/man/man?/*
fi

# gzip info pages
if [ -d $PKG/usr/info ]; then
    gzip -9 $PKG/usr/info/*.info
    rm -f $PKG/usr/info/dir
fi

# change permissions
chown -R root.root $PKG/sbin
chown -R root.root $PKG/lib
chown -R root.root $PKG/usr/include
chown -R root.root $PKG/usr/man
chown -R root.root $PKG/usr/doc
chmod -R 644 $PKG/usr/doc/$NAME-$VERSION/*

# create lock dir (don't know if this really is required but...)
mkdir -p $PKG/var/lock/$NAME

# create and populate ./install
mkdir -p $PKG/install

# for a good Slackware package this should exists
if [ -e $CWD/slack-desc ]; then
    cat $CWD/slack-desc > $PKG/install/slack-desc
fi

# add doinst.sh if exists
if [ -e $CWD/doinst.sh.gz ]; then
    zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
fi

# build the package
cd $PKG
makepkg -l y -c n $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz

echo "Package $TMP/$NAME-$VERSION-$ARCH-$BUILD.tgz created successfully!"
