#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# bzip2.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-2004
#
# Copyright 2005-2009, 2010  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Import the 'SOLIB' variable setting from the upstream SlackBuild.
# One less thing for us to maintain.
eval $( grep -E "^SOLIB=" $CWD/$PKGNAM.SlackBuild )
[ -z "$SOLIB" ] && { echo "ERROR: Failed to set SOLIB variable for Shared Object version number." ; exit 1 ;}

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# This should be ok, since libbz2.so.1.0 will still exist.
zcat $CWD/bzip2.saneso.diff.gz | patch -p1 || exit

# This hack will go away when the ARMv3l toolchain is ready:
# but.. I sort of like it for historical reasons :-)
# I'd never do it like this again..
sed -e 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/' Makefile-libbz2_so | make -f- $NUMJOBS || exit 1
sed -e 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/' Makefile | make -f- $NUMJOBS || exit 1

# Includes:
mkdir -p $PKG/usr/include
install -vpm644 bzlib.h $PKG/usr/include

# Static libraries:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/
#install -vpm644 libbz2.a $PKG/usr/lib${LIBDIRSUFFIX}/

# Shared objects:
mkdir -p $PKG/lib${LIBDIRSUFFIX}/
install -vpm755 libbz2.so.$SOLIB $PKG/lib${LIBDIRSUFFIX}/
mkdir -vpm755 $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
sed "s|libdir=|libdir=/usr/lib${LIBDIRSUFFIX}|g" $CWD/bzip2.pc > $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/bzip2.pc

# Binaries:
mkdir -p $PKG/bin
install -vpm755 bzip2-shared $PKG/bin/bzip2
install -vpm755 bzip2recover $PKG/bin

# Install man pages:
mkdir -p $PKG/usr/man/man1
install -vpm644 bzip2.1 $PKG/usr/man/man1
( cd $PKG/usr/man/man1 ; ln -vfs bzip2.1 bzip2recover.1 )

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -favv \
   CHANGES LICENSE README* \
   $PKG/usr/doc/$PKGNAM-$VERSION

# Link up them links
( cd $PKG
  ( cd lib${LIBDIRSUFFIX}
    rm -f libbz2.so.1.0 libbz2.so.1
    ln -sf libbz2.so.$SOLIB libbz2.so.1.0
    ln -sf libbz2.so.1.0 libbz2.so.1
  )
  ( cd usr/lib${LIBDIRSUFFIX}
    ln -sf ../../lib${LIBDIRSUFFIX}/libbz2.so.1 libbz2.so
  )
  ( cd bin ; ln -sf bzip2 bunzip2 )
  ( cd bin ; ln -sf bzip2 bzcat )
  mkdir -p usr/bin
  ( cd usr/bin
    ln -sf ../../bin/bzip2 .
    ln -sf ../../bin/bzip2 bunzip2
    ln -sf ../../bin/bzip2 bzcat
    ln -sf bzmore bzless
  )
)

# Install some optional scripts:
for file in bzdiff bzgrep bzmore ; do
  install -vpm755 $file $PKG/usr/bin
  install -vpm644 $file.1 $PKG/usr/man/man1
done

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
