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

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-glide.tar.gz

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

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

###
### Glide2x build section
###
# The Voodoo Banshee and Voodoo3 are known as an "h3"
export FX_GLIDE_HW=h3
make -C glide2x -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
( cd $PKG/usr/doc ; ln -sf Glide3 Glide2 )

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

###
### Write out the Glide2x libraries
###
cat glide2x/h3/lib/libglide.so.2.60 > $PKG/usr/lib/libglide.so.2.60
( cd $PKG/usr/lib ; ln -sf libglide.so.2.60 libglide.so.2 )
( cd $PKG/usr/lib ; ln -sf libglide.so.2 libglide.so )
( cd $PKG/usr/lib ; ln -sf libglide.so.2.60 libglide2x.so )
( cd $PKG/usr/lib ; ln -sf libglide2x.so libglide2x.so.2 )

###
### Write out the Glide2x test program
###
cat glide2x/h3/glide/tests/test00 > $PKG/usr/bin/testGlide2x
( cd $PKG/usr/bin ; ln -sf testGlide2x test3Dfx )

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/{libglide.so.2.60,libglide3.so.3.10,libtexus.so.1.1}

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

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