#!/bin/bash
# Script to make a libpng3,libtiff, and giflib .tcel extension from source
# Requires the compiletc, Xorg-dev, and bash-3.2.tce extension.
# Licenses:
# Libpng: Opensource - http://www.libpng.org/pub/png/src/libpng-LICENSE.txt
# Libtiff: Free - http://www.libtiff.org/misc.html
# Giflib: MIT

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti"
export LC_ALL="C"
set -e

NAME=graphics-libs
VERSION=1
DOWNLOAD=ftp://ftp.simplesystems.org/pub/png/src
DOWNLOAD1=http://dl.maptools.org/dl/libtiff
DOWNLOAD2=ftp://ftp.linux.ee/pub/gentoo/distfiles/distfiles/
SOURCE=libpng-1.2.33.tar.gz
SOURCE1=tiff-3.8.2.tar.gz
SOURCE2=giflib-4.1.6.tar.gz
MD5SUM=a388004b7d7492660c19a2d611a8ca62
MD5SUM1=fbb6f446ea4ed18955e2714934e5b698
MD5SUM2=950943daa71350a558c3edf41c3f0f9f
TMPDIR=/tmp/"$NAME"5168
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list
SRCDIR=/tmp
DIALOG=dialog


build_source() {
tar xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/libpng-1.2.33

./configure --prefix=/usr/local
make
mkdir "$TMPDIR"/png
make DESTDIR="$TMPDIR"/png install
mkdir -p "$PKG"/usr/local/lib
cd "$TMPDIR"/png/usr/local/lib
cp -a libpng.so* "$PKG"/usr/local/lib

cd $TMPDIR
}

build_source1() {
tar xzvf "$SRCDIR"/"$SOURCE1" -C "$TMPDIR"
cd "$TMPDIR"/tiff-3.8.2

./configure --prefix=/usr/local
make
make DESTDIR="$PKG" install
cd "$TMPDIR"
}

build_source2() {
tar xzvf "$SRCDIR"/"$SOURCE2" -C "$TMPDIR"
cd "$TMPDIR"/giflib-4.1.6

./configure --prefix=/usr/local
make
make DESTDIR="$PKG" install
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
cd "$TMPDIR"
}

remove_docs() {
if [ -e "$PKG"/usr/local/man ]; then
rm -r "$PKG"/usr/local/man
fi
if [ -e "$PKG"/usr/local/share/doc ]; then
rm -r "$PKG"/usr/local/share/doc
fi
}

make_extension() {
cd "$PKG"
find . -not -type d > "$LIST"
tar -T "$LIST" -czvf /home/dsl/"$NAME"-"$VERSION".tcel
cd "$TMPDIR"
}

if [ -e "$TMPDIR" ]; then
rm -rf "$TMPDIR"
fi
mkdir -p "$PKG" &&
mkdir -p "$DEV" 
build_source &&
build_source1 &&
build_source2 &&
remove_docs &&
#make_extension &&
echo "Done."