#!/bin/bash
#
# got it from ftp://ftp.slackware.com/pub/slackware/slackware-12.0/source/d/subversion/subversion.SlackBuild
# small changes by rhatto at riseup.net
#

# needs a function from simplepkg
function default_version {

  # get version from /etc/slackware-version
  if [ -f "$1/etc/slackware-version" ]; then
    cat $1/etc/slackware-version | awk '{ print $2 }' | sed -e 's/.0$//'
  else
    echo "none"
  fi

}

if [ -s "slack-required" ]; then
  echo Recomended and required packages for building subversion are:
  cat slack-required | sed -e 's/^/\t/'
  if [ "$INTERACT" != "no" ]; then
    echo If you dont have those installed, press Ctrl-C. Otherwise, hit ENTER.
    read crap
#  else
#    echo Sleeping 3 seconds...
#    sleep 3
  fi
fi

CWD="`pwd`"

if [ -f ~/.slackbuildrc ]; then
  source ~/.slackbuildrc
elif [ -f /etc/slackbuildrc ]; then
  source /etc/slackbuildrc
fi

# default settings
PACKAGE="subversion"
ARCH=${ARCH:=i486}
VERSION=${VERSION:=1.4.4}
BUILD=${BUILD:=1rha}
SRC_DIR=${SRC:=$CWD}
TMP=${TMP:=/tmp}
REPOS=${REPOS:=$TMP}
DISTRO_VERSION="`default_version`"

LIBDIR=/usr/lib
if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  LIBDIR="/usr/lib64"
  SLKCFLAGS="-O2 -fPIC"
fi

# -------  error codes for createpkg  --------------
ERROR_WGET=31;      ERROR_MAKE=32;      ERROR_INSTALL=33
ERROR_MD5=34;       ERROR_CONF=35;      ERROR_HELP=36
ERROR_TAR=37;       ERROR_MKPKG=38;     ERROR_GPG=39
ERROR_PATCH=40

RTOOL="wget"
PACKAGE_EXT="bz2"
SRC="$PACKAGE-$VERSION.tar.$PACKAGE_EXT"
URL="http://subversion.tigris.org/downloads/$SRC"

SRC_DIR="$SRC_DIR/$PACKAGE"
mkdir -p $SRC_DIR

if [ "$RTOOL" == "wget" ] && [ ! -f "$SRC_DIR/$SRC" ]; then
  wget "$URL" -O "$SRC_DIR/$SRC" || exit $ERROR_WGET
fi

wget http://svnbook.red-bean.com/nightly/en/svn-book-html.tar.bz2 -O $SRC_DIR/svn-book-html.tar.bz2 || exit $ERROR_WGET
chmod 644 $SRC_DIR/svn-book-html.tar.bz2

TMP=$TMP/$PACKAGE
PKG=$TMP/package-subversion
rm -rf $TMP
mkdir -p $PKG

cd $TMP/$PACKAGE
tar xvf $SRC_DIR/$SRC || exit $ERROR_TAR
cd subversion-$VERSION

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 {} \;

if [ "$DISTRO_VERSION" == "12.0" ]; then
  APXS="--with-apxs=/usr/sbin/apxs"
fi

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=$LIBDIR \
  --enable-shared \
  --disable-static \
  --with-apr=/usr \
  --with-apr-util=/usr \
  --with-neon=/usr \
  --with-pic \
  --with-ssl \
  --with-zlib \
  --build=$ARCH-slackware-linux $APXS || exit $ERROR_CONF

make -j4 || exit $ERROR_MAKE
make swig-py || exit $ERROR_MAKE
make install DESTDIR=$PKG || exit $ERROR_INSTALL
make install-docs DESTDIR=$PKG || exit $ERROR_INSTALL
make install-swig-py DESTDIR=$PKG || exit $ERROR_INSTALL

( 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
)
rm -rf $PKG/usr/info
gzip -9 $PKG/usr/man/man?/*

# What is this junk for?  Since I don't know, I'll erase it.  :-)
rm -rf $PKG/usr/build

cp -a \
  BUGS CHANGES COMMITTERS COPYING HACKING INSTALL README TRANSLATING doc \
  $PKG/usr/doc/subversion-$VERSION
# too big && useless for most || if you think not, can be found in the source tarball
rm -rf $PKG/usr/doc/subversion-$VERSION/doc/tools
# Add the HTML svn book:
( cd $PKG/usr/doc/subversion-$VERSION
  tar xjf $SRC_DIR/svn-book-html.tar.bz2
  mv svn-book-html book
  cd book
  find . -type d -exec chmod 755 {} \;
  find . -type f -exec chmod 644 {} \;
  chown -R root:root .
  find . \
  \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 -o perm 2755 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;
) 

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -c y -l y $REPOS/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz || exit $ERROR_MKPKG

if [ "$CLEANUP" == "yes" ]; then
  rm -rf $TMP
fi