#!/bin/sh
#
# Slackware build script for BLAS
# Copyright (C) 2009,2011 Georgi D. Sotirov <gdsotirov@dir.bg>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Visit SlackPack at http://sotirov-bg.net/slackpack/
#
# $Id: blas.SlackBuild,v 1.2 2011/10/20 21:21:58 gsotirov Exp $
#
. ../../slack-package.conf

NAME=blas
NAME_UC=`echo $NAME | tr [:lower:] [:upper:]`
VERSION=`stat --format %y $NAME.tgz | awk '{ print $1}' | awk -F'-' '{ print $1 $2 $3}'`
BUILD=${BUILD:-1}
ARCH=${ARCH:-i486}

TMP=${TMP:-/tmp}
CWD=${CWD:-`pwd`}

if [ ! -e $TMP ]; then
  mkdir -p $TMP # location to build the source
fi

SRC=$TMP/$NAME-$VERSION
if [ -e $SRC -a -d $SRC ]; then
  echo "Source directory '$SRC' already exists! Plaese, move it or remove it."
  exit 1
fi

PKG=$TMP/package-$NAME
if [ -e $PKG -a -d $PKG ]; then
  echo "Package creation directory '$PKG' already exists! Please, move it or remove it."
  exit 2
fi
mkdir -p $PKG

# Extract the source
cp -a $NAME.tgz $NAME-$VERSION.tar.gz
cd $TMP
tar -xvf $CWD/$NAME-$VERSION.tar.gz
mv $TMP/$NAME_UC $SRC
cd $SRC
fix_source

# Patch the source
patch -p1 < $CWD/$NAME-$VERSION.diff

# Configure, build and install
if [ "$ARCH" = "i386" ]; then
  SLKFFLAGS="-O3 -mieee-fp -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKFFLAGS="-O3 -mieee-fp -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKFFLAGS="-O3 -mieee-fp -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKFFLAGS="-O3 -mieee-fp"
else
  SLKFFLAGS="-O2"
fi

FFLAGS=$SLKFFLAGS \
make -j4 || exit 1

# Install
install -d -m 755 $PKG/usr/lib${LIBDIRSUFFIX}
install -m 644 $SRC/libblas.so $PKG/usr/lib${LIBDIRSUFFIX}
install -d -m 755 $PKG/usr/doc/$NAME-$VERSION
install -m 644 $CWD/README  $PKG/usr/doc/$NAME-$VERSION/README
install -m 644 $CWD/COPYING $PKG/usr/doc/$NAME-$VERSION/COPYING

# Prepare the package
( cd $PKG; strip_lib )

mkdir -p $PKG/install
install -m 644 $CWD/slack-desc     $PKG/install/slack-desc
install -m 644 $CWD/slack-required $PKG/install/slack-required

mkdir -p $PKG/usr/src/slackbuilds/$NAME-$VERSION
install -m 644 $CWD/slack-desc          $PKG/usr/src/slackbuilds/$NAME-$VERSION/slack-desc
install -m 644 $CWD/slack-required      $PKG/usr/src/slackbuilds/$NAME-$VERSION/slack-required
install -m 644 $CWD/README              $PKG/usr/src/slackbuilds/$NAME-$VERSION/README
install -m 644 $CWD/COPYING             $PKG/usr/src/slackbuilds/$NAME-$VERSION/COPYING
install -m 644 $CWD/$NAME-$VERSION.diff $PKG/usr/src/slackbuilds/$NAME-$VERSION/$NAME-$VERSION.diff
install -m 644 $CWD/$NAME.SlackBuild    $PKG/usr/src/slackbuilds/$NAME-$VERSION/$NAME.SlackBuild

# Pack
cd $PKG
PKGNAME=$NAME-$VERSION-$ARCH-$BUILD$MYIN
makepkg -l y -c n -p $PKG_DIR/$PKGNAME.txz
cd $PKG_DIR
md5sum $PKGNAME.txz > $PKGNAME.txz.md5
cat $PKG/install/slack-desc > $PKG_DIR/$PKGNAME.txt

# Clean up (optionally)
if [ "$1" == "--cleanup" ]; then
  rm -rf $SRC
  rm -rf $PKG
fi

