#!/bin/sh

TEclass=`pwd`

&>compile.log


check_result() 
{
    if [ -s $1 ] 
    then
        printf "    $2 looks ok\n\n"
    else
        printf "    $2 executable not found\n\n"
    fi 
}


# Compiles libsvm
if [ -s libsvm.tar.gz ]
then 
    tar xzf libsvm.tar.gz &>/dev/null
    cd $TEclass/libsvm*/
    echo Compiling libsvm
    echo -----Compiling libsvm----- 1>>$TEclass/compile.log
    make 2>>$TEclass/compile.log 1>>$TEclass/compile.log
    file="$TEclass/libsvm*/svm-predict" 
    check_result $file 'libsvm'
    cd $TEclass   
fi


# Compiles Glimmer
if [ -s glimmer.tar.gz ]
then
    tar xzf glimmer.tar.gz &>/dev/null
    cd $TEclass/glimmer*/src/
    echo Compiling Glimmer
    echo -----Compiling Glimmer----- 1>>$TEclass/compile.log
    make 2>>$TEclass/compile.log 1>>$TEclass/compile.log
    file="$TEclass/glimmer*/bin/glimmer3"
    check_result $file 'Glimmer'    
    cd $TEclass
fi


# Compiles librf
if [ -s librf.tar.gz ]
then
    tar xzf librf.tar.gz &>/dev/null
    cd $TEclass/librf*/
    echo Configuring librf
    echo -----Configuring librf----- 1>>$TEclass/compile.log
    ./configure 2>>$TEclass/compile.log 1>>$TEclass/compile.log
    echo Compiling librf
    # Adding a few includes to make it compile on current GCC-s
    perl -p -i -e 's/#include <vector>/#include <iostream>\n#include <vector>/' librf/random_forest.h
    perl -p -i -e 's/#include <vector>/#include <cassert>\n#include <vector>/' librf/discrete_dist.h
    perl -p -i -e 's/#include <fstream>/#include <cassert>\n#include <fstream>/' librf/tree_node.h
    perl -p -i -e 's/#include <string>/#include <stdlib.h>\n#include <string>/' librf/instance_set.h
    perl -p -i -e 's/#include <string>/#include <algorithm>\n#include <string>/' librf/instance_set.h
    echo -----Compiling librf----- 1>>$TEclass/compile.log
    make 2>>$TEclass/compile.log 1>>$TEclass/compile.log
    file="$TEclass/librf*/examples/rfpredict"
    check_result $file 'librf'    
    cd $TEclass
fi


# Compiles LVQ_PAK
if [ -s lvq_pak.tar ]
then 
    tar xvf lvq_pak.tar &>/dev/null
    cd $TEclass/lvq_pak*/
    echo Compiling LVQ
    # Fixes to make it compile on current GCC-s 
    # Renaming getline and setprogname functions to lvq_getline and lvq_setprogname 
    # (they are library functions at present)
    perl -p -i -e 's/getline/lvq_getline/g' *
    perl -p -i -e 's/setprogname/lvq_setprogname/g' *
    if [ -s fileio.c.bak ]
    then
        rm *.bak
    fi 
    cp makefile.unix makefile
    echo -----Compiling LVQ----- 1>>$TEclass/compile.log
    make 2>>$TEclass/compile.log 1>>$TEclass/compile.log
    file="$TEclass/lvq_pak*/classify"
    check_result $file 'lvq'   
    cd $TEclass
fi


# Extracts Blast
if [ -s blast.tar.gz ]
then
    echo Extracting Blast
    tar xzf blast.tar.gz &>/dev/null    
    file="$TEclass/blast*/bin/blastclust" 
    check_result $file 'blastclust' 
    cd $TEclass
elif [ -s blast.win64.exe ]
then
    echo Extracting Blast
    mkdir blast-2.2.26
    mv blast.win64.exe blast-2.2.26/
    cd blast-2.2.26/
    chmod +x blast.win64.exe
    ./blast.win64.exe &>/dev/null  
    file="$TEclass/blast*/bin/blastclust.exe" 
    check_result $file 'blastclust' 
    cd $TEclass
fi

printf "\nSee compile.log for details.\n"

