#!/bin/sh
version="Time-stamp: <2007-04-16 15:40:00 vonrhein>"
#
# Author: Clemens Vonrhein <vonrhein@GlobalPhasing.com>
#
#         Copyright 2001 by Global Phasing Ltd.
#
#----------------------------------------------------------------------
#              BEGIN OF USER INPUT
#----------------------------------------------------------------------

# list of SIN files to use (these are the ones from the latest
# sharp_tutorials.tar.gz file)
sins="$BDG_home/samples/sharp/cardfiles/IF3-C.1.sin $BDG_home/samples/sharp/cardfiles/KrEl.1.sin"

#----------------------------------------------------------------------
#               END OF USER INPUT
#----------------------------------------------------------------------
echo >&2
ShortVersion=`echo $version | cut -f2- -d':'`
echo " `basename $0`   version ${ShortVersion} " >&2
echo " Clemens Vonrhein <vonrhein@GlobalPhasing.com>" >&2
echo >&2
#----------------------------------------------------------------------
#               BEGIN OF SCRIPT
#----------------------------------------------------------------------

error () {
  echo " "
  echo " ERROR: $@"
  echo " "
  exit 1
}
analyse () {
  cat <<end_ip > .runSHARP

  #=============================================================================
  # `cat .hostname` `pwd`
  #-----------------------------------------------------------------------------
  Processor ........................ `cat .processor`
  no of CPUs ....................... `cat .ncpu`
  Speed ............................ `cat .speed`
  Memory ........................... `cat .memory`
  OS ............................... `cat .os`

  Timing:
end_ip
  tail LIST.html | grep "^[ ]*real " | \
    awk '{printf("    real");for(i=2;i<=NF;i++){printf(" %s",$i)};printf("\n")}' >> .runSHARP
  tail LIST.html | grep "^[ ]*user " | \
    awk '{printf("    user");for(i=2;i<=NF;i++){printf(" %s",$i)};printf("\n")}' >> .runSHARP
  tail LIST.html | grep "^[ ]*sys "  | \
    awk '{printf("    sys ");for(i=2;i<=NF;i++){printf(" %s",$i)};printf("\n")}' >> .runSHARP
  echo " " >> .runSHARP
  ncyc=`grep -c deriv_ LIST.html`
  if [ $ncyc -eq 0 ]; then
    ncyc=`grep -c cycle_info LIST.html`
  fi
  version=`awk '/Version/{print $4}' LIST.html`
  echo " " >> .runSHARP
  echo "    Version $version" >> .runSHARP
  echo " " >> .runSHARP
  echo "    Number of small cycles = $ncyc" >> .runSHARP
  cat .runSHARP
}

if [ "X$BDG_home" = "X" ]; then
  error "variable \$BDG_home not set"
fi
export BDG_home

case `uname -s` in
  Linux)
    case `uname -m` in
      alpha)
        BDG_hwsp=alinux
        ;;
      i?86)
        BDG_hwsp=linux
        ;;
      x86_64)
        BDG_hwsp=linux
        ;;
      ia64)
        BDG_hwsp=linuxa64
        ;;
      *)
        error "unsupported Linux hardware"
        ;;
    esac
    time="/usr/bin/time"
    timeflag="--portability"
    speed=`cat /proc/cpuinfo | awk '/^cpu MHz/{printf("%d\n",$NF)}' | tail -1`
    speed="$speed MHz"
    processor=`cat /proc/cpuinfo | awk '/^model name/' | cut -f2 -d':' | sed "s/^ //g" | tail -1`
    vendor=`cat /proc/cpuinfo | awk '/^vendor_id/' | cut -f2 -d':' | sed "s/^ //g" | tail -1`
    processor="$processor ($vendor)"
    memory=`dmesg  | awk '/^Memory/{print $2}' | \
            cut -f2 -d'/' | sed "s/k//g" | awk '{printf("%d\n",($1/1024)+0.5)}'`
    if [ "X$memory" = "X" ]; then
      memory=`cat /var/log/dmesg 2>/dev/null | \
              awk '/^Memory/{print $2}' | cut -f2 -d'/' | sed "s/k//g" | awk '{printf("%d\n",($1/1024)+0.5)}'`
    fi
    if [ "X$memory" = "X" ]; then
      memory=`cat /var/log/boot.msg 2>/dev/null | \
              awk '/Memory: /{print $2}' | cut -f2 -d'/' | sed "s/k//g" | awk '{printf("%d\n",($1/1024)+0.5)}'`
    fi
    memory="$memory MB"
    ncpu=`cat /proc/cpuinfo | grep -c '^processor'`
    if   [ -f /etc/debian_version ]; then
      os=`cat /etc/debian_version 2>/dev/null | head -1`
    elif [ -f /etc/redhat-release ]; then
      os=`cat /etc/redhat-release 2>/dev/null | head -1`
    elif [ -f /etc/SuSE-release ]; then
      os=`cat /etc/SuSE-release 2>/dev/null | head -1`
    elif [ -f /etc/mandrake-release ]; then
      os=`cat /etc/mandrake-release 2>/dev/null | head -1`
    elif [ -f /etc/slackware-version ]; then
      os=`cat /etc/slackware-version 2>/dev/null | head -1`
    fi
    if [ "X$os" = "X" ]; then
      os="$os `cat /proc/version | awk '{print $3}'`"
    fi
    ;;
  OSF1)
    BDG_hwsp=alpha
    time="/usr/bin/time"
    timeflag=""
    processor=`/usr/sbin/psrinfo -v | awk '/processor operates at/{print $2,$3,$4}' | tail -1`
    speed=`/usr/sbin/psrinfo -v | awk '/processor operates at/{print $(NF-1)}' | tail -1`
    speed="$speed MHz"
    memory=`/usr/bin/vmstat -P | awk '/^Total Physical Memory/{print $(NF-1)}'`
    memory="$memory MB"
    ncpu=`/usr/sbin/psrinfo -n | awk '{print $NF}'`
    os=`/usr/sbin/sizer -v | cut -f1 -d';'`
    ;;
  Darwin)
    BDG_hwsp=darwin
    time="/usr/bin/time"
    timeflag="-p"
    processor=processor=`/usr/bin/hostinfo | awk '/^Processor type/{print $3}'`
    speed=`/usr/sbin/lsconf | awk '/^Processor Clock Speed/{print $(NF-1)}' | tail -1`
    speed="$speed MHz"
    memory=`/usr/bin/hostinfo | awk '/^Primary memory available/{print $4}'`
    memory="$memory MB"
    ncpu=`/usr/bin/hostinfo | grep "processor" | awk '/physically available/{print $1}'`
    os=`/usr/bin/sw_vers | awk '/ProductName/{i=split($0,a,":")}/ProductVersion/{i=split($0,b,":");print a[2],b[2]}'`
    ;;

  IRIX*)
    case `uname -r | awk -F. '{print $1}'` in
      5)
        BDG_hwsp=irix
        ;;
      6)
        case `uname -r | awk -F. '{print $2}'` in
          [0-1])
            if [ "X`uname -s`" = "XIRIX" ]; then
              BDG_hwsp=irix
            else
              BDG_hwsp=irix64
            fi
            ;;
          *)
            BDG_hwsp=irixn32
            ;;
        esac
        ;;
      *)
        error "unsupported IRIX release `uname -r`"
        ;;
    esac
    time="/usr/bin/time"
    timeflag=""
    speed=`/sbin/hinv | grep " IP[0-9][0-9] " | awk '/Processor/{print $2}'`
    speed="$speed MHz"
    processor=`/sbin/hinv -t cpu | awk '{print $3}'`
    memory=`/sbin/hinv  | grep " memory " | awk '/Mb/{print $(NF-1)}'`
    memory="$memory MB"
    ncpu=`/sbin/hinv | grep " IP[0-9][0-9] " | awk '/Processor/{print $1}'`
    os="`uname -s` `uname -R 2>/dev/null || uname -r 2>/dev/null`"
    ;;
  *)
    error "unsupported platform"
    ;;
