#!/bin/bash

# xfm.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 27-May-2005

# 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 zxvvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
patch --verbose -lp0 < $PORTCWD/sources/xfm-1.4.3-arm.patch

# Create the make files:
xmkmf || failmisc
make Makefiles

# Build:
make || failmake

# Pat says:
# OK, this sucks, but otherwise there's stuff that isn't build, and I'm not putting
# a load of cpp/sed junk in here...  so your system will be spammed here.
# Stuart says: Sure, I'm all for it. It's a beautiful day outside and I'm
#              just chilling in the office; as I ponder about life and my future,
#              where I'll take myself.
rm -rf /usr/X11R6/lib/X11/xfm
make install

# OK, now build the package:
( cd src
  mkdir -p $PKG/usr/X11R6/bin
  install -m755 xfmtype xfm $PKG/usr/X11R6/bin )
install -m755 /usr/X11R6/bin/xfm.install $PKG/usr/X11R6/bin

# Install some app default stuff:
mkdir -p $PKG/etc/X11/app-defaults
cat /etc/X11/app-defaults/Xfm > $PKG/etc/X11/app-defaults/Xfm
mkdir -p $PKG/usr/X11R6/lib/X11/xfm

# Also processed during "make install"... sigh...
( cd $PKG/usr/X11R6/lib/X11/xfm
  cp -a /usr/X11R6/lib/X11/xfm/dot.xfm . )

# Fake these directories and use the system ones.  The system directories are
# already in the app-defaults file, but this makes it clear for the user where
# the icons are.
( cd $PKG/usr/X11R6/lib/X11/xfm
  rm -rf icons pixmaps bitmaps
  ln -sf /usr/X11R6/include/X11/bitmaps .
  ln -sf /usr/X11R6/include/X11/pixmaps .
  ln -sf /usr/X11R6/include/X11/pixmaps icons )

# Put the icons in the standard system locations.  Some of these are
# locations where fvwm95 already puts icons, but that shouldn't matter much.
mkdir -p $PKG/usr/X11R6/include/X11/bitmaps $PKG/usr/X11R6/include/X11/pixmaps
( cd lib
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/X11R6/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/X11R6/include/X11/bitmaps \; )
( cd contrib/misc
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/X11R6/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/X11R6/include/X11/bitmaps \; )
( cd contrib/fileicons
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/X11R6/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/X11R6/include/X11/bitmaps \; )
( cd contrib/3dicons
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/X11R6/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/X11R6/include/X11/bitmaps \; )
chmod 644 $PKG/usr/X11R6/include/X11/bitmaps/*
chmod 644 $PKG/usr/X11R6/include/X11/pixmaps/*

# Man pages:
mkdir -p $PKG/usr/X11R6/man/man1
( cd src
  install -m644 xfm.man $PKG/usr/X11R6/man/man1/xfm.1
  install -m644 xfmtype.man $PKG/usr/X11R6/man/man1/xfmtype.1 )

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a COPYING ChangeLog INSTALL README README-1.2 README-1.4 TODO \
      $PKG/usr/doc/$PACKAGE-$VERSION

# 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
   . $PORTCWD/arm/pkger
 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
