#!/bin/bash

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

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

# Configure:
zcat $CWD/lm_sensors.makefile.diff.gz | patch -p1 --verbose || failpatch

# This is needed because /usr/local is hardcoded in a few places in
# sensor-detect.  A little better searching by that program (like
# perhaps checking $PATH) would be nice, but for now we'll just
# commit the same error by hardcoding the path like we want it.  ;-)
zcat $CWD/lm_sensors.prefix.usr.diff.gz | patch -p1 --verbose || exit 1

# Build:
make user || failmake

# Install:
make user_install || failinstall

# Nope:
rm -f $PKG/usr/lib/*.a
rm -rf $PKG/usr/include/linux

# Do the .new thing with the config file:
mv $PKG/etc/sensors.conf $PKG/etc/sensors.conf.new

# Add a documentation directory:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION/doc
cp -favv BACKGROUND BUGS CHANGES CONTRIBUTORS COPYING INSTALL QUICKSTART README* TODO lm_sensors.lsm \
         $PKG/usr/doc/$PACKAGE-$VERSION
cd doc
cp -favv FAQ cvs donations fan-divisors fancontrol.txt mkpatch modules progs temperature-sensors version-2 vid \
         $PKG/usr/doc/$PACKAGE-$VERSION/doc

# Install doinst script:
mkdir -p $PKG/install
install -m644 $CWD/slack-desc $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/sensors.conf.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

# 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
