#!/bin/bash 
#
# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available at
# <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
# license, available in the LICENSE.TXT file accompanying this
# software.  These details are also available at
# <http://openib.org/license.html>.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
#

#cd ${0%*/*}
# pointing to the dir where this install script resides
PACKAGE_ORIG_DIR=`cd ${0%*/*};pwd`

# we modify the srcs so we need to 
PACKAGE_DIR=/tmp/mft_install_tmp
if [ -d $PACKAGE_DIR ]; then
    rm -rf $PACKAGE_DIR
fi
mkdir -p $PACKAGE_DIR

# copy over the original dir
cp -ap $PACKAGE_ORIG_DIR/* $PACKAGE_DIR/

# go there
cd $PACKAGE_DIR

MFT_PATCH_COOKY="Added by MFT install.sh"

INSTALL_DATE=`date`

#
# INFO - installation info file.
# Created by the install script. 
# Used by the uninstall script to remove the data.
#
# This file contains the following data (in "var=value" lines)
#
# INFO_MFTHOME  - Build tree path (directory to delete)
# INFO_FINAL_PREFIX - Remove soft links from this dir
# INFO_INSTALL_DATE - Installation date to check against the INSTALL_DATE file 
#                     in MFTHOME

INFO=/etc/mft/info

########################################################################
#
# FUNCTIONS:
#

function cleanup_mft_files() {
    # Clean old distribution
    binApps="mlxburn aimage mic spark flint t2a uninstall_mft.sh"

    local logFile="$1"
    
    echo "  Removing Executables from : ........ ${CONFIG_ROOT}/bin."
    for f in $binApps; do
        rm -f ${CONFIG_ROOT}/bin/$f 2>&1 > /dev/null;
        if [ $? == 0 ]; then
            echo "Removed : ${CONFIG_ROOT}/bin/$f" >> $logFile
        fi
    done

    echo "  Removing MFT Man Pages from : ...... /usr/share/man/man1."
    mans="mlxburn"
    for f in $mans; do
        rm -r /usr/share/man/man1/$f.1.* 2>&1 > /dev/null;
        if [ $? == 0 ]; then
            echo "Removed : " /usr/share/man/man1/$f.1.* >> $logFile
        fi
    done

    libs=${CONFIG_ROOT}/lib/mft
    echo "  Removing MFT libs from : ........... $libs"
    safe_rm_rf $libs 2>&1 > /dev/null;
    if [ $? == 0 ]; then
        echo "Removed : " $libs >> $logFile
    fi  

    echo "  Removing MFT directory: ............ $MFTHOME"
    safe_rm_rf $MFTHOME 2>&1 > /dev/null;
    if [ $? == 0 ]; then
        echo "Removed : " $MFTHOME >> $logFile
    fi

    echo "  Removing installation info file: ... $INFO"
    rm -f $INFO
    if [ $? == 0 ]; then
        echo "Removed : " $INFO >> $logFile
    fi   
}


#Report: if config failed
function config_failed() {
	echo "Config failed ! (see $CFGLOG for details)"
   cleanup
	exit 1
}

#Report: if make failed
function make_failed() {
	echo "Make failed ! (see $MAKELOG for details)"
   cleanup
	exit 1
}

#Report: if tcl failed
function tcl_failed() {
	echo "Making Internal Tcl failed ! (see $TCLMAKELOG for details)"
   cleanup
	exit 1
}



#Check if MST libmtcr.a is pre-installed - if not give an error and exit
function check_mst() {

    if test ! -f $MTCR_LIB; then
        echo "MFT Depends on pre-installed MST. Fail to find $MTCR_LIB "
        cleanup
        exit 1
    fi
}

SUBDIRS="ibfw tools mstdrv"
# cleanup
function cleanup() {
    rm -rf $PACKAGE_DIR
    for subdir in $SUBDIRS; do
        rm -rf $MFTHOME/$subdir
    done
}

function build_tcl() {
    cd $MFTHOME	
    TCL_PREFIX=$CONFIG_ROOT/lib/mft/tcl
    tar xfz ${PACKAGE_DIR}/tcl.tar.gz >> $TCLMAKELOG 2>&1
    find tcl8.4.9 -exec touch {} \;
    cd tcl*/unix
    # NOTE we used TCL shared lib to avoid issues with loading libs (on IA64)

    #echo "-D- skipping tcl build !!! REMOVE !!!"; return
    
    ./configure --prefix=$TCL_PREFIX $tcl_flag64 --enable-debug --enable-threads >> $TCLMAKELOG 2>&1
    make LD=g++ >> $TCLMAKELOG 2>&1
    if [ $? != 0 ]; then tcl_failed ; fi
    make install>> $TCLMAKELOG 2>&1
    if [ $? != 0 ]; then tcl_failed ; fi
    # we will use this tclsh in the underlying config and build ...
    export TCLSH=$TCL_PREFIX/bin/tclsh8.4

}

