#!/bin/bash
set -xe
######################################################
# Configure extension creation parameters            #
######################################################
S=$(basename $0)
PACKAGE=${S%%.build}

VERSION=0.7.2
SRCNAM=$PACKAGE-$VERSION.tar.bz2
WRKDIR=$PACKAGE-$VERSION
EXTNAM=$PACKAGE
TMPDIR=/tmp/$PACKAGE-$VERSION
OUTDIR=$PWD/../../build/$(version)
SRCDIR=$PWD

######################################################
# Prepare extension creation
######################################################

# Remove dirs and files left from previous creation
rm -r -f $WRKDIR
rm -r -f $TMPDIR

# Crete temporary directory
mkdir -p $TMPDIR

######################################################
# Compile extension
######################################################

# Export variables needed for compilation
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
export CPPFLAGS="-I/usr/local/include -I/usr/include"
export CFLAGS="-O2 -march=i486 -mtune=i486"
export CXXFLAGS="-O2 -fno-exceptions -fno-rtti -march=i486 -mtune=i486"
export LDFLAGS="-Wl,-O2"

# Unpack source in to WRK directory
tar -xf $SRCNAM
pushd $WRKDIR
patch -p1 -bz .qf-inline < $SRCDIR/qf-inline.patch
patch -p0 -bz .qf-x11-fullscreen < $SRCDIR/qf-x11-fullscreen.patch

#  --disable-flac  --disable-vorbis
#  --with-servers=master,nq,qw,qtv
#  --disable-sdl --disable-sdl-audio --disable-sdl-cd \
./configure --prefix=/usr/local  --disable-static --disable-debug --without-tools \
  --disable-vidmode --disable-dga \
  --disable-sun --disable-jack \
  --with-x --with-clients=x11,sdl \
  --with-global-cfg=/usr/local/etc/quakeforge.conf \
  --with-user-cfg=~/.quakeforge/quakeforge.conf \
  --with-userpath=~/.quakeforge \
  --with-ipv6 \
  --with-plugin-path=/usr/local/lib/qf-plugins

make

######################################################
# install to $TMPDIR
######################################################
make install DESTDIR=$TMPDIR

mkdir -p $TMPDIR/usr/local/share/doc/$PACKAGE
cp COPYING NEWS $TMPDIR/usr/local/share/doc/$PACKAGE/
sed -rne 's/Author: */  - /p' < ChangeLog | sed -re 's/<.*>//' | sort | uniq >$TMPDIR/usr/local/share/doc/$PACKAGE/AUTHORS

install -Dm644 $SRCDIR/quakeforge-x11.desktop $TMPDIR/usr/local/share/applications/quakeforge-x11.desktop
install -Dm644 $SRCDIR/quakeforge-x11.png $TMPDIR/usr/local/share/pixmaps/quakeforge-x11.png
install -Dm644 $SRCDIR/menu.dat $TMPDIR/usr/local/share/quakeforge/id1/menu.dat
install -Dm644 $SRCDIR/menu.plist $TMPDIR/usr/local/share/quakeforge/id1/menu.plist
install -Dm644 $SRCDIR/quakeforge.conf $TMPDIR/usr/local/etc/quakeforge.conf

# Delete compilation work directory
popd

rm -r -f $WRKDIR

# Strip executables
find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

######################################################
# Clear unusefull files
######################################################
#remove unused file(s)
rm -Rf $TMPDIR/usr/local/include
rm -Rf $TMPDIR/usr/local/lib/pkgconfig
rm -f $TMPDIR/usr/local/lib/*.a
rm -f $TMPDIR/usr/local/lib/*.la
rm -f $TMPDIR/usr/local/lib/qf-plugins/*.a
rm -f $TMPDIR/usr/local/lib/qf-plugins/*.la

###################################################
# Startup script
###################################################
mkdir -p $TMPDIR/usr/local/tce.installed
cat >$TMPDIR/usr/local/tce.installed/$PACKAGE <<EOS
#!/bin/sh
for PACK in /usr/local/libexec/quake/id1/*.pak; do
  ln -s "\$PACK" /usr/local/share/quakeforge/id1/
done
EOS

###################################################
# Create extensions
###################################################
sudo chown -R root:root $TMPDIR
sudo chown root:staff $TMPDIR/usr/local/tce.installed
sudo chmod 775 $TMPDIR/usr/local/tce.installed
sudo chown tc:staff $TMPDIR/usr/local/tce.installed/*
sudo chmod 755 $TMPDIR/usr/local/tce.installed/*

mksquashfs $TMPDIR $OUTDIR/$EXTNAM.tcz -noappend -no-xattrs

sudo rm -rf $TMPDIR

###################################################                              
# Update info/tree/dep/build-dep
cp $EXTNAM.tcz.build-dep $OUTDIR/  
for INFO in *.tcz.info; do                      
        TCZ=${INFO%%.info}                                      
        SIZE=$(ls -sh "$OUTDIR/$TCZ" | cut -f1 -d' ')                                 
        sed -re "s/\{SIZE\}/$SIZE/g" -e "s/\{TITLE\}/$TCZ/g" -e "s/\{VERSION\}/$VERSION/g" <$INFO >$OUTDIR/$INFO
        if [ -f $TCZ.tree ]; then
                cp $TCZ.tree $OUTDIR/$TCZ.tree          
                cat $TCZ.tree | grep -wv "$TCZ" | sed -re 's/^[ \t]+//' | sort | uniq > $OUTDIR/$TCZ.dep
        fi                                                                        
done

###################################################                              
pushd $OUTDIR
submitqc --libs --tcz=$EXTNAM.tcz
popd
