#!/bin/bash

# Hint:
# DARCS_P=/usr/local/bin/darcs ./profile-big-zoo.sh
# DARCS_P=/path/to/my/darcs ./profile-big-zoo.sh
if [ "${DARCS_P}" = "" ]; then
  DARCS_P=darcs
  TESTNAME=frompath
else
  DARCS_P_DIR=`dirname ${DARCS_P}`
  DARCS_P_DIR_DIR=`dirname ${DARCS_P_DIR}`
  DARCS_P_DIR_DIR=`basename ${DARCS_P_DIR_DIR}`
  DARCS_P_DIR_BASE=`basename ${DARCS_P_DIR}`
  TESTNAME=${DARCS_P_DIR_DIR}-${DARCS_P_DIR_BASE}
fi
if [ "${TIMESTAMP}" = "" ]; then
  TIMESTAMP=`date +%Y-%m-%d.%H%M.%S`
fi
PROF_DIR=`pwd`
#PROF_FLAGS="+RTS -prof -RTS"
LATEST_RESULTS=big_${TIMESTAMP}_${PREFIX}_${TESTNAME}

profile () {
  name=$1
  shift
  echo "=== $name ==="
  {
    time -p ${DARCS_P} $* -q ${PROF_FLAGS} > /dev/null
  } 2> ${PROF_DIR}/${name}.time
  if [ -e darcs_p.prof ]; then
    mv darcs_p.prof ${PROF_DIR}/${name}.prof
  fi
  echo "=== $name ===" >> ${PROF_DIR}/${LATEST_RESULTS}
  cat ${PROF_DIR}/${name}.time >> ${PROF_DIR}/${LATEST_RESULTS}
}

cd big-zoo

# ----------------------------------------------------------------------
# tests on the GHC repo
# ----------------------------------------------------------------------

rm -rf ghc-hashed-2
profile ghc-get-complete get --no-cache --complete ghc-hashed ghc-hashed-2

rm -rf ghc-hashed-2
profile ghc-get-complete-cache get --complete ghc-hashed ghc-hashed-2

cd ghc-hashed-2
profile ghc-check check
cd ..

rm -rf ghc-hashed-lazy
${DARCS_P} get --no-cache --lazy ghc-hashed ghc-hashed-lazy
rm -rf ghc-hashed-lazy/_darcs
cd ghc-hashed-lazy
profile ghc-init init
profile ghc-add-r-ghc add -r .
cd ..

rm -rf ghc-hashed-lazy
profile ghc-get-lazy get --no-cache --lazy ghc-hashed ghc-hashed-lazy

rm -rf ghc-hashed-lazy
profile ghc-get-lazy-cache get --lazy ghc-hashed ghc-hashed-lazy

cd ghc-hashed-lazy

profile ghc-unpull-10 unpull -a --last=10
profile ghc-pull-10   pull --no-cache -a

rm -f _darcs/patches/unrevert
profile ghc-unpull-100 unpull -a --last=100
profile ghc-pull-100   pull --no-cache -a

rm -f _darcs/patches/unrevert
profile ghc-unpull-1000 unpull -a --last=1000
profile ghc-pull-1000   pull --no-cache -a

rm -f _darcs/patches/unrevert
profile ghc-unpull-1000-cache unpull -a --last=1000
profile ghc-pull-1000-cache   pull -a

profile ghc-whatsnew-empty whatsnew
for i in `find compiler -name '*hs'`;\
do sed -e 's/id/ego/' $i > $i.2 ;\
   mv -f $i.2 $i;\
done
profile ghc-whatsnew-id-ego whatsnew
profile ghc-revert          revert -a
for i in `find compiler -name '*hs'`;\
do sed -e 's/id/ego/' $i > $i.2 ;\
   mv -f $i.2 $i;\
done
profile ghc-record          record -a -m foo -A me
profile ghc-unrecord        unrecord -a --last=1
profile ghc-record-l        record -l -a -m foo -A me
${DARCS_P} unpull --last=1 -a

profile ghc-annotate        annotate compiler/prelude/PrimOp.lhs +RTS -K100M -RTS

cd ..
# ----------------------------------------------------------------------
# tests on the darcs repo
# ----------------------------------------------------------------------

rm -rf darcs-repo-2
profile darcs-get-complete get --no-cache --complete darcs-repo darcs-repo-2

rm -rf darcs-repo-lazy
profile darcs-get-lazy     get --no-cache --lazy     darcs-repo darcs-repo-lazy

cd darcs-repo-lazy
profile darcs-annotate     annotate Setup.hs

cd ..
