#!/bin/bash

# lvm2.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>

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

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

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/LVM2.$VERSION.tar.xz
cd LVM2* || exit 1
slackhousekeeping

# Configure:
#install -vpm644 /usr/include/libdevmapper.h include/
CFLAGS="$SLKCFLAGS" \
./configure \
   --mandir=/usr/man \
   --disable-readline \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-usrlibdir=/usr/lib${LIBDIRSUFFIX} \
   --enable-cmdlib \
   --enable-dmeventd \
   --enable-static_link \
   --with-lvm1=internal \
   --enable-pkgconfig \
   --enable-udev_sync \
   --enable-udev_rules \
   --with-udev-prefix="" \
   --with-device-uid=0 \
   --with-device-gid=6 \
   --with-device-mode=0664 || exit 1

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

mv $PKG/etc/lvm/lvm.conf $PKG/etc/lvm/lvm.conf.new

if [ -d $PKG/usr/share/man ]; then # --mandir was ignored
  mv $PKG/usr/share/man $PKG/usr
  rmdir $PKG/usr/share
fi

# Move the binary and shared library to /sbin and /lib{,64}:
mkdir -p $PKG/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  for file in lib*.so.?.* ; do
    mv $file ../../lib${LIBDIRSUFFIX}
    ln -sf ../../lib${LIBDIRSUFFIX}/$file .
  done
  cp -a lib*.so.? ../../lib${LIBDIRSUFFIX}
)
# The Makefile is DIW.
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  find . -name "*.so" -type l | cut -b3- | while read file ; do
    rm -f $file
    ln -sf ${file}.?.* $file
  done
)

# Copy docs:
mkdir -p $PKG/usr/doc/LVM2.$VERSION
cp -favv \
    COPYING* INSTALL README* VERSION WHATS_NEW \
    $PKG/usr/doc/LVM2.$VERSION

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