#!/bin/sh
CWD=`pwd`
PKG=/tmp/package-fvwm

VERSION=2.4.19
ARCH=${ARCH:-i486}
BUILD=${BUILD:-4}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG
cd /tmp
rm -rf fvwm-$VERSION
tar xjvf $CWD/fvwm-$VERSION.tar.bz2
cd fvwm-$VERSION
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
#zcat $CWD/fvwm.colormap.diff.gz | patch -p1 --backup --suffix=.orig
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr/X11R6 \
  --sysconfdir=/etc/X11/fvwm2 \
  --enable-extras \
  $ARCH-slackware-linux
make
make install DESTDIR=$PKG
( cd $PKG/usr/X11R6/man/man1
  rm -f fvwm-root.1
  ln -sf xpmroot.1.gz fvwm-root.1.gz )
gzip -9 $PKG/usr/X11R6/man/man?/*.?
chown -R root.bin $PKG/usr/X11R6/bin
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/usr/doc/fvwm-$VERSION
cp -a \
  AUTHORS COPYING INSTALL INSTALL.fvwm README docs sample.fvwmrc \
  $PKG/usr/doc/fvwm-$VERSION
rm -f \
  $PKG/usr/doc/fvwm-$VERSION/docs/ChangeLog \
  $PKG/usr/doc/fvwm-$VERSION/docs/NEWS \
  $PKG/usr/doc/fvwm-$VERSION/docs/Makefile* \
  $PKG/usr/doc/fvwm-$VERSION/sample.fvwmrc/Makefile*
mkdir -p $PKG/usr/X11R6/include/X11/pixmaps
# Add all the old icons that have fallen through the cracks:
( cd $PKG ; tar xzvf $CWD/ancient-icons.tar.gz )
mkdir -p $PKG/etc/X11/xinit
zcat $CWD/xinitrc.fvwm2.gz > $PKG/etc/X11/xinit/xinitrc.fvwm2
chmod 755 $PKG/etc/X11/xinit/xinitrc.fvwm2
mkdir -p $PKG/etc/X11/fvwm2
zcat $CWD/system.fvwm2rc.gz > $PKG/etc/X11/fvwm2/system.fvwm2rc.new
mkdir -p $PKG/usr/X11R6/lib/X11
( cd $PKG/usr/X11R6/lib/X11 && rm -rf fvwm2 )
( cd $PKG/usr/X11R6/lib/X11 && ln -sf ../../../../etc/X11/fvwm2 fvwm2 )
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n ../fvwm-$VERSION-$ARCH-$BUILD.tgz