function install_mst_drv() {

    echo "  Installing MST driver under $MST_HOME ..."
    drv_inst=$PACKAGE_DIR/mst-driver/mst-`uname -m`-*/drvinstall
    if test ! -x $drv_inst; then
        echo "-E- Can't find MST installation under $drv_inst. Bad installation TAR or platform is not supported."
        cleanup
        exit 1

    fi

    echo "  Runing $drv_inst ..."
    $drv_inst >  $MSTMAKELOG 2>&1

    if [ $? != 0 ]; then 
        echo "-E- MST driver installation failed! (see $MSTMAKELOG for details)"
        cleanup
        exit 1
    fi
}


#Install MST
function install_mst() {
    # Install MST
    if test -f ${PACKAGE_DIR}/mst-`uname -m`-*.tgz; then
        echo "  Installing MST package under $MST_HOME ..."
        tar xfz ${PACKAGE_DIR}/mst-`uname -m`-*.tgz
        ./mst-`uname -m`*/mst_install > $MSTMAKELOG 2>&1 <<EOF


EOF
        if test $? != 0; then
            echo "-E- MST Installation failed! See $MSTMAKELOG for details"
            exit 1
        fi
    else
        echo "-E- Can't find MST installation. Bad installation TAR or platform (`uname -m`) is not supported."
        cleanup
        exit 1
    fi
}


function safe_rm_rf() {
    dir=$1
    case ${dir//\//} in
	usr|usrlocal|opt|bin|etc|lib|lib64|local|misc|sbin|var|boot|root|""|tmp)
	echo "-E- Tried to remove directory $dir. Deletion aborted. Make sure the right destination paths were given"
	return 1
	;;
        *)
        /bin/rm -rf ${dir}
        ;;
    esac

    return 0
}


# Argument: the yes/no question to ask
# Returns:  1 if user answered y|yes|YES ... or if FORCE_INSTALLATION
#           0 if user answered n|NO|...
function ask_user() {
    echo -n "$1 :(y/n) [n] "
    if [ $FORCE_INST != 0 ]; then 
        ans="y"
	echo $ans
    else
        read ans
    fi

    if    [ "x$ans" == "xy"   ]  \
       || [ "x$ans" == "xyes" ]  \
       || [ "x$ans" == "xY"   ]  \
       || [ "x$ans" == "xYES" ]  ; then
        ret=1
    else
        ret=0
    fi

    return $ret  
}



