#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
   TMP=/tmp
fi
PKG=$TMP/package-v2-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/_v2-glide.tar.gz

echo "+==================+"
echo "| Glide - Voodoo 2 |"
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
###
# The Voodoo 2 is known as a "cvg"
export FX_GLIDE_HW=cvg
make -C glide3x -f makefile.linux

###
### Glide2x build section
###
# The Voodoo 2 is known as a "cvg"
export FX_GLIDE_HW=cvg
make -C glide2x -f makefile.linux

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

###
### 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/cvg/glide3/tests/test00 > $PKG/usr/bin/testGlide3x

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

###
### Write out the Glide2x test program
###
cat glide2x/cvg/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.53,libglide3.so.3.01,libtexus.so.1.1}

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

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