#!/bin/bash
# Script to make a libpng3,libtiff, and giflib .tcel extension from source
# Requires the compiletc.dsl, X_headers.dsl, 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

set -e
CFLAGS="-march=i686 -02 -pipe"
CXXFLAGS="-march=i686 -02 -pipe"

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.29.tar.gz
SOURCE1=tiff-3.8.2.tar.gz
SOURCE2=giflib-4.1.4.tar.gz
MD5SUM=a388004b7d7492660c19a2d611a8ca62
MD5SUM1=fbb6f446ea4ed18955e2714934e5b698
MD5SUM2=950943daa71350a558c3edf41c3f0f9f
TMPDIR=/tmp/"$NAME"
PKG="$TMPDIR"/pkg
LIST="$TMPDIR"/"$NAME".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 "libpng-1.2.29.tar.gz md5sum passed."
sleep 2
else
echo "Download failed. aborting"
exit 1;
fi
}

download1() {
cd "$SRCDIR"
if [ -e "$SOURCE1" ]; then
if [ $(md5sum "$SOURCE1" | cut -c1-32) != "$MD5SUM1" ]; then
rm "$SOURCE1"
fi
fi
if [ ! -e "$SOURCE1" ]; then
wget "$DOWNLOAD1"/"$SOURCE1"
fi

if [ $(md5sum "$SOURCE1" | cut -c1-32) = "$MD5SUM1" ]; then
echo "tiff-3.8.2.tar.gz md5sum passed."
sleep 2
else
echo "Download failed. aborting"
exit 1;
fi
}

download2() {
cd "$SRCDIR"
if [ -e "$SOURCE2" ]; then
if [ $(md5sum "$SOURCE2" | cut -c1-32) != "$MD5SUM2" ]; then
rm "$SOURCE2"
fi
fi
if [ ! -e "$SOURCE2" ]; then
wget "$DOWNLOAD2"/"$SOURCE2"
fi

if [ $(md5sum "$SOURCE2" | cut -c1-32) = "$MD5SUM2" ]; then
echo "giflib-4.1.4.tar.gz md5sum passed."
sleep 2
else
echo "Download failed. aborting"
exit 1;
fi
}

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

./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
cp -a libpng.so.3 "$PKG"/usr/local/lib
cp -a libpng.so.3.29.0 "$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.4

./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" &&
download &&
download1 &&
download2 &&
build_source &&
build_source1 &&
build_source2 &&
remove_docs &&
make_extension &&
echo ""$NAME"-"$VERSION".tcel is now in your home directory."