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


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

set -e

NAME=lesstif
VERSION=0.95.0

SOURCE="$NAME"-"$VERSION".tar.bz2
TMPDIR=/usr/local/"$NAME"21654
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list
LIST1="$TMPDIR"/"$NAME".list1
SRCDIR=/tmp
DIALOG=dialog

download() {
	cd "$SRCDIR"
	if [ -e "$SOURCE" ]; then
	  if [ $(md5sum "$SOURCE" | cut -c1-32) != "$MD5SUM" ]; then
	    rm "$SOURCE"
	  fi
	fi
	if [ ! -e "$SOURCE" ]; then
	  wget "$DOWNLOAD"/"$SOURCE"
	fi
	if [ $(md5sum "$SOURCE" | cut -c1-32) = "$MD5SUM" ]; then
	  echo "md5sum passed."
	else
	  echo "Download failed. aborting"
	  exit 1;
	fi
}


build_source() {
	tar xjvf "$SOURCE" -C "$TMPDIR"
	cd "$TMPDIR"/"$NAME"-"$VERSION"
	./configure --prefix=/usr/local \
		--enable-shared \
		--enable-static \
		--enable-build-21 \
		--enable-build-Xlt \
		--enable-build-Xbae \
		--enable-xbae-24bit \
		--enable-xbae-row-heights \
		--disable-debug 
	make
	make install DESTDIR="$PKG"
	rm -r "$PKG"/usr/local/man
	rm -r "$PKG"/usr/local/LessTif
	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
	mv "$PKG"/usr/local/bin "$DEV"/usr/local/
	mv "$PKG"/usr/local/include "$DEV"/usr/local/
	mv "$PKG"/usr/local/lib/*.a "$DEV"/usr/local/lib/
	mv "$PKG"/usr/local/lib/*.la "$DEV"/usr/local/lib/
}




make_extension() {
	cd "$PKG"
	find . -not -type d > "$LIST"
	tar -T "$LIST" -czvf /home/tc/"$NAME".tcel
	cd "$DEV"
	find . -not -type d > "$LIST1"
	tar -T "$LIST1" -czvf /home/tc/"$NAME"-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 ""$NAME".tce is now in your home directory."