#!/bin/bash

# glut.SlackBuild
# Heavily based on the original Slackware build script,
# Modified by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# 25-Jul-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/l/glut
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)    export ARCH_CFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export ARCH_CFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/glut.solink.diff.gz | patch -p1 --verbose
#zcat $CWD/glut.i486i686.diff.gz | patch -p1 --verbose
zcat $PORTCWD/patches/makefile.arm.diff.gz | patch -p1

# Build:
rm -f Glut.cf
cp -f linux/Glut.cf .
./mkmkfiles.imake
cd lib/glut
rm -f Makefile
cp -f ../../linux/Makefile .
make depend
make || failmake
( cd ../glsmap ; make )
( cd ../mui ; make )
( cd ../../man
  sed -i 's/gle//' Imakefile 
  xmkmf )
cd ../..
mkdir -p $PKG/usr/X11R6/lib
install -m755 lib/glut/libglut.so.3.7 $PKG/usr/X11R6/lib
( cd $PKG/usr/X11R6/lib
  ln -sf libglut.so.3.7 libglut.so.3
  ln -sf libglut.so.3 libglut.so )
cp lib/*/lib*.a $PKG/usr/X11R6/lib
rm -f include/GL/tube.h
mkdir -p $PKG/usr/X11R6/include
cp -rp include/* $PKG/usr/X11R6/include
( cd man ; make DESTDIR=$PKG MANDIR=/usr/X11R6/man/man3 install.man )
( cd $PKG/usr/X11R6/man/man3
  for file in *.3xglut ; do
    mv $file `basename $file xglut`
  done 
  gzip -9 * )

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a FAQ.glut NOTICE README* \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Slackware policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   . $PORTCWD/arm/pkger
 else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
