#!/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"
export LC_ALL="C"
set -e

NAME=ntfs-3g
VERSION=1.5012
SOURCE="$NAME"-"$VERSION".tgz

TMPDIR=/usr/local/"$NAME"21654
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list
LIST1="$TMPDIR"/"$NAME".list1
SRCDIR=/tmp




build_source() {
tar xjvf "$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig

./configure --prefix=/usr/local || exit 1
make || exit 1
make DESTDIR="$PKG" install || exit 1
[ -d "$PKG"/usr/local/share ] && rm -r "$PKG"/usr/local/share
[ -d "$PKG"/usr/local/man ] && rm -r "$PKG"/usr/local/man
cd "$PKG"
find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || exit 1
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null || exit 1

## Configure options for these do not work correctly, so done manually.
mv "$PKG"/lib/* "$PKG"/usr/local/lib/
mv "$PKG"/bin/* "$PKG"/usr/local/bin/
mkdir "$PKG"/usr/local/sbin
mv "$PKG"/sbin/* "$PKG"/usr/local/sbin/
rm -r "$PKG"/lib

mv "$PKG"/usr/local/lib/*.a "$DEV"/usr/local/lib/
mv "$PKG"/usr/local/lib/*.la "$DEV"/usr/local/lib/
mv "$PKG"/usr/local/lib/pkgconfig "$DEV"/usr/local/lib/
mv "$PKG"/usr/local/include "$DEV"/usr/local/
}




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 || exit 1

build_source || exit 1
make_extension || exit 1
echo ""$NAME"-"$VERSION".tcel is now in your home directory."