#!/bin/sh
#
######################################################
# Build script for TinyCore 2.x                     					  #
#                                                    						  	  #
# See .info for details                              					  #
#                                                    							  #
# December 13, 2009                                   					  #
######################################################
# Build dependency list
# qt-4.5-dev
# aiksaurus
# libiconv
# python
######################################################
# Configure extension creation parameters           				  #
######################################################

SRCNAM=lyx-1.6.5.tar.bz2
WRKDIR=lyx-1.6.5
EXTNAM=lyx
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
./configure --prefix=/usr/local --disable-nls --with-libiconv-prefix=/usr/local --with-qt4 dir=/usr/local \
--enable-threads=posix --enable-build-type=rel --with-gnu-ld \
--with-included-gettext --without-pspell --with-x --without-aspell

# Compile

make -j3

# Install in base temp dir

make install DESTDIR=$TMPDIR

# Delete compilation work directory

cp -p lib/images/lyx.png $TMPDIR/usr/local

cd ..
rm -r -f $WRKDIR

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
mv $TMPDIR/usr/local/lyx.png $TMPDIR/usr/local/share/pixmaps/lyx.png

# remove unneeded files

cd $TMPDIR/usr/local/share/lyx

rm -rf fonts

# 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 > lyx << EOF
<JWM>
<Program label="LyX">/usr/local/bin/lyx</Program>
</JWM>

EOF

cd $TMPDIR/usr/local/tce.icons

cat > "$EXTNAM" << EOF
i: /usr/local/share/pixmaps/lyx.png
t: LyX
c: /usr/local/bin/lyx

EOF

cd $TMPDIR/usr/local/share/applications

cat > lyx.desktop << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=LyX Document Processor
GenericName=Document Processor
Comment=High level LaTeX frontend
Exec=lyx %F
TryExec=lyx
Icon=lyx
StartupNotify=false
StartupWMClass=Lyx
Terminal=false
Categories=Office;WordProcessor;Qt;
MimeType=text/x-lyx;application/x-lyx;

EOF


###################################################
# Create lyx 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  