check_prev_install()
{
    # Check prefix and kernel version using info file:
    if [ -f ${INFO} ]; then
        # source the info file - it defines the INFO_* variables.
        . $INFO

        if [ ! -z $INFO_FINAL_PREFIX ] && [ -x $INFO_FINAL_PREFIX/uninstall_mft.sh ]; then
            uninst=$INFO_FINAL_PREFIX/uninstall_mft.sh
        elif [ ! -z $INFO_MFTHOME ]     && [ -x $INFO_MFTHOME/uninstall.sh ]; then
            uninst=$INFO_MFTHOME/uninstall.sh
        else
            echo "-E- Corrupted installation info file ($INFO) found. Probably left from previous partial installation. Removing."
            rm -rf $INFO
	    uninst=$INFO
        fi
        
        if [ -x $uninst ]; then
            OLD_BLD_ID="<unknown>"
            if [ -f $MFTHOME/BUILD_ID ]; then
                OLD_BLD_ID=`cat $MFTHOME/BUILD_ID`
            fi

	    echo "  Found a previous installation of the MFT package."
            echo "  Current installed MFT Build ID is $OLD_BLD_ID"
            echo "  This installation MFT Build ID is $BLD_ID"
	    echo ""
	
	    ask_user "  Remove currently installed components (run $uninst) ? " 
            if [ $? -ne 0 ]; then
                echo "  Running $uninst ... " | tee -a $CFGLOG
                echo " ----------------------------------------- " >> $CFGLOG
                $uninst --force            >> $CFGLOG           
                echo " ----------------------------------------- " >> $CFGLOG
                if [ $? -ne 0 ] ; then
                   echo "  Uninstall Failed !"                     | tee -a $CFGLOG   
                else
                   echo "  Uninstall completed successfully."      | tee -a $CFGLOG  
                fi
	    else
	        echo Aborting		
                rm -rf $PACKAGE_DIR
		exit 1 
            fi
        fi
    fi

    # Check for previous installations remaining in current prefix after INFO based cleanup
    if [ -n "$MFTHOME" ] ; then
        if [ -d $MFTHOME ] ; then
            OLD_BLD_ID="<unknown>"
            if [ -f $MFTHOME/BUILD_ID ]; then
                OLD_BLD_ID=`cat $MFTHOME/BUILD_ID`
            else 
                echo "-E- Previous instalation directory ($MFTHOME) does not contain a BUILD_ID file."
                echo "    Make sure the right build_root directory is given, or manualy remove $MFTHOME directory"
                exit 1
            fi
            # Don't remove MFTHOME in case of RPM build because: MFTHOME == STACK_PREFIX
            if [ $BUILD_ONLY == 0 ]; then 
                echo "  Found traces of another Mellanox software installation at $MFTHOME."
                echo "  Current installed MFT Build ID is $OLD_BLD_ID"
                echo "  This installation MFT Build ID is $BLD_ID"
		ask_user "  Remove currently installed components (rm -rf $MFTHOME) ?"
                if [ $? -ne 0 ]; then
                    # Clean old distribution
                    safe_rm_rf $MFTHOME
                    cleanup_mft_files  $CFGLOG
                else
		    echo Aborting
                    rm -rf $PACKAGE_DIR
                    exit 1
		fi
            fi
        fi
    else
        # no MFTHOME but cleanup anyway - what if someone deleted it?
        if [ $BUILD_ONLY == 0 ]; then 
            cleanup_mft_files $CFGLOG
        fi
    fi
}

# Check existance and version of given/detected tcl shell
# set the local_tcl var.

function check_tcl() {
    if [ -z $use_tclsh ]; then
	local_tcl=`which tclsh`
    else
        local_tcl=$use_tclsh
    fi
    
    if [ ! -x $local_tcl ] ; then
	echo "-E- $use_tcl tcl shell not found. Can't use local tcl. You can re-install without the --with_tclsh flag."
	cleanup
	exit 1
    fi

    # check tcl version. require 8.4 or higher.
    tcl_ver=`echo 'puts $tcl_version' | $local_tcl`
    tcl_ver_maj=`echo $tcl_ver | sed 's/\..*$//'`
    tcl_ver_min=`echo $tcl_ver | sed 's/^.*\.//'`
    
    if [ "$tcl_ver_maj" -ge 8 ] && [ "$tcl_ver_min" -ge 4 ]; then
	# TCL OK
	local_tcl=$local_tcl
    else 
	echo "-E- Can't use existing tclsh - Bad tclsh version. Required: 8.4 of higher. Detected (for $local_tcl): $tcl_ver."
	exit 1
    fi
}





function configure_ibspark() {
   if [ `uname -i` == "x86_64" ]; then
        ARCH="64";
   elif [ `uname -i` == "ppc64" ]; then
        ARCH="64";
   else
        ARCH="";
    fi
    
    msg="Make sure that the OFED driver is installed correctly. Use the --ib-prefix flag to specify a non-standard OFED installation path"

    echo Looking for dependencies...
    if [ "$IB_PREFIX" != "" ]; then
        export IB_LIB=${IB_PREFIX}/lib${ARCH};
        export IB_INC=${IB_PREFIX}/include;
    else
        if [ -e /etc/infiniband/info ]; then
            #ofed
            IB_PREFIX=`/etc/infiniband/info | grep "prefix=" | cut -d = -f 2`;
        elif [ -e /usr/local/ofed/lib${ARCH}/libibmad.so ]; then
            IB_PREFIX=/usr/local/ofed/;
        elif [ -e /usr/local/lib${ARCH}/libibmad.so ]; then
            IB_PREFIX=/usr/local/;
        elif [ -e /usr/lib${ARCH}/libibmad.so ]; then
            IB_PREFIX=/usr/;
        elif [ -e /usr/ofed/lib${ARCH}/libibmad.so ]; then
            IB_PREFIX=/usr/ofed/;
        else
            echo "-E- shared library libibmad.so . $msg"
            return 1;
        fi

        export IB_LIB=${IB_PREFIX}/lib${ARCH};
        export IB_INC=${IB_PREFIX}/include;
        
        if [ ! -e ${IB_LIB}/libibmad.so ]; then
            echo "-E- shared library libibmad.so was not found in ${IB_LIB}.  $msg"
            return 1;
        fi
        if [ ! -e ${IB_LIB}/libibumad.so ]; then
            echo "-E- shared library libibumad.so was not found in ${IB_LIB}. $msg"
            return 1;
        fi
        if [ ! -e ${IB_LIB}/libibcommon.so ]; then
            echo "-E- shared library libibcommon.so was not found in ${IB_LIB}. $msg"
            return 1;
        fi
    fi

    return 0
}

