#!/bin/sh

DEBUG=NO_DEBUG
ENTERED_VALUE=0

OSYS=dummy
CC=gcc
MPI=/opt/MPI
MAKE=make
MPCROOT=/opt/MPC
CPPDIR=/usr/bin

HMPI_LOG_FILE=`pwd`/hmpi_build_`date '+%m:%d:%y:%H:%M:%S'`.txt

input_control ()
{
 if_true=1
 while [ $if_true -eq 1 ]  ;  do
        read c
        if [  -z "$c"  ] ; then
                ENTERED_VALUE=7
                return 0
        else
               case "$c" in
                "$1")
                        ENTERED_VALUE=1
                        return 0
                        ;;
                "$2")
                        ENTERED_VALUE=2
                        return 0
                        ;;
                "$3")
                        ENTERED_VALUE=3
                        return 0
                        ;;
                "$4")
                        ENTERED_VALUE=4
                        return 0
                        ;;
                "$5")
                        ENTERED_VALUE=5
                        return 0
                        ;;
                "$6")
                        ENTERED_VALUE=6
                        return 0
                        ;;
                *)
                        printf  "Incorrect data\nEnter choice [%s]:" $1
                        ;;
          esac
        fi
 done
}


log()
{
  echo $1
}


askvar()
{
  printf "Enter value for $1\n"    
  echo
  printf "  ----------------------------------------\n"
  printf " |    Press  b  to go  BACK               |\n"
  printf " |    Press  q  to QUIT the instalation   |\n"
  printf "  ----------------------------------------\n"
  echo
  printf "Enter your path [$2]:"
  read ans
  if [ $ans ] ; then
    CHOICE=$ans
  else
    CHOICE=$2
  fi
}

sel_choice()
{
  param_1=1
  param_2=2
  param_3="@"
  param_4="@"
  if [ -n "$4" ] ; then
	param_3=3
        if [ -n "$5" ] ; then
		param_4=4
	fi
  fi
  CHOICE_def="nil"
  echo $1
  echo
  shift
  j=`expr 1`
  for i in $*
  do
    if [ ! -z $PRECHOICE ] ; then
      if [ $PRECHOICE = $i ] ; then
        CHOICE_def=$j
      fi
    fi
    printf "  %d) %s\n" $j $i
    
    j=`expr $j + 1`
  done
  echo
  if [ $first_time -eq 1 ] ; then
          printf "  ----------------------------------------\n"
          printf " |    Press  q  to QUIT the instalation   |\n"
          printf "  ----------------------------------------\n"
  else
          printf "  ----------------------------------------\n"
          printf " |    Press  b  to go  BACK               |\n"
          printf " |    Press  q  to QUIT the instalation   |\n"
          printf "  ----------------------------------------\n"
  fi

  if [ $CHOICE_def = "nil" ] ; then
    CHOICE_str=""
  else
    CHOICE_str="[$CHOICE_def]"
  fi
  echo
  printf "Enter choice [%s]:" $param_1
  if [ $first_time -eq 1 ] ; then
	  input_control $param_1 $param_2 $param_3 $param_4 @ q $param_1
	  first_time=0
  else
	  input_control $param_1 $param_2 $param_3 $param_4 b q $param_1
  fi
  if   [ $ENTERED_VALUE = "1" -o $ENTERED_VALUE = "7" ] ; then
  	CHOICE=$1
  elif [ $ENTERED_VALUE = "2" ] ; then
        CHOICE=$2
  elif [ $ENTERED_VALUE = "3" ] ; then
        CHOICE=$3
  elif [ $ENTERED_VALUE = "4" ] ; then
        CHOICE=$4
  elif [ $ENTERED_VALUE = "5" ] ; then
        CHOICE=b
  elif [ $ENTERED_VALUE = "6" ] ; then
        CHOICE=q
  fi
  echo $CHOICE
}

askyn()
{
  if [ $2 ] ; then
    askyn_def=$2;
  else
    askyn_def="yes";
  fi
  echo $1
  echo
  printf "  y) yes\n" 
  printf "  n) no\n"
  echo
  printf "  ----------------------------------------\n"
  printf " |    Press  b  to go  BACK               |\n"
  printf " |    Press  q  to QUIT the instalation   |\n"
  printf "  ----------------------------------------\n"
  echo
  printf "Enter choice [%s]:" $askyn_def
  input_control y n b q @ @ y
  if   [ $ENTERED_VALUE = "1" -o $ENTERED_VALUE = "7" ] ; then
        CHOICE=1
  elif [ $ENTERED_VALUE = "2" ] ; then
	CHOICE=2
  elif [ $ENTERED_VALUE = "3" ] ; then
        CHOICE=b
  elif [ $ENTERED_VALUE = "4" ] ; then
        CHOICE=q
  fi
}

intrpt()
{
  echo "Process interrupted by the user"
  sleep 1
  exit 1
}

