#!/bin/sh
#
# Build Script to package physfs. working well 
# with physfs-1.1.1.tar.gz layout.
#
# Quick reference:  
# 
# $NAME				: name to use in tgz file
#
# $TARNAME			: usually the mane name used on tgz, but sometimes 
# 					  those names are different. so remember to set up
#					  a proper $TARNAME
#
# $CWD,$TMP ...		: more common variables used to make a reasonable 
#					  name for the package and also select certain flags
#
# pack-$NAME		: working area
#
# pack-$NAME/BUILD	: place to use as DESTDIR
#
# SLKCFLAGS			: classic way to set up compiler flags
#
# slack-desc		: a file with that name MUST exists. 
#
# This script is brought to you by "Slackware Jar Packaging Initiative" 

NAME="physfs"
CWD=${CWD:-`pwd`}
TMP=${TMP:-/tmp}
VERSION=${VERSION:-1.1.1} # don't forget that
TARNAME=$NAME-$VERSION # change it if applicable
ARCH=${ARCH:-i486} # change it if applicable
TRY=${TRY:-1leo}

# MUST be root.
[ `whoami` != root ] && echo "You must be root. Exiting..." && exit 1

# the SLCKFLAGS section use it to set CFLAGS and CXXFLAGS
# some guys does not recommend -Wno-strict-aliasing, but if i want -O2 i will need it, :/
if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686 -Wno-strict-aliasing"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686 -Wno-strict-aliasing"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2  -fno-function-sections -Wno-strict-aliasing"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2  -fno-function-sections -Wno-strict-aliasing"
fi

# cleaning 
rm -rf $TMP/pack-$NAME

# re-creating working area
mkdir -p $TMP/pack-$NAME/BUILD/install

# specific stuff (like upack under $TMP/pack-$NAME) goes there
tar xvzf $TARNAME.tar.gz -C $TMP/pack-$NAME/ 

# physfs needs cmake isntead automake. very intersting!
cd $TMP/pack-$NAME/$TARNAME 
# cmake .
cmake \
-D CMAKE_INSTALL_PREFIX:STRING="/usr" \
-D CMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
-D CMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
.
make
make install DESTDIR=$TMP/pack-$NAME/BUILD

# this version does not create /usr/doc directory...
mkdir -p $TMP/pack-$NAME/BUILD/usr/doc/$TARNAME
cp $TMP/pack-$NAME/$TARNAME/{CHANGELOG.txt,CREDITS.txt,INSTALL.txt,LICENSE.txt,TODO.txt} \
$CWD/$NAME.SlackBuild $TMP/pack-$NAME/BUILD/usr/doc/$TARNAME

# finally, copy slack-desc file
cp $CWD/slack-desc $TMP/pack-$NAME/BUILD/install

# building the package
cd $TMP/pack-$NAME/BUILD
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$TRY.tgz
