#!/bin/sh

# Check Boost code for errors
#
# Currently each public header file is just compiled. This ensures
# proper inclusions, ...
#
# Copright 2007, Jens Seidel <jensseidel@users.sf.net>
# Licensed under GPL version 2 or later

#set -x
skip_file=~/Boost/svn/trunk/skip
debug=1

CXXFLAGS="-Wall -Wno-long-long -W -pedantic -I /home/jens/Boost/svn/trunk/ -I /usr/include/python2.4"

libraries=""

function usage()
{
  echo $0: Usage: $(basename $0) [-l library]...
  echo
}

function compile()
{
  #/usr/lib/gcc-snapshot/bin/g++ $CXXFLAGS -c $1
  g++ $CXXFLAGS -c $1
}

function compile_in_vim()
{
  #echo -e "all:\n	/usr/lib/gcc-snapshot/bin/g++ $CXXFLAGS -c $1" > /tmp/Makefile
  echo -e "all:\n	g++ $CXXFLAGS -c $1" > /tmp/Makefile
  cd /tmp
  vim $1
  cd -
}

while [ $# -gt 0 ]; do
  if [ "$1" = -l ]; then
    shift
    if [ $# -eq 0 ]; then
      echo "$0: Error -l not followed by a library"
      echo
      exit -1
    fi
    libraries="$libraries $1"
    shift
    continue
  fi
  usage
  exit -2
done

if [ -z "$libraries" ]; then
  usage
  exit -2
fi

[ $debug = 1 ] && echo "Checking libraries $libraries ..."

for lib in $libraries; do
  if ! [ -d $lib ]; then
    echo "Directory $lib doesn't exist"
    exit -3
  fi
  cd $lib
  tmpcpp=$(tempfile --prefix=test --suffix=.cpp)
  for header in $(find -type f -name "*.hpp" | grep -v "/detail/" | sed 's,^\./,,'); do
    echo Checking boost/$lib/$header ...
    if grep --quiet boost/$lib/$header $skip_file; then
      echo "  ... skipping (black listed)"
      continue
    fi

    [ -e /tmp/stop.test ] && break
    echo "#include <boost/$lib/$header>" > $tmpcpp
    echo >> $tmpcpp
    #echo "int main() { return 0; }" >> $tmpcpp
    compile $tmpcpp 2>&1 > /tmp/boost.errors
    if [ $? -ne 0 ] ; then
      skip_header=0
      for included in $(cat $skip_file); do
        grep --quiet $included: /tmp/boost.errors && echo "  ... skipping (uses blacklisted $included)" && skip_header=1 && break
      done
      [ $skip_header = 1 ] && continue
      cat /tmp/boost.errors
      #compile_in_vim $tmpcpp
    fi
  done
done