function build_ibspark() {
    #cd $PACKAGE_DIR
    
    echo ""
    echo "  Installing ibspark ..."

    echo "  Configuring ibspark ..." >> $IBSPARKLOG 2>&1
    configure_ibspark $IB_PREFIX >> $IBSPARKLOG 2>&1
    if [ $? != 0 ]; then
        echo "   -E- ibspark build failed. See $IBSPARKLOG for details."
        return 1
    fi

    echo "  Making ibspark ..." >> $IBSPARKLOG 2>&1
    make ibspark -C ./ibfw/localburn IB_LIB=$IB_LIB IB_INC=$IB_INC >> $IBSPARKLOG 2>&1
    if [ $? != 0 ]; then 
	echo "   -E- ibspark build failed. See $IBSPARKLOG for details."
	return 1
    fi
    
    make install_ibspark -C ./ibfw/localburn >> $IBSPARKLOG 2>&1
    if [ $? != 0 ]; then 
	echo "   -E- ibspark installation failed. See $IBSPARKLOG for details."
	return 1
    else  
	echo "  ibspark installed successfully."
    fi
    
    return 0
}

function create_info_file() {
    mkdir -p `dirname $INFO`
    cat <<EOF > $INFO
INFO_INSTALL_DATE="$INSTALL_DATE"
INFO_MFTHOME="$MFTHOME"          
INFO_FINAL_PREFIX="$FINAL_PREFIX"
INFO_BLD_ID="$BLD_ID"
INFO_INSTALL_MST="$INSTALL_MST"
EOF
    
}

########################################################################

# Initialize default before parsing parameters
CONFIG_ROOT=/usr
INSTALL_ROOT=/usr/mellanox/mft
BUILD_ROOT=
INSTALL_LIB=/usr/mellanox
BUILD_ONLY=0
DIST=0
LOCAL_NODE=1
MFT_INST=1
FORCE_INST=0
WITH_IBSPARK=0

INSTALL_MST=1

use_local_tcl=0
use_tclsh=""

if test -z $MST_HOME; then
    export MST_HOME=/usr/mst
fi
MTCR_LIB=$MST_HOME/lib/libmtcr.a


# check_arg <flag> <arg>
# Check that agr is not empty.
# prints arg.
function check_arg() {
    if [ -z $2 ] || [[ "$2" == -* ]]; then 
        echo "-E- Missing parameter after \"$1\" switch."
	exit 1
    fi
}

# parse parameters
while [ "$1" ]; do
#  echo "Current parsed param is : $1"
  case $1 in
    "--prefix")
	  check_arg $1 $2
          CONFIG_ROOT=$2
          shift
          ;;
    "--ib-prefix")
	  check_arg $1 $2
          IB_PREFIX=$2
          shift
          ;;
    "--with-ibspark")
          WITH_IBSPARK=1
          ;;
	  
    "--build_root")     
	  check_arg $1 $2
          BUILD_ROOT=$2
          shift
          ;;
    "--build_only") 
          BUILD_ONLY=1
          echo "<<<<<<<< BUILD_ONLY                       >>>>>>>>>>"
          ;;
    "-d") 
	  check_arg $1 $2
          INSTALL_ROOT=$2
          shift
          ;;
    "--dist")
          DIST=1
          echo "<<<<<<<< DISTRIBUTION MODE                >>>>>>>>>>"
          ;;
    -f|--force)
          FORCE_INST=1
          echo "<<<<<<<< INSTALATION IN FORCE MODE        >>>>>>>>>>"
          ;;
     --skip_mst)
          INSTALL_MST=0
          echo "<<<<<<<< SKIP MST PACKAGE INSTALLATION    >>>>>>>>>>"
          ;;
     "--use_local_tcl")
          use_local_tcl=1
          echo "<<<<<<<< USING LOCAL TCL SHELL            >>>>>>>>>>"
          ;;
     "--with_tclsh")
	  check_arg $1 $2
          use_tclsh=$2
	  use_local_tcl=1
	  shift
          echo "<<<<<<<< USING LOCAL TCL SHELL $use_tclsh >>>>>>>>>>"
          ;;         
    *)

     cat<<EOF
