#!/bin/sh
# --------------------------------------------------------------
# (C) Copyright 2001,2007,
# International Business Machines Corporation,
#
# All Rights Reserved.
# --------------------------------------------------------------

#------------------------------------------------------------------------
#  cellsdk_select_compiler
#
#  This script selects the compiler in make.env
#
#------------------------------------------------------------------------

#
# Miscellaneous variables
#
if [ -f ${CELL_TOP}/buildutils/make.env ] ; then
	CELL_BUILD_DIR=$CELL_TOP
else
	CELL_BUILD_DIR=/opt/cell/sdk
fi
CELL_MAKE_ENV=${CELL_BUILD_DIR}/buildutils/make.env
CELL_MAKE_FOOTER=${CELL_BUILD_DIR}/buildutils/make.footer

#--------------------------------------------------------------------------
# Switches to the gcc compiler
#
# return: none
function switch_to_gcc()
{
  # abort if gcc is not installed
  which `PROGRAM_ppu=foo PPU_COMPILER=gcc make --no-print-directory CCACHE="" -f ${CELL_MAKE_FOOTER} listenv_c 2>/dev/null` >/dev/null 2>&1
  if [ $? -ne 0 ] ; then
        echo "gcc is not installed - can't change default to gcc."
  elif [ "`grep "^ .*_COMPILER.*:= gcc" ${CELL_MAKE_ENV}`" == "" ] ; then
	sed -i -e 's/^    PPU32_COMPILER	:= xlc/#   PPU32_COMPILER	:= xlc/' \
	       -e 's/^#   PPU32_COMPILER	:= gcc/    PPU32_COMPILER	:= gcc/' \
	       -e 's/^    PPU64_COMPILER	:= xlc/#   PPU64_COMPILER	:= xlc/' \
	       -e 's/^#   PPU64_COMPILER	:= gcc/    PPU64_COMPILER	:= gcc/' \
	       -e 's/^    SPU_COMPILER	:= xlc/#   SPU_COMPILER	:= xlc/' \
	       -e 's/^#   SPU_COMPILER	:= gcc/    SPU_COMPILER	:= gcc/' \
	    ${CELL_MAKE_ENV} && echo "Default compiler changed to GNU gcc."
  else
      echo "Default compiler is already set to GNU gcc."
  fi
}

#--------------------------------------------------------------------------
# Switches to the gfortran
#
function switch_to_gfortran()
{
  # abort if gfortran is not installed
  which `PROGRAM_ppu=foo FTN_PPU32_COMPILER=gfortran FTN_PPU64_COMPILER=gfortran make --no-print-directory -f ${CELL_MAKE_FOOTER} listenv_f 2>/dev/null` >/dev/null 2>&1
  if [ $? -ne 0 ] ; then
        echo "gfortran is not installed - can't change default to gfortran."
  elif [ "`grep "^ .*_COMPILER.*:= gfortran" ${CELL_MAKE_ENV}`" == "" ] ; then
	sed -i -e 's/^    FTN_PPU32_COMPILER	:= xlf/#   FTN_PPU32_COMPILER	:= xlf/' \
	       -e 's/^#   FTN_PPU32_COMPILER	:= gfortran/    FTN_PPU32_COMPILER	:= gfortran/' \
	       -e 's/^    FTN_PPU64_COMPILER	:= xlf/#   FTN_PPU64_COMPILER	:= xlf/' \
	       -e 's/^#   FTN_PPU64_COMPILER	:= gfortran/    FTN_PPU64_COMPILER	:= gfortran/' \
	       -e 's/^    FTN_SPU_COMPILER	:= xlf/#   FTN_SPU_COMPILER	:= xlf/' \
	       -e 's/^#   FTN_SPU_COMPILER	:= gfortran/    FTN_SPU_COMPILER	:= gfortran/' \
	    ${CELL_MAKE_ENV} && echo "Default compiler changed to GNU gfortran."
  else
      echo "Default compiler is already set to GNU gfortran."
  fi
}

