#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp}

VERSION=3.2.4
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

PKG=$TMP/package-xfig
rm -rf $PKG
mkdir -p $PKG

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

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;

zcat $CWD/xfig.png.jpg.prefix.diff.gz | patch -E -p1 --verbose --backup --suffix=.orig || exit 1
# Use Firefox and xpdf, not netscape and acroread:
zcat $CWD/xfig.fig.ad.diff.gz | patch -E -p1 --verbose --backup --suffix=.orig || exit 1
# Put doc files under /usr/doc:
zcat $CWD/xfig.usr.doc.version.diff.gz | patch -E -p1 --verbose --backup --suffix=.orig || exit 1
# Don't install PDF reference manual because the HTML manual is the same thing (and
# also includes a Japanese translation):
zcat $CWD/xfig.no.xfig_ref_en.pdf.diff.gz | patch -E -p1 --verbose --backup --suffix=.orig || exit 1
# Eliminate menu choice for non-distributed (redundant) PDF documentation:
zcat $CWD/xfig.no.pdf.ref.diff.gz | patch -E -p1 --verbose --backup --suffix=.orig || exit 1

xmkmf
make -j4 || exit 1
make install DESTDIR=$PKG || exit 1
make install.man DESTDIR=$PKG || exit 1
make install.libs DESTDIR=$PKG || exit 1
make install.html DESTDIR=$PKG || exit 1

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

gzip -9 $PKG/usr/X11R6/man/man1/*

mkdir -p $PKG/usr/doc/xfig-$VERSION
cp -a \
  CHANGES FIGAPPS LATEX.AND.XFIG README \
  $PKG/usr/doc/xfig-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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

