#!/bin/bash
set -xe
######################################################
# Build script for Core 6.0                          #
######################################################

######################################################
# Configure extension creation parameters            #
######################################################
S=$(basename $0)
PACKAGE=${S%%.build}

VERSION=0.9.1
SRCNAM=$PACKAGE-$VERSION.tar.bz2
WRKDIR=$PACKAGE-$VERSION
EXTNAM=$PACKAGE
TMPDIR=/tmp/$PACKAGE-$VERSION
OUTDIR=../

ENTRIES=(fltk qt4 gtk-2 curses)

######################################################
# Prepare extension creation                         #
######################################################
rm -r -f $WRKDIR
sudo rm -r -f $TMPDIR
sudo rm -r -f $TMPDIR-doc
for ENTRY in ${ENTRIES[@]}; do
	sudo rm -Rf $TMPDIR-$ENTRY
done

# 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="-Os -pipe -march=i486 -mtune=i686"
export CXXFLAGS="-Os -pipe -fno-rtti -march=i486 -mtune=i686 -fexceptions -std=c++11"
export LDFLAGS="-Wl,-Os"

# Unpack source in to WRK directory
tar -xf $SRCNAM
cd $WRKDIR
tar -xf ../$PACKAGE-$VERSION-fltk.tar.bz2
patch -p1 < ../$PACKAGE-$VERSION-1-fltk.patch
tar -xf ../$PACKAGE-$VERSION-fltk-configure.tar.bz2
patch -p1 < ../$PACKAGE-$VERSION-2-configure-qt4.patch

# Configure,Compile,Check,Install in base temp dir
PREFIX=/usr/local
./configure --prefix=/usr/local --enable-pinentry-tty --enable-pinentry-gtk2 --enable-pinentry-qt4 --enable-pinentry-fltk --disable-fallback-curses
make
make install DESTDIR=$TMPDIR

# Delete compilation work directory
cd ..
rm -r -f $WRKDIR

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

# Move files to doc extension
mkdir -p $TMPDIR-doc/usr/local/share/
mv $TMPDIR/usr/local/share/info $TMPDIR-doc/usr/local/share

for ENTRY in ${ENTRIES[@]}; do 
	mkdir -p $TMPDIR-$ENTRY/usr/local/bin
	mv $TMPDIR/usr/local/bin/pinentry-$ENTRY $TMPDIR-$ENTRY/usr/local/bin/

	mkdir -p $TMPDIR-$ENTRY/usr/local/tce.installed

	# for TUI package(s) not needed display detecting
        if [ "$ENTRY" == "curses" ]; then
#--- START: TUI implementations not need checking DISPLAY => just symlink to pinentry is enouth
	cat >$TMPDIR-$ENTRY/usr/local/tce.installed/$PACKAGE-$ENTRY <<EOS
#!/bin/sh
which pinentry >/dev/null 2>&1 || ln -s ${PACKAGE}-${ENTRY} /usr/local/bin/pinentry
EOS
#--- END: TUI implementations not need checking DISPLAY => just symlink is enouth
        else
#--- START: GUI implementations check check DISPLAY and if notavalable try use pientry-curses and pinentry-tty
#--- Also they MUST relink to itself
        cat >$TMPDIR-$ENTRY/usr/local/tce.installed/$PACKAGE-$ENTRY <<EOS
#!/bin/sh
rm -f $PREFIX/bin/pinentry
cat >$PREFIX/bin/pinentry <<EOP
#!/bin/sh
test -n "\\\$DISPLAY" && pinentry-$ENTRY || { which pinentry-curses >/dev/null 2>&1 && exec pinentry-curses || exec pinentry-tty; }
EOP
chmod a+rx $PREFIX/bin/pinentry
EOS
#--- END: GUI implementations check check DISPLAY and if notavalable try use pientry-curses and pinentry-tty
        fi
done

unlink $TMPDIR/usr/local/bin/pinentry
# if empty one
rmdir $TMPDIR/usr/local/share || true

###################################################
# Startup script
###################################################
mkdir -p $TMPDIR/usr/local/tce.installed
cat >$TMPDIR/usr/local/tce.installed/$PACKAGE <<EOS
#!/bin/sh
which pinentry >/dev/null 2>&1 || ln -s  pinentry-tty /usr/local/bin/pinentry
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 
unsquashfs -li -ls -d 'root-dir' $OUTDIR/$EXTNAM.tcz | grep 'root-dir/' | grep -vE '^d' | awk '{print substr($6,9);}' >$OUTDIR/$EXTNAM.tcz.list

mksquashfs $TMPDIR-doc $OUTDIR/$EXTNAM-doc.tcz -noappend -no-xattrs -all-root
unsquashfs -li -ls -d 'root-dir' $OUTDIR/$EXTNAM-doc.tcz | grep 'root-dir/' | grep -vE '^d' | awk '{print substr($6,9);}' >$OUTDIR/$EXTNAM-doc.tcz.list

sudo rm -rf $TMPDIR
rm -rf $TMPDIR-doc

pushd $OUTDIR
md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt
md5sum $EXTNAM-doc.tcz > $EXTNAM-doc.tcz.md5.txt
popd

for ENTRY in ${ENTRIES[@]}; do 
	sudo chown -R root:root $TMPDIR-$ENTRY
	sudo chown root:staff $TMPDIR-$ENTRY/usr/local/tce.installed
	sudo chmod 775 $TMPDIR-$ENTRY/usr/local/tce.installed
	sudo chown tc:staff $TMPDIR-$ENTRY/usr/local/tce.installed/*
	sudo chmod 755 $TMPDIR-$ENTRY/usr/local/tce.installed/*
	mksquashfs $TMPDIR-$ENTRY $OUTDIR/$EXTNAM-$ENTRY.tcz -noappend -no-xattrs
	unsquashfs -li -ls -d 'root-dir' $OUTDIR/$EXTNAM-$ENTRY.tcz | grep 'root-dir/' | grep -vE '^d' | awk '{print substr($6,9);}' >$OUTDIR/$EXTNAM-$ENTRY.tcz.list
	sudo rm -rf $TMPDIR-$ENTRY

	pushd $OUTDIR
	md5sum $EXTNAM-$ENTRY.tcz > $EXTNAM-$ENTRY.tcz.md5.txt
	popd
done

###################################################
# Update info/tree/dep
###################################################
size() {
        local MIN=$[9*1024/10] # 799 and less
        local MAX=100  # 3.5 and less
        local PWR=1
        local V=$1
        local M=""
        for F in "" "K" "M" "G"; do
                M=$F
                V1=$[($1*10)/$PWR]
                R=$[$V1%10]
                V=$[$V1/10]
                [ $V1 -le $MAX ] && { [ $R -ne 0 ] && V="$V.$R"; break; }
                [ $V -lt $MIN ] && break;
                PWR=$[$PWR*1024]
        done
        echo "$V$M"
}

cp $EXTNAM.tcz.build-dep $OUTDIR/

for INFO in *.tcz.info; do
        TCZ=${INFO%%.info}
        LEN=$(stat -c%s $OUTDIR/$TCZ)
        SIZE=$(size $LEN)
        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
submitqc --libs --tcz=$EXTNAM-doc.tcz
for ENTRY in ${ENTRIES[@]}; do
	submitqc --libs --tcz=$EXTNAM-$ENTRY.tcz
done
popd
