#! /bin/bash

HEADERS="config init close inte6 ioselect poll"

PLUG=plugin
DIR=src/$PLUG
INC=src/include
PREF=plugin_
CONF=config
LIB=plugin_libdirs
PINC=plugin_incdirs
PARSDIR=src/base/init

PDIRS=`cd $DIR; find ./ -maxdepth 1 -type d -printf ' %f'`

function gendummy {
  for i in $HEADERS; do
    if [ "$1" = "clean" ]; then
      rm -f $INC/$PREF$i.h
    else
      echo -n "" >$INC/$PREF$i.h
    fi
  done
}

function restore_enable {
  if [ "$PDIRS" != " " ]; then
    for d in $PDIRS; do
      if [ -f $DIR/$d/$CONF/${PREF}enable -a "`cat $DIR/$d/$CONF/${PREF}enable`" = "replaced" ]; then
        echo "yes" >$DIR/$d/$CONF/${PREF}enable
      fi
    done
  fi
}

gendummy clean
restore_enable
rm -f $LIB $PINC $PARSDIR/{parser.y,lexer.l}
if [ "$1" = "clean" ]; then
  exit 0
fi

gendummy
cp -fp $PARSDIR/parser.y.in $PARSDIR/parser.y
cp -fp $PARSDIR/lexer.l.in $PARSDIR/lexer.l

if [ ! -d $DIR ]; then
  exit 1
fi


if [ "$PDIRS" != " " ]; then

  # Check for plugins which override default ones.
  # Overriding plugins will disable the clashing plugin
  for d in $PDIRS; do
    if [ -f $DIR/$d/$CONF/${PREF}enable -a "`cat $DIR/$d/$CONF/${PREF}enable`" = "yes" ]; then
      if [ -f $DIR/$d/$CONF/${PREF}override ]; then
        for oplugin in `cat $DIR/$d/$CONF/${PREF}override`; do
          if [ "$oplugin" != "" -a -d $DIR/$oplugin ]; then
            if [ -f $DIR/$oplugin/$CONF/${PREF}enable -a "`cat $DIR/$oplugin/$CONF/${PREF}enable`" = "yes" ]; then
              echo "replaced" >$DIR/$oplugin/$CONF/${PREF}enable
            fi
          fi
        done
      fi
    fi
  done

  # ok, now we are safe

  rm -f $LIB
  for d in $PDIRS; do
    if [ -f $DIR/$d/$CONF/${PREF}enable ]; then
      enable=`cat $DIR/$d/$CONF/${PREF}enable`
    else
      enable=no
    fi
    if [ "$enable" = "yes" ]; then
      if [ -f $DIR/$d/$CONF/${PREF}dirs ]; then
        for i in `cat $DIR/$d/$CONF/${PREF}dirs`; do
          if [ "$i" = "./" ]; then
            echo -n " $PLUG/$d" >>$LIB
          else
            echo -n " $PLUG/$d/$i" >>$LIB
          fi
        done
      else
        echo -n " $PLUG/$d" >>$LIB
      fi
      if [ -f $DIR/$d/$CONF/${PREF}incdirs ]; then
        for i in `cat $DIR/$d/$CONF/${PREF}incdirs`; do
          if [ "$i" = "./" ]; then
            echo -n " $PLUG/$d" >>$PINC
          else
            echo -n " $PLUG/$d/$i" >>$PINC
          fi
        done
      fi
      if [ -f $DIR/$d/$CONF/${PREF}parser ]; then
        ./bisonpp.pl -y $PARSDIR/parser.y $DIR/$d/$CONF/${PREF}parser >$PARSDIR/parser.y.tmp
        mv -f $PARSDIR/parser.y.tmp $PARSDIR/parser.y
      fi
      if [ -f $DIR/$d/$CONF/${PREF}lexer ]; then
        ./bisonpp.pl -l $PARSDIR/lexer.l $DIR/$d/$CONF/${PREF}lexer >$PARSDIR/lexer.l.tmp
        mv -f $PARSDIR/lexer.l.tmp $PARSDIR/lexer.l
      fi
      for h in $HEADERS; do
        if [ -f $DIR/$d/$CONF/$PREF$h.h ]; then
          echo -E "#include \"../$PLUG/$d/$CONF/$PREF$h.h\"" >>$INC/$PREF$h.h
        fi
      done
    fi
  done
  if [ -f $LIB ]; then
    echo "" >>$LIB
  fi
  if [ -f $PINC ]; then
    echo "" >>$PINC
  fi
fi
