#!/bin/bash

# checkinstall.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# Version of installwatch:
INSTWAT=${INSTWAT:-0.7.0beta4}

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/source/$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)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# This patch causes checkinstall to use the native Slackware package
# building tool, though, and allows us to meet the "good enough"
# standard of excellence.  ;-)
zcat $CWD/checkinstall.followspecsbetter.diff.gz | patch -p1 --verbose --backup --suffix=.orig

# If there's a checkinstallrc, move it:
if [ -r /etc/checkinstallrc ]; then
  mv /etc/checkinstallrc /etc/checkinstallrc.bak
fi

# Build:
make || exit 1

# Install onto filesystem: honestly is this necessary?!
make install

mkdir -p $PKG/usr/sbin
cat /usr/sbin/checkinstall > $PKG/usr/sbin/checkinstall
chmod 755 $PKG/usr/sbin/checkinstall
# Note that we DO NOT install "makepak".  Don't use that -- it's broken.

# Our patch also places the config file in /etc since Linux standards say config
# files must not go under /usr:
mkdir -p $PKG/etc/checkinstall
cat /etc/checkinstall/checkinstallrc > $PKG/etc/checkinstall/checkinstallrc.new

# Install NLS files:
mkdir -p $PKG/usr/share/locale/es/LC_MESSAGES
cp -favv /usr/share/locale/es/LC_MESSAGES/checkinstall.mo $PKG/usr/share/locale/es/LC_MESSAGES/checkinstall.mo

# Docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a BUGS COPYING CREDITS Changelog FAQ INSTALL NLS_SUPPORT README RELNOTES TODO \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Now install the installwatch components:
cd installwatch-$INSTWAT
  mkdir -p $PKG/usr/bin
  cat /usr/bin/installwatch > $PKG/usr/bin/installwatch
  chmod 755 $PKG/usr/bin/installwatch
  mkdir -p $PKG/usr/lib
  cat installwatch.so > $PKG/usr/lib/installwatch.so
  chmod 755 $PKG/usr/lib/installwatch.so
  mkdir -p $PKG/usr/doc/checkinstall-$VERSION/installwatch-$INSTWAT
  cp -a \
    BUGS CHANGELOG COPYING INSTALL README TODO VERSION \
    $PKG/usr/doc/checkinstall-$VERSION/installwatch-$INSTWAT
cd ..

# Add install script for checkinstallrc:
mkdir -p $PKG/install
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/checkinstall/checkinstallrc.new
EOF

# 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

# /extra has packages in separate dirs:
export PKGSERIES=$PKGSERIES/$PACKAGE
# 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
