#!/bin/sh
#
######################################################
# Build script                                       #
#                                                    #
# See .info for details                              #
#                                                    #
######################################################

tce-load -i compiletc squashfs-tools ncurses-dev

######################################################
# Configure extension creation parameters            #
######################################################
SRCPATH="../../../sources"

SRCNAM=vim-9.1.1033.tar.gz
WRKDIR=vim-9.1.1033
EXTNAM=vim
GEXTNAM=gvim
TMPDIR=/tmp/$EXTNAM
GTMPDIR=/tmp/$GEXTNAM

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

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

rm -r -f $TMPDIR
rm -r -f $TMPDIR-doc
rm -r -f $TMPDIR-locale

# Crete temporary directory

mkdir -p $TMPDIR

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

# Export variables needed for compilation

case $(find /lib | grep ld-linux) in
    *armhf*)
       export CFLAGS="-flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
       export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
       BITS="linux32"
    ;;
    *aarch64*)
       export CFLAGS="-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
       export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72"
       BITS=""
    ;;
esac

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Unpack source in current directory

tar xf ${SRCPATH}/${SRCNAM}

# Configure it

cd $WRKDIR

${BITS} ./configure --prefix=/usr/local

# Compile

${BITS} make -j6

# Install in base temp dir

${BITS} make install DESTDIR=$TMPDIR

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# Adjust directory access rigths

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

# Strip executables

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

# Move files to doc extension

mkdir -p $GTMPDIR-doc/usr/local/share/vim/vim91
mv -f $TMPDIR/usr/local/share/man $GTMPDIR-doc/usr/local/share
mv -f $TMPDIR/usr/local/share/vim/vim91/doc  $GTMPDIR-doc/usr/local/share/vim/vim91

# Move base to gvim-base

mkdir -p $GTMPDIR-base/usr/local/share/vim
mv -f $TMPDIR/usr/local/share/applications $GTMPDIR-base/usr/local/share
mv -f $TMPDIR/usr/local/share/icons $GTMPDIR-base/usr/local/share
mv -f $TMPDIR/usr/local/share/vim/vim91 $GTMPDIR-base/usr/local/share/vim

###################################################
# Create base extension in temp dir               #
###################################################

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

# Create md5 file

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

# Cleanup temp directory

rm -r -f usr

###################################################
# Create Gbase extension in temp dir               #
###################################################

cd $GTMPDIR-base
cd ..
mksquashfs $GTMPDIR-base $GEXTNAM-base.tcz
cd $GTMPDIR-base
find usr -not -type d > $GEXTNAM-base.tcz.list
mv ../$GEXTNAM-base.tcz .

# Create md5 file

md5sum $GEXTNAM-base.tcz > $GEXTNAM-base.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

###################################################
# Create doc extension in temp dir                #
###################################################

cd $GTMPDIR-doc
cd ..
mksquashfs $GTMPDIR-doc $GEXTNAM-doc.tcz
cd $GTMPDIR-doc
find usr -not -type d > $GEXTNAM-doc.tcz.list
mv ../$GEXTNAM-doc.tcz .

# Create md5 file

md5sum $GEXTNAM-doc.tcz > $GEXTNAM-doc.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

