#!/bin/bash
# Script to make a tcp-wrapper .tcel extension from source



NAME=tcp_wrappers
VERSION=7.6
DOWNLOAD=http://files.ichilton.co.uk/nfs
DOWNLOAD1=http://www.linuxfromscratch.org/patches/blfs/svn
DOWNLOAD2=http://svn.exactcode.de/t2/trunk/package/network/tcp_wrappers
SOURCE="$NAME"_"$VERSION".tar.gz
SOURCE1=tcp_wrappers-7.6-shared_lib_plus_plus-1.patch
SOURCE2=glibc-2.3.4.patch
MD5SUM=e6fa25f71226d090f34de3f6b122fb5a 
MD5SUM1=7262407ace12078824f0f64e60615e40
MD5SUM2=73dd958873ee00dfac52e730507f6ae8
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
}

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 "md5sum passed."
		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 "md5sum passed."
		else
			echo "Download failed. aborting"
			exit 1;
		fi
}


build_source() {
	tar xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
	cd "$TMPDIR"/"$NAME"_"$VERSION"
	
	export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
	patch -Np1 -i "$SRCDIR"/"$SOURCE1"
	patch -Np0 -i "$SRCDIR"/"$SOURCE2"
	sed -i -e "s,^extern char \*malloc();,/* & */," scaffold.c
	
	make REAL_DAEMON_DIR=/usr/local/sbin linux
	
	cp libwrap.a "$DEV"/usr/local/lib/
	cp shared/libwrap.so* "$PKG"/usr/local/lib/
	cp tcpd.h "$DEV"/usr/local/include/
	cp safe_finger "$PKG"/usr/local/sbin/
	cp tcpd "$PKG"/usr/local/sbin/
	cp tcpdchk "$PKG"/usr/local/sbin/
	cp tcpdmatch "$PKG"/usr/local/sbin/
	cp try-from "$PKG"/usr/local/sbin/
	chmod 755 "$PKG"/usr/local/sbin/*
	echo "# hosts.allow   This file describes the names of the hosts which are allowed access by the '/usr/local/bin/tcpd' server." > "$PKG"/etc/hosts.allow
	echo "# hosts.deny   This file describes the names of the hosts which are denied access by the '/usr/local/bin/tcpd' server." > "$PKG"/etc/hosts.deny
	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
}



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 
}


#############Main############

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


if [ -e "$TMPDIR" ]; then
	rm -r "$TMPDIR"
fi

mkdir -p "$PKG"/etc || exit 1
mkdir -p "$PKG"/usr/local/{lib,sbin} || exit 1
mkdir -p "$DEV"/usr/local/{include,lib} || exit 1
download || exit 1
download1 || exit 1
download2 || exit 1
build_source || exit 1
make_extension && exit 1
ZZho "Done."