#!/bin/sh
# Set initial variables:

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-xpaint
rm -rf $PKG
mkdir -p $PKG

VERSION=2.7.0
ARCH=sparc
BUILD=0

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

# Unpack
cd $TMP
rm -rf xpaint-$VERSION
tar -jxvf $CWD/xpaint-$VERSION.tar.bz2
cd xpaint-$VERSION

# Patch
# Found a funny Imakefile problem:
zcat $CWD/patches-splack/xpaint-2.7.0-Imakefilefix-1.diff.gz | \
  patch -p0 --verbose --backup --suffix=.orig -E
zcat $CWD/patches-splack/xpaint-2.7.0-librw_errno_fixes-1.diff.gz | \
  patch -p0 --verbose --backup --suffix=.orig -E

# Build
xmkmf -a
# make Makefiles
# make includes
make xaw
# For a different look, replace the above line with the one below
# make xaw95static

# Finish
strip xpaint
mkdir -p $PKG/usr/X11R6/bin
cat xpaint > $PKG/usr/X11R6/bin/xpaint
chmod 755 $PKG/usr/X11R6/bin/*
chown -R root.bin $PKG/usr/X11R6/bin
mkdir -p $PKG/etc/X11/app-defaults
cat XPaint.ad > $PKG/etc/X11/app-defaults/XPaint
mkdir -p $PKG/usr/X11R6/man/man1
cat xpaint.man | gzip -9c > $PKG/usr/X11R6/man/man1/xpaint.1.gz
mkdir -p $PKG/usr/X11R6/include/X11/bitmaps $PKG/usr/X11R6/include/X11/pixmaps

( cd bitmaps
  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/*
chown -R root.root $PKG/usr/X11R6/include/X11/bitmaps
chmod 644 $PKG/usr/X11R6/include/X11/pixmaps/*
chown -R root.root $PKG/usr/X11R6/include/X11/pixmaps
mkdir -p $PKG/usr/doc/xpaint-$VERSION

# Documentation
cp -a \
  ChangeLog INSTALL README README.PNG README.old TODO Doc/CHANGES Doc/Operator.doc \
  $PKG/usr/doc/xpaint-$VERSION
chmod 644 $PKG/usr/doc/xpaint-$VERSION/*
chown -R root.root $PKG/usr/doc/xpaint-$VERSION

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

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

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