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

######################################################
# Build dependency list:
# compiletc
# Xorg-7.4-dev and its deps
# qt-4.5-dev
# qt-4.5-base
######################################################
# Configure extension creation parameters            #
######################################################

SRCNAM=bsc_4.1.0_src.tar.bz2
WRKDIR=bsc
EXTNAM=bsc
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
DEFINES="-DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED"
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe $DEFINES"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions $DEFINES"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Unpack source in current directory

tar -xf $SRCNAM

# Configure it

cd $WRKDIR

rm -f Makefile

patch < ../bsc.patch

qmake

# Compile

make -j3 CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS"

# Install in base temp dir

mkdir -p $TMPDIR/usr/local/bin

cp -p bsc $TMPDIR/usr/local/bin

mkdir -p $TMPDIR/usr/local/tce.menu

mkdir -p $TMPDIR/usr/local/tce.icons

mkdir -p $TMPDIR/usr/local/share/applications

mkdir -p $TMPDIR/usr/local/share/pixmaps

mkdir -p $TMPDIR/usr/local/share/doc

cp -p BeesoftCommander.png $TMPDIR/usr/local/share/pixmaps/bsc.png

# Delete compilation work directory

# remove unneeded

cd ..
rm -r -f $WRKDIR

# Adjust directory access rigths

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

# Strip executables

cd $TMPDIR

find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null

# create menu item

cd $TMPDIR/usr/local/tce.menu

cat > bsc << EOF
<JWM>
<Program label="Beesoft Commander">/usr/local/bin/bsc</Program>
</JWM>

EOF

cd $TMPDIR/usr/local/tce.icons

cat > bsc << EOF
i: /usr/local/share/pixmaps/bsc.png
t: Beesoft Commander
c: /usr/local/bin/bsc

EOF

cd $TMPDIR/usr/local/share/applications

cat > bsc.desktop << EOF
[Desktop Entry]
Name=Beesoft Commander
Comment=Run beesoft commander file manager
Exec=bsc
Icon=bsc.png
Terminal=false
Type=Application
Categories=Application;System;Utility;FileManager;

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  