#!/bin/bash
# Script to make a  .tce extension from source


export CFLAGS="-march=i486 -mtune=i686 -O2 -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -O2 -pipe"

set -e

NAME=libmcs
VERSION=0.7.0

SOURCE="$NAME"-"$VERSION".tgz
TMPDIR=/usr/local/"$NAME"21654
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list

SRCDIR=/tmp





build_source() {
	tar xzvf "$SOURCE" -C "$TMPDIR"
	cd "$TMPDIR"/"$NAME"-"$VERSION"
	export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
	./configure --prefix=/usr/local 
		
	make
	make install DESTDIR="$PKG"
	
	cd "$PKG"
	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
	# At this point manually divide package and dev files.
}




make_extension() {
	cd "$PKG"
	find . -not -type d > "$LIST"
	tar -T "$LIST" -czvf /home/tc/"$NAME"-"$VERSION".tcel
	cd "$DEV"
	find . -not -type d > "$LIST1"
	tar -T "$LIST1" -czvf /home/tc/"$NAME"-"$VERSION"-devs.tce
}

if [ -e "$TMPDIR" ]; then
  rm -rf "$TMPDIR" 
fi
mkdir -p "$PKG" || exit 1
mkdir -p "$DEV"/usr/local/lib
build_source || exit 1
#make_extension || exit 1
echo "Done."