#!/bin/sh

PKGNAM=windowmaker
SRCNAM=WindowMaker
VERSION=${VERSION:-20060427cvs}
BUILD=${BUILD:-1}

. /etc/pkghelpers
pkghelpers_env

rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf $SRCNAM-$VERSION
tar xjvf $CWD/$SRCNAM-$VERSION.tar.bz2
cd $SRCNAM-$VERSION

zcat $CWD/windowmaker.no-mmx.diff.gz | patch -p1 -F 3 || exit 1
sh autogen.sh

# Make sure ownerships and permissions are sane:
pkghelpers_permissions

# This should be non-interactive where possible.
zcat $CWD/wmaker.inst.diff.gz | patch -p1 --verbose || exit 1

LINGUAS="`cd po ; /bin/ls *.po | sed 's/.po//g'`" \
GNUSTEP_LOCAL_ROOT=/usr/lib$LIBSUFFIX/GNUstep \
CFLAGS="$SLKCFLAGS" \
NLSDIR=/usr/share/locale \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --mandir=/usr/man \
  --sysconfdir=/etc/X11 \
  --enable-gnome \
  --enable-kde \
  --enable-usermenu \
  --with-appspath=/usr/lib$LIBSUFFIX/GNUstep/Applications \
  --build=$ARCH-$DISTRO-linux

make -j $NUMJOBS || exit 1
# NLS bugs like -i
make -i || exit 1

# Change /usr/local/GNUstep to /usr/lib/GNUstep in the WMRootMenu:
zcat $CWD/windowmaker.wmrootmenu.diff.gz | patch -p1 --verbose || exit  1

if [ "$LIBSUFFIX" != "" ]; then
  sed -i s%/usr/lib/GNUstep%/usr/lib$LIBSUFFIX/GNUstep/% WindowMaker/Defaults/WMRootMenu || exit 1
fi

make -i install DESTDIR=$PKG || exit 1

# Install documentation:
mkdir -p $PKG/usr/doc/WindowMaker-$VERSION
cp -a \
  AUTHORS BUGFORM BUGS COPYING COPYING.WTFPL FAQ FAQ.I18N FAQ.I18N.cs FAQ.I18N.sk INSTALL INSTALL.cs INSTALL.es INSTALL.fr INSTALL.pt INSTALL.sk MIRRORS NEWS README README.definable-cursor README.pt TODO \
   $PKG/usr/doc/WindowMaker-$VERSION

cd $TMP
rm -rf $SRCNAM-extra-0.1
tar xzvf $CWD/$SRCNAM-extra-0.1.tar.gz
cd $SRCNAM-extra-0.1
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib$LIBSUFFIX \
  --with-iconsdir=/usr/share/pixmaps \
  --build=$ARCH-$DISTRO-linux
make -j $NUMJOBS || exit 1
make install DESTDIR=$PKG || exit 1

mkdir -p $PKG/etc/X11/xinit
cat $CWD/xinitrc.wmaker > $PKG/etc/X11/xinit/xinitrc.wmaker
chmod 755 $PKG/etc/X11/xinit/xinitrc.wmaker
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
pkghelpers_fixup
pkghelpers_makepkg