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

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

# extract the package framework
cd $PKG
explodepkg $CWD/_sdk2x.tar.gz

echo "+===============+"
echo "| Glide 2.x SDK |"
echo "+===============+"
cd $TMP
mkdir Glide
cd Glide
tar xvzf $CWD/glide2x.tar.gz
tar xvzf $CWD/swlibs.tar.gz

###
### Write out the header files
###
cat swlibs/fxmisc/3dfx.h > $PKG/usr/include/glide/3dfx.h
cat glide2x/h3/glide/src/glide.h > $PKG/usr/include/glide/glide.h
cat glide2x/h3/glide/src/glidesys.h > $PKG/usr/include/glide/glidesys.h
cat glide2x/h3/glide/src/glideutl.h > $PKG/usr/include/glide/glideutl.h
cat glide2x/h3/incsrc/sst1vid.h > $PKG/usr/include/glide/sst1vid.h
cat glide2x/h3/glide/src/gump.h > $PKG/usr/include/glide/gump.h
cat swlibs/fxmisc/linutil.h > $PKG/usr/include/glide/linutil.h
cat swlibs/texus/lib/texus.h > $PKG/usr/include/glide/texus.h

###
### Write out the test sources
###
cat glide2x/h3/glide/tests/makefile.distrib > $PKG/usr/src/glide/tests/Makefile
cp -a glide2x/h3/glide/tests/*.3df $PKG/usr/src/glide/tests
cp -a glide2x/h3/glide/tests/test??.c $PKG/usr/src/glide/tests
cp -a glide2x/h3/glide/tests/tldata.inc $PKG/usr/src/glide/tests
cp -a glide2x/h3/glide/tests/tlib.[ch] $PKG/usr/src/glide/tests

###
### Write out the Texus sources
###
cat swlibs/texus/makefile.distrib > $PKG/usr/src/glide/texus/Makefile
cat swlibs/texus/lib/makefile.distrib > $PKG/usr/src/glide/texus/lib/Makefile
cat swlibs/texus/cmd/makefile.distrib > $PKG/usr/src/glide/texus/cmd/Makefile
cat swlibs/texus/examples/makefile.distrib > \
   $PKG/usr/src/glide/texus/examples/Makefile
cp -a swlibs/texus/lib/*.c $PKG/usr/src/glide/texus/lib
cat swlibs/texus/lib/texusint.h > $PKG/usr/src/glide/texus/lib/texusint.h
cp -a swlibs/texus/cmd/*.c $PKG/usr/src/glide/texus/cmd
cp -a swlibs/texus/examples/*.c $PKG/usr/src/glide/texus/examples

###
### Write out the documentation
###
( cd $PKG/usr/doc ; tar xvzf $CWD/Glide2-Doc.tar.gz )
cat glide2x/glide_license.txt > $PKG/usr/doc/Glide2/glide_license.txt

# Fix permissions and ownerships
chown -R root.root $PKG

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

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