#--------------------------------------------------------------------------
# Switches to the xlc compiler
#
function switch_to_xlc()
{
  # abort if xlc is not installed
  which `PROGRAM_ppu=foo PPU_COMPILER=xlc make --no-print-directory -f ${CELL_MAKE_FOOTER} listenv_c 2>/dev/null` >/dev/null 2>&1
  if [ $? -ne 0 ] ; then
        echo "xlc is not installed - can't change default to xlc."
  elif [ "`grep "^ .*_COMPILER.*:= xlc" ${CELL_MAKE_ENV}`" == "" ] ; then
	sed -i -e 's/^#   PPU32_COMPILER	:= xlc/    PPU32_COMPILER	:= xlc/' \
	       -e 's/^    PPU32_COMPILER	:= gcc/#   PPU32_COMPILER	:= gcc/' \
	       -e 's/^#   PPU64_COMPILER	:= xlc/    PPU64_COMPILER	:= xlc/' \
	       -e 's/^    PPU64_COMPILER	:= gcc/#   PPU64_COMPILER	:= gcc/' \
	       -e 's/^#   SPU_COMPILER	:= xlc/    SPU_COMPILER	:= xlc/' \
	       -e 's/^    SPU_COMPILER	:= gcc/#   SPU_COMPILER	:= gcc/' \
	    ${CELL_MAKE_ENV} && echo "Default compiler changed to IBM xlc."
  else
      echo "Default compiler is already set to IBM xlc."
  fi
}

#--------------------------------------------------------------------------
# Switches to the xlf compiler
#
function switch_to_xlf()
{
  # warn if xlf is not installed
  which `PROGRAM_ppu=foo FTN_PPU32_COMPILER=xlf FTN_PPU64_COMPILER=xlf make --no-print-directory -f ${CELL_MAKE_FOOTER} listenv_f 2>/dev/null` >/dev/null 2>&1
  if [ $? -ne 0 ] ; then
        echo "xlf is not installed - can't change default to xlf."
  elif [ "`grep "^ .*_COMPILER.*:= xlf" ${CELL_MAKE_ENV}`" == "" ] ; then
	sed -i -e 's/^#   FTN_PPU32_COMPILER	:= xlf/    FTN_PPU32_COMPILER	:= xlf/' \
	       -e 's/^    FTN_PPU32_COMPILER	:= gfortran/#   FTN_PPU32_COMPILER	:= gfortran/' \
	       -e 's/^#   FTN_PPU64_COMPILER	:= xlf/    FTN_PPU64_COMPILER	:= xlf/' \
	       -e 's/^    FTN_PPU64_COMPILER	:= gfortran/#   FTN_PPU64_COMPILER	:= gfortran/' \
	       -e 's/^#   FTN_SPU_COMPILER	:= xlf/    FTN_SPU_COMPILER	:= xlf/' \
	       -e 's/^    FTN_SPU_COMPILER	:= gfortran/#   FTN_SPU_COMPILER	:= gfortran/' \
	    ${CELL_MAKE_ENV} && echo "Default compiler changed to IBM xlf."
  else
      echo "Default compiler is already set to IBM xlf."
  fi
}


#--------------------------------------------------------------------------
# Displays the usage statement
#
function usage()
{
  echo "Usage: $SELF <gcc | gfortran | gnu | xlc | xlf | xl> "
  echo ""
  echo "  gcc:      set GNU gcc as default c/c++ compiler in make.footer (default)"
  echo "  gfortran: set GNU gfortran as default fortran compiler in make.footer (default)"
  echo "  gnu:      set both GNU gcc and gfortran as default compilers in make.footer (default)"
  echo "  xlc:      set IBM xlc as default c/c++ compiler in make.footer"
  echo "  xlf:      set IBM xlf as default fortran compiler in make.footer"
  echo "  xl:       set IBM xlc and xlf as default compilers in make.footer"
  echo ""
}


#--------------------------------------------------------------------------
# Continue with main line execution of script


#  Set our name
SELF=`basename $0`

#  Ensure we have at least one argument
if [ $# -lt 1 ] ; then
  usage
  exit 1
fi

#  Pop the first argument as the task
TASK=$1
shift

case $TASK in
  gcc|gfortran|gnu|xlc|xlf|xl)
    if [ ! -w ${CELL_MAKE_ENV} ] ; then
	echo ${CELL_MAKE_ENV} "not writable"
	exit 1
    fi
    ;;

  -?|-h|--help|--usage)
    usage
    exit 0
    ;;

  *)
    echo "$SELF:  unknown option '$TASK'"
    usage
    exit 1
    ;;

esac

case $TASK in
 	gcc)
		switch_to_gcc
		;;
 	gfortran)
		switch_to_gfortran
		;;
 	gnu)
		switch_to_gcc
		switch_to_gfortran
		;;

 	xlc)
		switch_to_xlc
		;;
 	xlf)
		switch_to_xlf
		;;
 	xl)
		switch_to_xlc
		switch_to_xlf
		;;
	*)
		usage
		exit 1
		;;
esac

# end of script