check_not_in_path()
{
if which $1 > /dev/null 2>/dev/null; then
  log "Found $1 in path"
else
  log "$1 was not found in path. Please update your PATH variable and run install again."
  exit 1
fi
}

clear
true_param=1
menu_item=2
MPCCURDIR=`pwd`

trap intrpt 2

while [ $true_param -eq 1 ]  ;  do
   case "$menu_item" in
                "1")
		     clear
		     echo
		     PRECHOICE=$INST
		     first_time=1
		     printf "Please select Installation type:\n"
                     printf "  NORMAL - building application on different nodes.\n"
                     printf "  SINGLE - no distributed work is required.\n" 	
		     sel_choice ""  NORMAL SINGLE 
		     if   [ "$CHOICE" = "q" ] ;then
   			log "User cancelled installation"
                        exit 1
		     else
		        INST=$CHOICE
			menu_item=2
		     fi
		     ;;
		"2")
		     clear
		     echo
		     PRECHOICE=$OSYS
		     first_time=1
		     sel_choice "Please select PLATFORM:" LINUX FREEBSD SOLARIS HPUX 
		     if   [ "$CHOICE" = "b" ] ; then
                        menu_item=1
                     elif [ "$CHOICE" = "q" ] ;then
                        log "User cancelled installation"
                        exit 1
                     else
		        OSYS=$CHOICE
			menu_item=3
		     fi
		     ;;
		"3")
		     clear
		     echo
		     PRECHOICE=$MPI
  		     sel_choice  "Please select MPI implementation:" MPICH LAM HP-MPI
		     if   [ "$CHOICE" = "b" ] ; then
                        menu_item=2
                     elif [ "$CHOICE" = "q" ] ;then
                        log "User cancelled installation"
                        exit 1
                     else
			MPI=$CHOICE
                        menu_item=4
                     fi
                     ;;
		"4")
		     clear
		     echo
		     PRECHOICE=$CC
 		     sel_choice "Please select C compiler:" gcc cc mpicc
		     if   [ "$CHOICE" = "b" ] ; then
                        menu_item=3
                     elif [ "$CHOICE" = "q" ] ;then
                        log "User cancelled installation"
                        exit 1
                     else
                        CC=$CHOICE
                        menu_item=5
                     fi
                     ;;
		"5")
		     clear
		     echo
		     PRECHOICE=$MAKE
		     sel_choice "Please select make utility:" make gmake
		     if   [ "$CHOICE" = "b" ] ; then
                        menu_item=4
                     elif [ "$CHOICE" = "q" ] ;then
                        log "User cancelled installation"
                        exit 1
                     else
			MAKE=$CHOICE
			menu_item=6
		     fi
		     ;;
		"6")
		     clear
		     echo
		     askvar "HeteroMPI root installation Directory" "$MPCROOT" 
		     if   [ "$CHOICE" = "b" ] ; then
                        menu_item=5
                     elif [ "$CHOICE" = "q" ] ;then
                        log "User cancelled installation"
                        exit 1
                     else
			MPCROOT=$CHOICE
			HMPI_HOME=$CHOICE
			menu_item=7
		     fi
		     ;;

		"7")
		     clear
	             echo 
		     askvar "Path to the C preprocessor" "$CPPDIR"
                     if   [ "$CHOICE" = "b" ] ; then
                          menu_item=6
                     elif [ "$CHOICE" = "q" ] ;then
                          log "User cancelled installation"
                          exit 1
                     else
		          CPPDIR=$CHOICE  
			  true_param=0
	             fi
		     ;;
   esac
done

clear

echo
echo
echo
echo
echo
echo
echo
echo
echo ==============================================================================
echo                   Welcome to HeteroMPI installation
echo
echo      HeteroMPI provides all the features to the user to write portable and
echo      efficient parallel applications on Heterogeneous Networks of Computers.
echo      These features automate all the essential steps involved in application
echo      development on Heterogeneous Networks of Computers
echo ==============================================================================
echo
echo
echo
echo
echo
echo
echo
echo

sleep 5
clear

###################################################################################
#             DISPLAY MESSAGE OF INSTALLATION OF HETEROMPI                        #
###################################################################################

echo
echo
echo "Please see the file $HMPI_LOG_FILE for installation progress"

###################################################################################
#                          Checking the Requirements                              #
###################################################################################

echo
echo
echo Checking requirements
echo
echo

echo "=================================" > $HMPI_LOG_FILE 2>&1
echo `date '+%m:%d:%y:%H:%M:%S'` >> $HMPI_LOG_FILE 2>&1
echo "=================================" >> $HMPI_LOG_FILE 2>&1

#
# Check if $MPIDIR is set
#
if [ "$MPIDIR" = "" ]; then
        echo "Variable MPIDIR pointing to MPI installation not set in the environment"
        exit 1
fi

#
# Check if mpicc is in $PATH
#
if test ! -f $MPIDIR/bin/mpicc; then
        echo "mpicc to compile MPI code does not exist in $MPIDIR directory"
        exit 2
