#!/bin/bash
# Script to make a ripperX .tce extension from source
# Requires the compiletc.tce, gtk1, glib1, id3lib/devs extensions.

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

set -e

NAME=ripperX
VERSION=2.6.7
DOWNLOAD=http://prdownloads.sourceforge.net/ripperx
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=200ea28270528ee5bad6b68c984ca83e
TMPDIR=/usr/local/"$NAME"21654
PKG="$TMPDIR"/pkg
INSTALL="$TMPDIR"/install
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 xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
	cd "$TMPDIR"/"$NAME"-"$VERSION"
	export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
	export LDFLAGS="-L/usr/local/lib"
	export CPPFLAGS="-I/usr/local/include -I/usr/local/include/id3"
	./configure --prefix="$INSTALL"
	make
	make install 
	mv "$INSTALL"/bin/* "$PKG"/usr/local/bin/	


	cd "$PKG"
	find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	
}




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

if [ -e "$TMPDIR" ]; then
  rm -rf "$TMPDIR" 
fi
mkdir -p "$PKG"/usr/local/bin || exit 1
download || exit 1
build_source || exit 1
make_extension || exit 1
echo ""$NAME"-"$VERSION".tce is now in your home directory."