#!/bin/sh

if [ "x$DEBUG" = "xyes" ]; then
  set -x
fi

DOXYOUT=${1:-doc/doxygen/Doxyfile.in}
OUTDIR=`dirname "$DOXYOUT"`
DOXYDIR="doc/doxygen/"

find_doxystyle()
{
  for i in `dirname $0`/../doc/doxygen/Doxystyle `dirname $0`/../share/dune-common/doc/doxygen/Doxystyle; do
    if [ -f "$i" ]; then
      export DOXYSTYLE=$i
      return 0
    fi
  done
  return 1
}

test_doxylocal()
{
  while [ -n "$1" ]; do
    if [ -f "$1/dune.module" ]; then
      return 0
    fi
    shift
  done
  return 1
}

parse_doxyfile()
{
  # Doxylocal is used _as is_
  if [ "$2" = "Doxylocal" ]; then
    cat $1/$DOXYDIR/$2
    return
  fi

  local FILE=$1/$DOXYDIR/$2
  local FOUND=0
  local line

  # find lines that match the pattern
  set `grep -n '^ *\(INPUT\|EXAMPLE_PATH\|IMAGE_PATH\|PREDEFINED\|EXCLUDE\|EXAMPLE_PATTERNS\) *[+=]' $FILE | cut -d: -f1`

  # search lines in $@ and proceeding lines
  grep -n . $FILE |
  sed -e 's/\\/\\\\/g' |
  while read line; do
    if [ "${line%%:*}" -eq "${1:-0}" ]; then
      FOUND=1
      # subst = by += if necessary
      start="${line%%=*}"
      case "$start" in
        *+) ;;
        *)  line="$start+=${line#*=}" ;;
      esac
      shift
    fi
    if [ $FOUND -eq 1 ]; then
      echo "$line"
    else
      continue
    fi
    # check for trailing '\'
    case "$line" in
      *\\) FOUND=1 ;;
      *)   FOUND=0 ;;
    esac
  done | sed -e 's/^[0-9]*://'
}

parse_doxylocal()
{
  if echo $1 | grep -q "^/"; then
    srcdir=$1/$DOXYDIR
    top_srcdir=$1
    parse_doxyfile $1 $2 | sed -e "s!@\(abs_\)*srcdir@!$srcdir!" -e "s!@\(abs_\)*top_srcdir@!$top_srcdir!";
  else
    parse_doxyfile $1 $2
  fi
}

get_module_name()
{
  grep "^Module:" $1/dune.module | sed -e 's/^Module: *//'
}

generate_doxyout()
{
  echo "Generating $DOXYOUT from "
  echo "    global style"
  cat $DOXYSTYLE > $DOXYOUT
  while [ -n "$1" ]; do
    for DOXY in Doxylocal Doxyfile.in Doxyfile; do
	  if [ "$1/$DOXYDIR/$DOXY" -ef "$DOXYOUT" ]; then continue; fi
      if [ -f "$1/$DOXYDIR/$DOXY" ]; then
        echo "    and `get_module_name $1` config"
        parse_doxylocal $1 $DOXY >> $DOXYOUT
        break
      fi
    done
    shift
  done
  echo "    ... done"
}

generate_doxylocal()
{
  echo "Generating $DOXYOUT from "
  for DOXY in Doxyfile.in Doxyfile; do
    if [ -f "$1/$DOXYDIR/$DOXY" ]; then
      echo "    `get_module_name .` $DOXY"
      parse_doxylocal . $DOXY > $DOXYOUT
      break
    fi
  done
  echo "    ... done"
}

# make sure we are in dune module
if ! [ -f dune.module ]; then
  echo "Error: dunedoxynize must be called from the top_srcdir of your module"
  exit 1
fi

# don't do anything if we have old style Doxyfile or Doxyfile.in, but no Doxylocal
if [ $# -eq 0 ] && ! [ -f "$DOXYDIR/Doxylocal" ]; then
  for DOXY in Doxyfile.in Doxyfile; do
    if [ -f "$DOXYDIR/$DOXY" ]; then
      echo "Warning: module still uses handwritten $DOXY"
      echo "         You can transform it to an initial Doxylocal by calling"
      echo "         dune-common/bin/dunedoxynize doc/doxygen/Doxylocal ."
      echo "         in the top source directory of this module"
      exit 0
    fi
  done
  exit 0
fi

# quit if your module doesn't have any documentation
if ! [ -d "$OUTDIR" ]; then
  if [ $# -eq 0 ]; then
    exit 0
  else
    echo "Error: output directory $OUTDIR does not exist."
    exit 0
  fi
fi

# search for doxygen style
if ! find_doxystyle; then
  echo "Error: dunedoxynize global style not found"
  exit 1
fi

# drop the parameter of the output file
if [ $# -gt 0 ]; then
  shift
fi

# make sure that there is at least one Doxylocal file
if ! test_doxylocal "${@:-.}" && [ $# -gt 0 ]; then
  echo "Error: you didn't supply any valid Doxylocal file"
  exit 1
fi

if [ "`basename $DOXYOUT`" = "Doxylocal" ]; then
  generate_doxylocal "${@:-.}"
else
  generate_doxyout "${@:-.}"
fi
