#!/bin/bash
#
#  rrdtool.SlackBuild 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 any later version.
#
#  rrdtool.SlackBuild 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
# Copyright (c) 2006 Eric Hameleers <alien@sox.homeip.net>
# Distributed under the terms of the GNU General Public License, Version 2
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:        Eric Hameleers <alien@sox.homeip.net>
# For:       rrdtool
# URL:       http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
# Needs:     
# Changelog:
# 1.2.8-1:   18/May/2005 by Eric Hameleers <alien@sox.homeip.net>
#            * Initial build.
# 1.2.12-1:  21/feb/2006 by Eric Hameleers <alien@sox.homeip.net>
#            * Upgrade. The cgilib is no longer needed as of 1.2.12.
# 
# Run 'sh SlackBuild --cleanup' to build a Slackware package.
# The package (.tgz) plus descriptive .txt file are created in /tmp .
# Install using 'installpkg'. 
#
# Small modifications made by rhatto at riseup.net
#
# -----------------------------------------------------------------------------

if [ -s "slack-required" ]; then
  echo Recomended and required packages for building rrdtool 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

# --- INIT ---
# Set initial variables:
CWD=`pwd`

if [ "$INTERACT" != "no" ]; then
  echo "Hit ENTER do continue, Crtl-C to abort"
  read crap
fi

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

PACKAGE="rrdtool"
PRGNAM="$PACKAGE"
ARCH=${ARCH:=i486}
VERSION=${VERSION:=1.2.26}
BUILD=${BUILD:=1rha}
SRC_DIR=${SRC:=$CWD}
TMP=${TMP:=/tmp}
REPOS=${REPOS:=$TMP}

# -------  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

if [ "$ARCH" == "x86_64" ]; then
  export LDFLAGS="-L/lib64 -L/usr/lib64"
  LIBDIR=/usr/lib64
else
  LIBDIR=/usr/lib
fi

SRC="$PRGNAM-$VERSION.tar.gz"
URL="http://oss.oetiker.ch/rrdtool/pub/$SRC"

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

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

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

if [ ! -d $TMP/tmp-$PRGNAM ]; then
  mkdir -p $TMP/tmp-$PRGNAM # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# --- PACKAGE BUILDING ---

echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

rm -rf $PKG/*

cd $PKG

# Explode the package framework:
if [ -f $CWD/_$PRGNAM.tar.gz ]; then
  explodepkg $CWD/_$PRGNAM.tar.gz || exit $ERROR_TAR
fi

cd $TMP

# --- TARBALL EXTRACTION,PATCH,MODIFY ---

echo "Extracting the program tarball for $PRGNAM..."
tar xvf $SRC_DIR/${PRGNAM}-${VERSION}.tar.gz || exit $ERROR_TAR

cd ${PRGNAM}-${VERSION}
chown -R root.root .

# --- BUILDING ---

echo Building ...

CPPFLAGS="$SLKCFLAGS"
CFLAGS="$SLKCFLAGS"
export CPPFLAGS CFLAGS
./configure --prefix=/usr \
            --localstatedir=/var \
            --sysconfdir=/etc \
            --libdir=$LIBDIR \
            --enable-perl-site-install \
            2>&1 | tee $TMP/configure-${PRGNAM}.log || exit $ERROR_CONF
make 2>&1 | tee $TMP/make-${PRGNAM}.log || exit $ERROR_MAKE

#
# Install all the needed stuff to the package dir
#
# Use installwatch if available:
which installwatch > /dev/null 2>&1
if [ $? == 0 ]; then
  installwatch -o $TMP/install-${PRGNAM}.log make DESTDIR=$PKG install || exit $ERROR_INSTALL
else
  make DESTDIR=$PKG install 2>&1 |tee $TMP/install-${PRGNAM}.log || exit $ERROR_INSTALL
fi

# unneeded perl stuff
find $PKG/usr/lib/perl5/ -name Bundle -type d | xargs rm -rf
find $PKG/usr/lib/perl5/ -name perllocal.pod | xargs rm -f
find $PKG/usr/lib/perl5/ -name .packlist | xargs rm -f
find $PKG/usr/lib/perl5/ -type f -size 0c | xargs rm -f
# Remove empty directories (assuming two levels of file-less directories)
find $PKG/usr/lib/perl5/ -type d | xargs rmdir 2>/dev/null
find $PKG/usr/lib/perl5/ -type d | xargs rmdir 2>/dev/null

# Screw-uppings
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/share/rrdtool/examples $PKG/usr/doc/$PRGNAM-$VERSION/
mv $PKG/usr/share/doc/*/html $PKG/usr/doc/$PRGNAM-$VERSION/
rm -rf $PKG/usr/share/doc
mkdir -p $PKG/usr/man/man3/
mv $PKG/usr/share/man/man3/* $PKG/usr/man/man3/
rm -rf $PKG/usr/share/man


# --- DOCUMENTATION ---

DOCS="CHANGES CONTRIBUTORS COPYING COPYRIGHT NEWS NT-BUILD-TIPS.txt \
      README THREADS TODO"

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS \
      $PKG/usr/doc/$PRGNAM-$VERSION
chmod -R a-w $PKG/usr/doc/$PRGNAM-$VERSION/*

# Compress the man page(s)
gzip -9f $PKG/usr/man/*/*

# Strip binaries
( 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
)


# --- OWNERSHIP, RIGHTS ---

chmod -R o-w $PKG
chown root:bin $PKG/usr/bin/* $PKG/usr/sbin/* $PKG/bin/* $PKG/sbin/* \
               $PKG/usr/X11R6/bin/* $PKG/opt/kde/bin/* 2>/dev/null


# --- PACKAGE DESCRIPTION ---

mkdir -p $PKG/install

cat << EOF > $PKG/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

       |-----handy-ruler-----------------------------------------------------|
rrdtool: rrdtool (Time-series data storage and graphing software)
rrdtool:
rrdtool: RRDtool (Round Robin Database Tool) is time-series data storage and
rrdtool: graphing utility created by the author of MRTG. Using RRDtool, you
rrdtool: can write your own MRTG-like tools in a matter minutes with only a
rrdtool: few lines of Perl or shell code.
rrdtool:
rrdtool:
rrdtool:
rrdtool:
rrdtool:
EOF

if [ -f $CWD/doinst.sh ]; then
  cat $CWD/doinst.sh > $PKG/install/doinst.sh
fi


# --- BUILDING ---

# Build the package:
cd $PKG

# mv usr/lib/perl usr/lib/perl5

makepkg --linkadd y --chown n $REPOS/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.tgz \
  2>&1 | tee $TMP/makepkg-${PRGNAM}.log || exit $ERROR_MKPKG
#(cd $REPOS && md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.tgz > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.tgz.md5)
#cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $REPOS/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.txt


# --- CLEANUP ---

# Clean up the extra stuff:
if [ "$CLEANUP" == "yes" ]; then
  rm -rf $TMP
fi