#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
   TMP=/tmp
fi
PKG=$TMP/package-xtdfx

if [ ! -d $TMP ]; then
   mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
   mkdir -p $PKG # place for the package to be built
fi

# explode the package framework
cd $PKG
tar xvzf $CWD/_xtdfx.tar.gz

echo "+==========+"
echo "| TDFX-DRI |"
echo "+==========+"
cd $TMP
mkdir TDFX-DRI
cd TDFX-DRI
tar xvzf $CWD/DRI.tgz
tar xvzf $CWD/glu.tgz
tar xvzf $CWD/glut.tgz
tar xvzf $CWD/misc.tgz
make World -C xc CDEBUGFLAGS=-O2 CCOPTIONS="-O2 -ansi -Wall -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -pipe"
touch glu/depend
make -C glu dep CDEBUGFLAGS=-O2 CCOPTIONS="-O2 -ansi -Wall -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -pipe"
make -C glu linux-elf CDEBUGFLAGS=-O2 CCOPTIONS="-O2 -ansi -Wall -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -pipe"
touch glut/depend
make -C glut dep
make -C glut linux-elf CDEBUGFLAGS=-O2 CCOPTIONS="-O2 -ansi -Wall -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -pipe"

###
### Write out the X stuff
###
cat xc/programs/Xserver/XFree86 > $PKG/usr/X11R6/bin/XFCOM
( cd $PKG/var/X11R6/bin ; ln -sf /usr/X11R6/bin/XFCOM X )
cat xc/exports/lib/libGL.so.1.2 > $PKG/usr/X11R6/lib/libGL.so.1.2
( cd $PKG/usr/X11R6/lib ; ln -sf libGL.so.1.2 libGL.so.1 )
( cd $PKG/usr/X11R6/lib ; ln -sf libGL.so.1 libGL.so )
( cd $PKG/usr/X11R6/lib ; ln -sf libGL.so.1.2 libGL.so.3 )
( cd $PKG/usr/X11R6/lib ; ln -sf libGL.so.1.2 libMesaGL.so.3 )
cp xc/exports/lib/modules/*.a $PKG/usr/X11R6/lib/modules
cp xc/exports/lib/modules/drivers/*.o $PKG/usr/X11R6/lib/modules/drivers
cp xc/exports/lib/modules/extensions/*.a \
   $PKG/usr/X11R6/lib/modules/extensions
cp xc/exports/lib/modules/input/*.o $PKG/usr/X11R6/lib/modules/input
cp xc/exports/lib/modules/linux/*.a $PKG/usr/X11R6/lib/modules/linux
cp xc/exports/lib/modules/fonts/*.a $PKG/usr/X11R6/lib/modules/fonts
cp xc/exports/lib/modules/tdfx_dri.so $PKG/usr/X11R6/lib/modules/dri

###
### Write out the documentation
###
cp -a man/GL/*/*.3gl $PKG/usr/X11R6/man/man3

###
### Write out GLU/GLUT
###
cat lib/libGLU.so.1.2.030200 > $PKG/usr/X11R6/lib/libGLU.so.1.2.030200
cat lib/libglut.so.3.7.0 > $PKG/usr/X11R6/lib/libglut.so.3.7.0
( cd $PKG/usr/X11R6/lib ; ln -sf libGLU.so.1.2.030200 libGLU.so.1.2.0 )
( cd $PKG/usr/X11R6/lib ; ln -sf libGLU.so.1.2.0 libGLU.so.1.2 )
( cd $PKG/usr/X11R6/lib ; ln -sf libGLU.so.1.2 libGLU.so.1 )
( cd $PKG/usr/X11R6/lib ; ln -sf libGLU.so.1 libGLU.so )
( cd $PKG/usr/X11R6/lib ; ln -sf libglut.so.3.7.0 libglut.so.3.7 )
( cd $PKG/usr/X11R6/lib ; ln -sf libglut.so.3.7 libglut.so.3 )
( cd $PKG/usr/X11R6/lib ; ln -sf libglut.so.3 libglut.so )
( cd $PKG/usr/X11R6/lib ; ln -sf libGLU.so.1.2.0 libGLU.so.3 )
( cd $PKG/usr/X11R6/lib ; ln -sf libGLU.so.1.2.0 libMesaGLU.so.3 )

###
### Write out the config file
###
mkdir -p $PKG/var/X11R6/lib
chown -R root.root $PKG/var/X11R6/lib
cat XF86Config > $PKG/var/X11R6/lib/XF86Config.TDFX
chown root.root $PKG/var/X11R6/lib/XF86Config.TDFX
chmod 644 $PKG/var/X11R6/lib/XF86Config.TDFX
( cd $PKG/etc ; ln -sf /var/X11R6/lib X11 )

###
### Write out the header files
###
cp -a include/GL/* $PKG/usr/X11R6/include/GL

# strip binaries and libraries
strip $PKG/usr/X11R6/bin/XFCOM

# Fix permissions and ownerships
( cd $PKG/usr/X11R6/include/GL ; chown root.root * )
( cd $PKG/usr/X11R6/include/GL ; chmod 644 * )
chmod 755 $PKG/usr/X11R6/lib/libGLU.so.1.2.030200
chmod 755 $PKG/usr/X11R6/lib/libglut.so.3.7.0
chmod 755 $PKG/usr/X11R6/lib/libGL.so.1.2
( cd $PKG/usr/X11R6/man/man3 ; chown root.root * )
( cd $PKG/usr/X11R6/man/man3 ; chmod 644 * )
( cd $PKG/usr/X11R6/man/man3 ; gzip -9 *.3gl )

# make the package
cd $PKG
echo "y
n" | makepkg $TMP/xtdfx.tgz

# clean up the extra stuff
if [ "$1" = "--cleanup" ]; then
   rm -rf $PKG
   rm -rf $TMP/TDFX-DRI
fi