esac

for dir in $BDG_home $BDG_home/database $BDG_home/bin/${BDG_hwsp}_exe
do
  if [ ! -d $dir ]; then
    error "directory \"$dir\" not found"
  fi
done


 SYMOP=$BDG_home/database/symop
ATOMSF=$BDG_home/database/atomsf
export SYMOP ATOMSF

exe=$BDG_home/bin/${BDG_hwsp}_exe/sharp

for file in $BDG_home/.licence $exe $SYMOP $ATOMSF $time
do
  if [ ! -f $file ]; then
    error "file \"$file\" not found"
  fi
done

if [ $# -eq 1 ] && \
   [ "X$1" = "X-a" ]; then
  doAnalyse="yes"
  doRunit="no"
else
  doAnalyse="no"
  doRunit="yes"
fi

cdir=`pwd`

isin=0
for sin in $sins
do
  isin=`expr $isin + 1`
  if [ ! -f $sin ]; then
    error "SIN file \"$sin\" not found"
  fi
  id=`basename $sin | awk -F. '{print $1}'`
  if [ "X$mtzs" != "X" ]; then
    mtz=`echo $mtzs | awk -v i=$isin '{if (NF>=i) print $i}'`
  else
    mtz=`dirname $sin`
    mtz="`dirname $mtz`/datafiles"
    mtz="$mtz/$id.data.mtz"
  fi
  if [ "X$mtz" = "X" ]; then
    error "unable to determine MTZ file"
  fi
  if [ ! -f $mtz ]; then
    error "MTZ file \"$mtz\" not found"
  fi

  i=1
  while [ -d ${id}.${i} ] || [ -f ${id}.${i} ]
  do
    if [ "X$doAnalyse" = "Xyes" ]; then
      cd ${id}.${i}
      if [ ! -f .hostname ]; then
        hostname -s > .hostname
      else
        h=`cat .hostname`
      fi
      if [ "X$h" = "X`hostname -s`" ]; then
        echo "$processor" > .processor
        echo "$ncpu"      > .ncpu
        echo "$speed"     > .speed
        echo "$memory"    > .memory
        echo "$os"        > .os
      fi
      analyse
      cd $cdir
    fi
    i=`expr $i + 1`
  done

  if [ "X$doRunit" = "Xyes" ]; then

    mkdir ${id}.${i}
    if [ $? -ne 0 ]; then
      error "unable to create directory ${id}.${i}"
    fi

    cd ${id}.${i}
    hostname -s > .hostname
    echo "$processor" > .processor
    echo "$ncpu" > .ncpu
    echo "$speed" > .speed
    echo "$memory" > .memory
    echo "$os" > .os

    cp $sin SIN
    ln -s $mtz REFL01.mtz

    echo -n " running SHARP in `pwd` ... "
    $time $timeflag $exe > LIST.html 2>&1
    cat .SHARPstatus

    analyse

    cd $cdir
  fi

done

#----------------------------------------------------------------------
#               END OF SCRIPT
#----------------------------------------------------------------------
