#!/bin/sh
# do_make
#


W_PATH=`pwd`

IPATH="include/"


if [ $# -lt 2 ]; then
  echo "Usage: `basename $0` system cpu [action]"
  echo "  system: SINIX | LINUX | SOLARIS | AIX | IRIX | BSD | HPUX"
  echo "  cpu:    R4K | R10K | I586 | I586MMX | I686 | I686MMX | ATHLON | PPC604 | PA1" 
  echo "  action: <nothing> | clean | clobber"
  exit 1
fi


# function to install links ...
# install links for preprof, sprof, sprof_pt in your bin directory
#
install_splinks() {
if [ $# -eq 0 ]; then
  echo "usage: `basename $0` bin-directory"
  echo "example:   `basename $0` ~/bin"
  echo "to install some links in your bin-directory."
  echo "(`basename $0` must be called inside its directory!)"
  exit 1
fi

BINDIR=$1
#SPROF_DIR=${W_PATH}/sprof
SCRIPT_DIR=${W_PATH}/scripts

#for i in preprof sprof sprof_pt; do
#  if [ -h "${BINDIR}/$i" ]; then
#    # echo "--- removing old link $i"
#    rm "${BINDIR}/$i"
#  fi
#  ln -s ${SPROF_DIR}/$i ${BINDIR}/$i
#done

for i in s_run_pt sprof_eval_r4k sprof_eval_r10k sprof_eval_i686; do
  if [ -h "${BINDIR}/$i" ]; then
    # echo "--- removing old link $i"
    rm "${BINDIR}/$i"
  fi
  ln -s ${SCRIPT_DIR}/$i ${BINDIR}/$i
done
}
# end of function

#
# Solaris check isa (instruction set architecture)
# $1=ISA to compile for, $2..$n=possible ISA on this machine
#
sol_check_isa() {
  if [ $# -eq 0 ]; then
    echo "sol_check_isa: parameter error!"
    exit 1
  fi
  ISA1=$1
  shift
  for i in $*; do
    if [ "${ISA1}" = "${i}" ]; then
      return 0
    fi
  done
  echo "WARNING: ISA ${ISA1} is not supported on this machine!"
  return 1
}
# end of function

AWK=awk
# determine if nawk exists (Solaris 2.6: nawk supports toupper)
type nawk >/dev/null 2>&1
if [ $? -eq 0 ]; then
  AWK=nawk
fi

SYSTEM=`echo $1 | ${AWK} '{print toupper($1)}'`
CPU=`echo $2 | ${AWK} '{print toupper($1)}'`
ACTION=`echo $3 | ${AWK} '{print tolower($1)}'`
SUBDIRS="include sprof etrace mstack bench tools"

echo "system: ${SYSTEM}, cpu: ${CPU}  selected."

if [ "${SYSTEM}" = "SINIX" ]; then
  SYSTEM=SINIX
  if [ "${CPU}" = "R4K" ]; then
    SUBDIRS="${SUBDIRS} driver/sinix/r4k_evcnt driver/sinix/rxk_evcnt driver/sinix/rxk_prf"
  elif [ "${CPU}" = "R10K" ]; then
    SUBDIRS="${SUBDIRS} driver/sinix/r4k_evcnt driver/sinix/rxk_evcnt driver/sinix/rxk_prf"
  else 
    echo "Warning: System ${SYSTEM} supports only CPUs R4K, R10K."
    exit 1
  fi
elif [ "${SYSTEM}" = "LINUX" ]; then
  SYSTEM=LINUX
  SUBDIRS="${SUBDIRS} driver/linux/ix86"
  if [ "${CPU}" = "I586" ]; then
   :
  elif [ "${CPU}" = "I586MMX" ]; then
   :
  elif [ "${CPU}" = "I686" ]; then
   :
  elif [ "${CPU}" = "I686MMX" ]; then
   :
  elif [ "${CPU}" = "ATHLON" ]; then
   : 
  else 
    echo "Warning: System ${SYSTEM} supports only CPUs I586, I586MMX, I686, I686MMX, ATHLON."
    exit 1
  fi
elif [ "${SYSTEM}" = "SOLARIS" ]; then
  SYSTEM=SOLARIS
  SUBDIRS="${SUBDIRS} driver/solaris/ix86"
  ISALIST=`isalist`
  if [ "${CPU}" = "I586" ]; then
   sol_check_isa pentium ${ISALIST}
   :
  elif [ "${CPU}" = "I586MMX" ]; then
   sol_check_isa pentium+mmx ${ISALIST}
   :
  elif [ "${CPU}" = "I686" ]; then
   sol_check_isa pentium_pro ${ISALIST}
   :
  elif [ "${CPU}" = "I686MMX" ]; then
   sol_check_isa pentium_pro+mmx ${ISALIST}
   :
  else
    echo "Warning: System ${SYSTEM} supports only CPUs I586, I586MMX, I686, I686MMX"
    exit 1
  fi
elif [ "${SYSTEM}" = "AIX" ]; then
  SYSTEM=AIX
  SUBDIRS="${SUBDIRS} driver/aix/ppc604"
  if [ "${CPU}" = "PPC604" ]; then
   :
  else
    echo "Warning: System ${SYSTEM} supports only CPU PPC604"
    exit 1
  fi
elif [ "${SYSTEM}" = "IRIX" ]; then
  SYSTEM=IRIX
  SUBDIRS="${SUBDIRS}"
elif [ "${SYSTEM}" = "BSD" ]; then
  SYSTEM=BSD
  SUBDIRS="${SUBDIRS}"
elif [ "${SYSTEM}" = "HPUX" ]; then
  SYSTEM=HPUX
  SUBDIRS="${SUBDIRS}"
  if [ "${CPU}" = "PA1" ]; then
   :
  else
    echo "Warning: System ${SYSTEM} supports only CPU PA1"
    exit 1
  fi
else
  echo "Error: Unknown system ${SYSTEM}."
  exit 1
fi


echo "Creating ${IPATH}make.hd"
echo "# This file is automatically created from make.hd.template." > ${IPATH}make.hd
echo "# system: ${SYSTEM} cpu: ${CPU}" >> ${IPATH}make.hd
echo "CPU = ${CPU}_EVCNT" >> ${IPATH}make.hd
sed "s/${SYSTEM}_//" ${IPATH}make.hd.template >> ${IPATH}make.hd
  # remove all "${SYSTEM}_" from make.hd


for i in ${SUBDIRS}; do
  echo "Checking ${W_PATH}/${i}"
  cd ${W_PATH}/${i}
  make -k ${ACTION}
done



if [ "${ACTION}" = "clean" ]; then
  cd ${W_PATH}/lib
  rm -f `find . -name '*.[o]' ! -print` ""
  rm -f ${W_PATH}/${IPATH}make.hd
  echo "make clean: *.o removed."
fi

if [ "${ACTION}" = "clobber" ]; then
  cd ${W_PATH}/lib
  rm -f `find . -name '*.[oa]' ! -print` ""
  rm -f ${W_PATH}/${IPATH}make.hd
  cd ${W_PATH}/bin
  rm ${W_PATH}/bin/*
  echo "make clobber: *.o, *.a and executables removed."
fi

echo ""

BINDIR=${W_PATH}/bin
# action=none (compile) -> install also links
if [ "${ACTION}" = "" ]; then
  install_splinks ${BINDIR}
fi

#end