fi

#
# Check if mpirun is in $PATH
#
if test ! -f $MPIDIR/bin/mpirun; then
        echo "mpirun does not exist in $MPIDIR directory"
        exit 2
fi

check_not_in_path $CC >> $HMPI_LOG_FILE 2>&1
check_not_in_path $MAKE >> $HMPI_LOG_FILE 2>&1

#
# Check if $MPCLOAD is set
#
if [ "$MPCLOAD" = "" ]; then
        echo "Variable MPCLOAD not set in the environment"
        exit 1
fi

#
# Check if $MPCTOPO is set
#
if [ "$MPCTOPO" = "" ]; then
        echo "Variable MPCTOPO not set in the environment"
        exit 1
fi

#
# Check if $WHICHMPI is set
#
if [ "$WHICHMPI" = "" ]; then
        echo "Variable WHICHMPI not set in the environment"
        exit 1
fi

###################################################################################
#                          Installation of MPC                                    #
###################################################################################

echo
echo CC=$CC > ./install.vars
echo MPI=$MPI >> ./install.vars
echo MAKE=$MAKE >> ./install.vars
echo MPCHOME=$MPCROOT >> ./install.vars
echo CPPDIR=$CPPDIR >> ./install.vars
mv ./install.vars ./src/mpc/

HMPI_HOME=$MPCROOT; export HMPI_HOME
MPCHOME=$MPCROOT; export MPCHOME

ARCHITECTURE=`uname`

if [ "$ARCHITECTURE" = "Linux" ]; then
/bin/cp -f ./src/mpc/Makefile.Linux ./src/mpc/Makefile
elif [ "$ARCHITECTURE" = "SunOS" ]; then
/bin/cp -f ./src/mpc/Makefile.SunOS ./src/mpc/Makefile
elif [ "$ARCHITECTURE" = "Alpha" ]; then
/bin/cp -f ./src/mpc/Makefile.Alpha ./src/mpc/Makefile
elif [ "$ARCHITECTURE" = "HP-UX" ]; then
/bin/cp -f ./src/mpc/Makefile.HPUX ./src/mpc/Makefile
elif [ "$ARCHITECTURE" = "FreeBSD" ]; then
/bin/cp -f ./src/mpc/Makefile.FreeBSD ./src/mpc/Makefile
else
/bin/cp -f ./src/mpc/Makefile.unknown ./src/mpc/Makefile
fi

echo
echo
printf "Cleaning previous MPC build ..."
echo
echo
(cd src/mpc && $MAKE clean >> $HMPI_LOG_FILE 2>&1)
echo
echo
printf "done\n"
echo
echo

echo
echo
printf "Installing MPC ..."
echo
echo
(cd src/mpc && $MAKE all install >> $HMPI_LOG_FILE 2>&1)
echo
echo
echo "Installation of MPC succeeded"
echo
echo

###################################################################################
#                          Installation of HeteroMPI                              #
###################################################################################

ARCHITECTURE=`uname`

if [ "$ARCHITECTURE" = "Linux" ]; then
echo "HMETIS_DIR      = ../../Third_Party_Software/hmetis-1.5/hmetis-1.5-linux" > ./src/heterompi/Make.hmetis
/bin/cp -f ./src/heterompi/Makefile.$ARCHITECTURE ./src/heterompi/Makefile
elif [ "$ARCHITECTURE" = "SunOS" ]; then
echo "HMETIS_DIR      = ../../Third_Party_Software/hmetis-1.5/hmetis-1.5-sun4u-USparc" > ./src/heterompi/Make.hmetis
/bin/cp -f ./src/heterompi/Makefile.$ARCHITECTURE ./src/heterompi/Makefile
else
(cd ./Third_Party_Software/hmetis-1.5/DummyLibSrc && make)
echo "HMETIS_DIR      = ../../Third_Party_Software/hmetis-1.5/hmetis-1.5-platform" > ./src/heterompi/Make.hmetis
/bin/cp -f ./src/heterompi/Makefile.platform ./src/heterompi/Makefile
fi

echo
echo
printf "Cleaning previous HeteroMPI build ..."
echo
echo
$MAKE clean >> $HMPI_LOG_FILE 2>&1
echo
echo
printf "done\n"
echo
echo

echo
echo
printf "Installing HeteroMPI ..."
echo
echo

$MAKE build_third_party_software >> $HMPI_LOG_FILE 2>&1

if [ $? -gt 0 ]; then
	echo ""
	echo "Could not build the METIS Graph Partitioning Library"
        echo "Building dummy library, Some graph partitioning "
	echo "functionality may not work"
	echo ""
	(cd ./Third_Party_Software/metis-4.0/DummyLibSrc && $MAKE >> $HMPI_LOG_FILE 2>&1)
fi

$MAKE >> $HMPI_LOG_FILE 2>&1

if [ $? -gt 0 ]; then
	echo "Problems installing HeteroMPI..."
	exit 1
fi

$MAKE message

