#!/bin/sh
#
######################################################
# Build script for TinyCore 2.x                      #
#                                                    #
# See .info for details                              #
#                                                    #
# December 3, 2009                                   #
######################################################

######################################################
# Build dependency list:
# compiletc
# Xorg-dev
# qt-4.5-dev
# SDL-dev
# cmake
# fpc (freepascal compiler)
######################################################
# Configure extension creation parameters            #
######################################################

SRCNAM=hedgewars-src-0.9.12.tar.bz2
WRKDIR=hedgewars-src-0.9.12
EXTNAM=hedgewars
TMPDIR=/tmp/package
EXTYPE=""
######################################################
# Prepare extension creation                         #
######################################################

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

rm -r -f $TMPDIR

# Create temporary directory

mkdir -p $TMPDIR

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

# Export variables needed for compilation

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Unpack source in current directory

tar -xf $SRCNAM

# Configure it

cd $WRKDIR

cmake . -DCMAKE_BUILD_TYPE="Release" -DCMAKE_INSTALL_PREFIX="/usr/local"

# Compile

make -j3

# Install in base temp dir

make install DESTDIR=$TMPDIR

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

cp -p hedgewars.png $TMPDIR/usr/local

# Remove unneeded dirs and files

# Adjust directory access rigths

find $TMPDIR/ -type d | xargs chmod -v 755;

# Strip executables

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

# create menu item
cd $TMPDIR
mkdir -p usr/local/tce.menu
cd usr/local/tce.menu

cat > hedgewars << EOF
<JWM>
<Program label="Hedgewars">/usr/local/bin/hedgewars</Program>
</JWM>

EOF

cd $TMPDIR/usr/local

mkdir tce.icons

cd tce.icons

mv ../hedgewars.png .

cat > hedgewars << EOF
i: /usr/local/tce.icons/hedgewars.png
t: Hedgewars
c: /usr/local/bin/hedgewars

EOF

###################################################
# Create extension in TMPDIR                      #
###################################################

cd $TMPDIR/..
mksquashfs $TMPDIR/ $EXTNAM.tcz$EXTYPE
cd $TMPDIR
find usr -not -type d > $EXTNAM.tcz$EXTYPE.list
mv ../$EXTNAM.tcz$EXTYPE .

# Create md5 file

md5sum $EXTNAM.tcz$EXTYPE > $EXTNAM.tcz$EXTYPE.md5.txt

# Cleanup temp directory

rm -r -f usr  