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

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/_v3-dri.tar.gz

echo "+========================================+"
echo "| Glide - Voodoo Banshee/3 - DRI Support |"
echo "+========================================+"
cd $TMP
mkdir Glide
cd Glide
tar xvzf $CWD/glide3x.tar.gz
tar xvzf $CWD/swlibs.tar.gz
chmod +x swlibs/include/make/ostype
( cd $TMP/Glide/glide3x ; ln -s $TMP/Glide/swlibs swlibs )

###
### Glide3x build section
###
# These symlinks must be in place to build a DRI library
( cd $TMP/Glide/glide3x/h3/minihwc ; ln -s linhwc.c.dri linhwc.c )
( cd $TMP/Glide/glide3x/h3/glide3/src ; ln -s gglide.c.dri gglide.c )
( cd $TMP/Glide/glide3x/h3/glide3/src ; ln -s gsst.c.dri gsst.c )
( cd $TMP/Glide/glide3x/h3/glide3/src ; ln -s glfb.c.dri glfb.c )
# The Voodoo Banshee and Voodoo3 are known as an "h3"
export FX_GLIDE_HW=h3
make -C glide3x -f makefile.linux

###
### Write out the Glide3x libraries
###
cat glide3x/h3/lib/libglide3.so.3.10 > $PKG/usr/lib/libglide3.so.3.10
( cd $PKG/usr/lib ; ln -sf libglide3.so.3.10 libglide3.so.3 )
( cd $PKG/usr/lib ; ln -sf libglide3.so.3 libglide3.so )
( cd $PKG/usr/lib ; ln -sf libglide3.so libglide3x.so )
( cd $PKG/usr/lib ; ln -sf libglide3x.so libglide3x.so.3 )

###
### Write out Texus
###
cat swlibs/lib/libtexus.so.1.1 > $PKG/usr/lib/libtexus.so.1.1
( cd $PKG/usr/lib ; ln -sf libtexus.so.1.1 libtexus.so.1 )
( cd $PKG/usr/lib ; ln -sf libtexus.so.1 libtexus.so )
cat swlibs/bin/texus > $PKG/usr/bin/texus

###
### Write out the license documentation
###
cat glide3x/glide_license.txt > $PKG/usr/doc/Glide3/glide_license.txt

###
### Write out the test program
###
cat glide3x/h3/glide3/tests/test00 > $PKG/usr/bin/testGlideDRI

strip $PKG/usr/bin/*

###
### Fix permissions and ownerships
###
( cd $PKG/usr/bin ; chown root.bin * )
( cd $PKG/usr/bin ; chmod 755 * )
chmod 755 $PKG/usr/lib/{libglide3.so.3.10,libtexus.so.1.1}

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

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