#! /bin/bash

HEADERS="config init close inte6 poll"

TOP=`dirname $0`
PLUG=plugin
SRCDIR=$TOP/src/$PLUG
DESTDIR=src/$PLUG
INC=$DESTDIR/include
PREF=plugin_
CONF=config
CONFIGURE=configure
LIB=plugin_libdirs
PINC=plugin_incdirs
PCONF=plugin_configure
PARSDIR=src/base/init

PDIRS=`cd $SRCDIR; find ./ -maxdepth 1 ! -name include -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 $SRCDIR/$d/$CONF/${PREF}enable ]; then
        if [ "`cat $DESTDIR/$d/$CONF/${PREF}enable`" = "replaced" ]; then
          echo "yes" >$DESTDIR/$d/$CONF/${PREF}enable
        fi
      fi
    done
  fi
}

if [ "$1" = 'enable' ]; then
	shift 
	while [ $# -ge 2 ]; do
		dir=$1
		on=$2
		shift
		shift
		if test -d $SRCDIR/$dir ; then
			mkdir -p $DESTDIR/$dir/config
			echo $on > $DESTDIR/$dir/config/plugin_enable
		fi
	done
	exit 0
fi

if [ "$1" = "" -o "$1" = "clean" ]; then
  gendummy clean
  restore_enable
  rm -f $LIB $PINC $PCONF $PARSDIR/parser.y $PARSDIR/lexer.l
  rm -rf $INC
fi

if [ "$1" = "clean" ]; then
  rm -f $DESTDIR/*/config/plugin_enable
  exit 0
fi

mkdir -p $INC

if [ "$1" = "" ]; then gendummy; fi
mkdir -p $PARSDIR
if [ "$1" = "" -o "$1" = "parser" ]; then cp -fp $TOP/$PARSDIR/parser.y.in $PARSDIR/parser.y; fi
if [ "$1" = "" -o "$1" = "lexer" ]; then cp -fp $TOP/$PARSDIR/lexer.l.in $PARSDIR/lexer.l; fi

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


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

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

  # ok, now we are safe

  if [ "$1" = "" ]; then rm -f $LIB; fi
  for d in $PDIRS; do
    # Test if this plugin is enabled
    if [ -f $DESTDIR/$d/$CONF/${PREF}enable ]; then
      enable=`cat $DESTDIR/$d/$CONF/${PREF}enable`
    else
      enable=no
    fi
    if [ "$enable" = "yes" ]; then
      if [ "$1" = "" ]; then
        # Plugin library direcories
        if [ -f $SRCDIR/$d/$CONF/${PREF}dirs ]; then
          for i in `cat $SRCDIR/$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
        # Plugin include directories
        if [ -f $SRCDIR/$d/$CONF/${PREF}incdirs ]; then
          for i in `cat $SRCDIR/$d/$CONF/${PREF}incdirs`; do
            if [ "$i" = "./" ]; then
              echo -n " $PLUG/$d" >>$PINC
            else
              echo -n " $PLUG/$d/$i" >>$PINC
            fi
          done
        fi
        # Plugin configure directories
        if [ -f $SRCDIR/$d/$CONFIGURE ]; then
          echo -n " $DESTDIR/$d" >> $PCONF
        fi
        # Plugin special headers...
        for h in $HEADERS; do
          if [ -f $SRCDIR/$d/$CONF/$PREF$h.h ]; then
            echo -E "#include \"../$PLUG/$d/$CONF/$PREF$h.h\"" >>$INC/$PREF$h.h
          fi
        done
      fi
      # Plugin parsers parts
      if [ "$1" = "" -o "$1" = "parser" ]; then
        if [ -f $SRCDIR/$d/$CONF/${PREF}parser ]; then
          $TOP/bisonpp.pl -y $PARSDIR/parser.y $SRCDIR/$d/$CONF/${PREF}parser >$PARSDIR/parser.y.tmp
          mv -f $PARSDIR/parser.y.tmp $PARSDIR/parser.y
        fi
      fi
      # Plugin lexer parts
      if [ "$1" = "" -o "$1" = "lexer" ]; then
        if [ -f $SRCDIR/$d/$CONF/${PREF}lexer ]; then
          $TOP/bisonpp.pl -l $PARSDIR/lexer.l $SRCDIR/$d/$CONF/${PREF}lexer >$PARSDIR/lexer.l.tmp
          mv -f $PARSDIR/lexer.l.tmp $PARSDIR/lexer.l
        fi
      fi
    fi
  done
  if [ "$1" = "" ]; then
    if [ -f $LIB ]; then
      echo "" >>$LIB
    fi
    if [ -f $PINC ]; then
      echo "" >>$PINC
    fi
    if [ -f $PCONF ]; then
      echo "" >>$PCONF
    fi
  fi
fi
