#!/bin/sh
CWD=$(pwd)
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-${PKGNAM}

VERSION=1.4.3
ARCH=${ARCH:-i486}
BUILD=2

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf xfm-$VERSION
tar xzvf $CWD/xfm-$VERSION.tar.gz
cd xfm-$VERSION
zcat $CWD/xfm.malloc.void.diff.gz | patch -p1 --verbose || exit 1
xmkmf
make Makefiles
make || exit 1
# 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.
rm -rf /usr/lib/X11/xfm
make install
# OK, now build the package:
( cd src
  mkdir -p $PKG/usr/bin
  strip xfm xfmtype
  cat xfm > $PKG/usr/bin/xfm
  cat xfmtype > $PKG/usr/bin/xfmtype )
cat /usr/bin/xfm.install > $PKG/usr/bin/xfm.install
chmod 755 $PKG/usr/bin/*
mkdir -p $PKG/etc/X11/app-defaults
cat /etc/X11/app-defaults/Xfm > $PKG/etc/X11/app-defaults/Xfm
mkdir -p $PKG/usr/lib/X11/xfm
# Also processed during "make install"... sigh...
( cd $PKG/usr/lib/X11/xfm
  cp -a /usr/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/lib/X11/xfm
  rm -rf icons pixmaps bitmaps
  ln -sf /usr/include/X11/bitmaps .
  ln -sf /usr/include/X11/pixmaps .
  ln -sf /usr/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/include/X11/bitmaps $PKG/usr/include/X11/pixmaps
( cd lib
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/include/X11/bitmaps \; )
( cd contrib/misc
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/include/X11/bitmaps \; )
( cd contrib/fileicons
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/include/X11/bitmaps \; )
( cd contrib/3dicons
  find . -name "*.xpm" -exec cp -a {} $PKG/usr/include/X11/pixmaps \;
  find . -name "*.xbm" -exec cp -a {} $PKG/usr/include/X11/bitmaps \; )
chmod 644 $PKG/usr/include/X11/bitmaps/*
chmod 644 $PKG/usr/include/X11/pixmaps/*
# Man pages:
mkdir -p $PKG/usr/man/man1
( cd src
  cat xfm.man | gzip -9c > $PKG/usr/man/man1/xfm.1.gz
  cat xfmtype.man | gzip -9c > $PKG/usr/man/man1/xfmtype.1.gz )
mkdir -p $PKG/usr/doc/xfm-$VERSION
cp -a \
  COPYING ChangeLog INSTALL README README-1.2 README-1.4 TODO \
  $PKG/usr/doc/xfm-$VERSION
chmod 644 $PKG/usr/doc/xfm-$VERSION/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/xfm-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/xfm-$VERSION
  rm -rf $PKG
fi