#!/bin/bash
# Script to make a openssl .tcel extension.  Requires compiletc.tcel and perl_xml.tce.

export CFLAGS="-march=i686 -02 -pipe"
export CXXFLAGS="-march=i686 -02 -pipe"
export LANG="C"
export LANGUAGE="us"


NAME=openssl
VERSION=0.9.8h
DOWNLOAD=http://www.openssl.org/source
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=7d3d41dafc76cf2fcb5559963b5783b3
TMPDIR=/tmp/"$NAME"6548
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list
LIST1="$TMPDIR"/"$NAME"1.list
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 xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"
./config --openssldir=/usr/local/etc/ssl --prefix=/usr/local shared
make MANDIR=/usr/local/share/man
make INSTALL_PREFIX="$PKG" MANDIR=/usr/local/share/man install
rm -r "$PKG"/usr/local/share/man
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
}

split_extension() {
mkdir -p "$DEV"/usr/local/lib
mv "$PKG"/usr/local/include "$DEV"/usr/local/
mv "$PKG"/usr/local/lib/*.a "$DEV"/usr/local/lib/
mv "$PKG"/usr/local/lib/pkgconfig "$DEV"/usr/local/lib/
}

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.tcel
}

if [ -e "$TMPDIR" ]; then
rm -rf "$TMPDIR" 
fi
mkdir -p "$PKG" || exit 1
mkdir -p "$DEV" || exit 1
download || exit 1
build_source || exit 1
split_extension || exit 1
make_extension || exit 1
echo "Done."