-E- Bad switch "$1"
Usage: $0 [--prefix <install-dir>][-d <bld-dir>]
          [--force][--with_tclsh <tclsh>][--skip_mst]
          [--build_only][--build_root <bld-root-dir>]
Options:
   --prefix <dir> :       place bin/lib/include into <dir>
   -d <dir> :             place the build tree of MFT into <dir>
   --build_only:          do not update environment parameters
   --build_root <dir> :   use <dir> as addition to the prefix
   --with_tclsh <tclsh>:  do not install internal tcl, use the given tcl shell
   --skip_mst :           do not install mst package, use the insalled mst
   --force :              non interactive, force mode installation
   --with-ibspark:        include ibspark tool in MFT installation
   --ib-prefix <dir> :    prefix of OFED installation (for ibspark)
                          Supply this flag if it is not auto detected

EOF
     exit 1
  esac
  shift
done

# take the src BUILD_ID
BLD_ID="<unknown>"
if test -e ${PACKAGE_DIR}/BUILD_ID ; then
  BLD_ID=`cat ${PACKAGE_DIR}/BUILD_ID`
fi

tar_file=mft.tgz

if test ! -f $tar_file; then
    echo "-E- Failed to find tar file:$tar_file"
    exit 1
fi

if [ $use_local_tcl != 0 ]; then
   check_tcl
fi

if [ $INSTALL_MST == 0 ]; then 
    if test -f $MTCR_LIB; then
        echo "  Using the pre installed MST driver."
    else
        echo "-E- No installed MST on this machine. MFT Depends on $MTCR_LIB. Can not skip MST installation."
	cleanup
	exit 1
    fi
fi


cd $PACKAGE_DIR 
tar -zxf $tar_file
find . -exec touch {} \;

# Make sure the build root is available to the lower levels
export BUILD_ROOT
export LOCAL_NODE
export MFT_INST

BLD_VER=$BLD_ID
export BLD_VER

CFGLOG=/tmp/MFT.config.log.$$
rm -f /tmp/MFT.config.log.* 2>&1 > /dev/null
echo "MFT-$MY_EXT \($BLD_ID\) configuration as `date`." >| $CFGLOG
echo "Modules configuration log file:" >| $CFGLOG

MAKELOG=/tmp/MFT.make.log.$$
rm -f /tmp/MFT.make.log.* 2>&1 > /dev/null
echo "MFT-$MY_EXT \($BLD_ID\) installation at `date`." >| $MAKELOG
echo "Modules making log file:" >| $MAKELOG

MSTMAKELOG=/tmp/MFT.mst.log.$$
rm -f /tmp/MFT.mst.log.* 2>&1 > /dev/null

TCLMAKELOG=/tmp/MFT.tcl.log.$$
rm -f /tmp/MFT.tcl.log.* 2>&1 > /dev/null

IBSPARKLOG=/tmp/MFT.ibspark.log.$$
rm -f /tmp/MFT.ibspark.log.* 2>&1 > /dev/null

export MFTHOME=$INSTALL_ROOT

if [ $UID != 0 ]; then
  echo You must be root to install MFT
  rm -rf $PACKAGE_DIR
  exit 1
fi
umask 022   # assure readability to all users

cat<<EOF

  *** Mellanox Firmware Tools (MFT) Package Installation ***
      MFT Build $BLD_ID
  
  Copyright (C) June  2002, Mellanox Technologies  Ltd.  
  ALL  RIGHTS  RESERVED.   Use of  software subject to the
  terms and conditions detailed in the file "LICENSE.txt".

EOF

# Clear previous installations if any
check_prev_install

rm -f $INFO

