#!/bin/bash
# Script to make a cmake .tce extension from source
# Requires the compiletc.tce,s extensions.

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

set -e

NAME=cmake
VERSION=2.6.3
DOWNLOAD=http://www.cmake.org/files/v2.6
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=6717f4bc3cfcced4172ad98660ce7599
TMPDIR=/usr/local/"$NAME"21654
PKG="$TMPDIR"/pkg
LIST="$TMPDIR"/"$NAME".list
SRCDIR=/tmp


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 "$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
./configure --prefix=/usr/local 
make
make DESTDIR="$PKG" install
rm -r "$PKG"/usr/local/{man,doc}
}




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" || exit 1
#download || exit 1
build_source || exit 1
make_extension || exit 1
echo "Done."