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

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

###
### Glide2x build section
###
# The Voodoo Graphics is known as an "sst1"
export FX_GLIDE_HW=sst1
make -C glide2x -f makefile.linux

###
### Write out the Glide2x libraries
###
cat glide2x/sst1/lib/libglide.so.2.46 > $PKG/usr/lib/libglide.so.2.46
( cd $PKG/usr/lib ; ln -sf libglide.so.2.46 libglide.so.2 )
( cd $PKG/usr/lib ; ln -sf libglide.so.2 libglide.so )
( cd $PKG/usr/lib ; ln -sf libglide.so.2.46 libglide2x.so.2 )
( cd $PKG/usr/lib ; ln -sf libglide2x.so.2 libglide2x.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 file
###
cat glide2x/glide_license.txt > $PKG/usr/doc/Glide2/glide_license.txt

###
### Write out the test program
###
cat glide2x/sst1/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.46,libtexus.so.1.1}

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

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