echo ""
echo ""
if test ! -z $BUILD_ROOT; then
FINAL_PREFIX=`echo $CONFIG_ROOT | sed 's='$BUILD_ROOT'=/='`
else
FINAL_PREFIX=$CONFIG_ROOT
fi

mkdir -p $MFTHOME
cp ${PACKAGE_DIR}/BUILD_ID ${MFTHOME}/BUILD_ID
cp ${PACKAGE_DIR}/uninstall.sh ${MFTHOME}/
cp ${PACKAGE_DIR}/LICENSE.txt ${MFTHOME}/

echo $INSTALL_DATE > ${MFTHOME}/INSTALL_DATE

# Only installing user (root) can run uninstall
chmod a-x ${MFTHOME}/uninstall.sh
chmod u+x ${MFTHOME}/uninstall.sh

create_info_file

echo "  This installation installs the MFT components into $FINAL_PREFIX"
cd $MFTHOME

# Install MST Driver only if needed?
if [ $INSTALL_MST == 0 ]; then 
    if test -f $MTCR_LIB; then
        echo "  Using the pre installed MST driver."
    else
        echo "-E- MST is not installed on this machine. Can not skip MST installation"
	cleanup
	exit 1
    fi
else
    install_mst
fi


# check if MST libmtcr is available
check_mst

if [ $use_local_tcl != 0 ] ; then
    export TCLSH=$local_tcl
    echo "  Using pre installed tcl: $local_tcl" 
else
  echo "  Building internal Tcl 8.4 installation ..."
  build_tcl
fi

cd $MFTHOME
export PATH=$PATH:${PACKAGE_DIR}
echo "  Configuring $MFTHOME directory ..."
${PACKAGE_DIR}/configure --prefix ${MFTHOME} >> $CFGLOG 2>&1 &
config_pid=$!
wait $config_pid >/dev/null 2>&1
if [ $? != 0 ]; then config_failed ; fi
echo "  Building all packages (this will take a few minutes) ... "
mkdir -p $MFTHOME/bin
make install>> $MAKELOG 2>&1 
#echo "-D- Yaani making - REMOVE !!!"; yaani_make
if [ $? != 0 ]; then make_failed ; fi

if [ $WITH_IBSPARK == 1 ]; then
    build_ibspark
fi

# Copy the BUILD_ID to the MFTHOME (where we leave the sources etc)
cp ${PACKAGE_DIR}/BUILD_ID ${MFTHOME}/BUILD_ID

echo ""
echo "  Executables were placed under : $FINAL_PREFIX/bin"

# Copy docs
echo "  Copying docs .."
cp ${PACKAGE_DIR}/README ${MFTHOME}
cp -r  ${PACKAGE_DIR}/man ${MFTHOME}
#####################Update environment#############################
# we only need to set env if the prefix is not /usr and build_only option not used:
if [ $BUILD_ONLY == 0 ]; then 
    mkdir -p ${FINAL_PREFIX}/share/man/man1
    ln -fs ${MFTHOME}/man/mlxburn.1.gz ${FINAL_PREFIX}/share/man/man1
    ln -fs ${MFTHOME}/man/spark.1.gz ${FINAL_PREFIX}/share/man/man1
    ln -fs ${MFTHOME}/man/flint.1.gz ${FINAL_PREFIX}/share/man/man1


    mkdir -p ${FINAL_PREFIX}/bin
    ln -fs $MFTHOME/bin/t2a     ${FINAL_PREFIX}/bin/t2a
    ln -fs $MFTHOME/bin/mic     ${FINAL_PREFIX}/bin/mic
    ln -fs $MFTHOME/bin/aimage  ${FINAL_PREFIX}/bin/aimage
    ln -fs $MFTHOME/bin/flint   ${FINAL_PREFIX}/bin/flint
    ln -fs $MFTHOME/bin/spark   ${FINAL_PREFIX}/bin/spark
    ln -fs $MFTHOME/bin/mlxburn ${FINAL_PREFIX}/bin/mlxburn
    if [ -e $MFTHOME/bin/ibspark ]; then
        ln -fs $MFTHOME/bin/ibspark ${FINAL_PREFIX}/bin/ibspark 
    fi
    
    ln -fs $MFTHOME/uninstall.sh ${FINAL_PREFIX}/bin/uninstall_mft.sh
fi

cleanup 

cat<<EOF

  -----------------------------
     MFT Installation done.
  -----------------------------

EOF
exit 0
