#!/bin/bash

# bzip2.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-2004

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="" ;;
   sparc)   SLKCFLAGS="" ;;
   *)       SLKCFLAGS="" ;;
esac

# Extract source:
tar zxvvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# This should be ok, since libbz2.so.1.0 will still exist.
zcat $CWD/bzip2-1.0.3.saneso.diff.gz | patch -p1 || failpatch
# Fix a Makefile bug that leaves out support for files > 2GB:
zcat $CWD/bzip2.bigfiles.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= -march=armv3 -mtune=xscale \1/' Makefile-libbz2_so | make -f-
sed -e 's/^CFLAGS=\(.*\)$/CFLAGS= -march=armv3 -mtune=xscale \1/' Makefile | make -f-

# Includes:
mkdir -p $PKG/usr/include
cp -a bzlib.h $PKG/usr/include
chown root:root $PKG/usr/include/bzlib.h
chmod 644 $PKG/usr/include/bzlib.h

# Static libraries:
mkdir -p $PKG/usr/lib
cp -a libbz2.a $PKG/usr/lib/libbz2.a

# Shared objects:
mkdir -p $PKG/lib
cp -a libbz2.so.$VERSION $PKG/lib/libbz2.so.$VERSION
chmod 644 $PKG/usr/lib/libbz2.a
chmod 755 $PKG/lib/libbz2.so.$VERSION

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

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

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a CHANGES LICENSE README README.COMPILATION.PROBLEMS Y2K_INFO bzip2.txt *.html \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Link up them links
( cd $PKG
  ( cd lib
    rm -f libbz2.so.1.0 libbz2.so.1
    ln -sf libbz2.so.$VERSION libbz2.so.1.0
    ln -sf libbz2.so.1.0 libbz2.so.1 )
  ( cd usr/lib ; ln -sf ../../lib/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 -m755 $file $PKG/usr/bin
  install -m644 $file.1 $PKG/usr/man/man1
done

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown (root:bin), slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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