#! /bin/sh

HEADERS="config"

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

PDIRS="$(cd $SRCDIR; find . -maxdepth 1 ! -name include ! -path . -type d -exec basename {} \; | LC_ALL=C sort)"

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
}

if [ "$1" = 'enable' ]; then
	shift
	dir=$1
	on=$2
	shift
	shift
	req_on=$on
	if [ "$on" = "yes" -a -f $SRCDIR/$dir/$CONFIGURE ]; then
			wd=`pwd`
			[ -d $DESTDIR/$dir ] || mkdir -p $DESTDIR/$dir
			cd $DESTDIR/$dir
			[ -f $CONFIGURE ] || ln -s $SRCDIR/$dir/$CONFIGURE $CONFIGURE
			[ -f Makefile.conf.in ] || [ ! -f $SRCDIR/$dir/Makefile.conf.in ] || ln -s $SRCDIR/$dir/Makefile.conf.in Makefile.conf.in
			echo "=== configuring in $dir"
			trap "echo ; exit 130" INT
			if [ ! -f ./configure ] && ! autoreconf -v ; then
				on="no"
			fi
			if [ ! -f ./configure ]; then
				on="no"
			else
				if ! ./configure ; then
					echo "Configuration for $dir failed, disabling"
					on="no"
				fi
			fi
			trap - INT
			cd $wd
	fi
	if test -d $SRCDIR/$dir ; then
			mkdir -p $DESTDIR/$dir/config
			echo $on > $DESTDIR/$dir/config/plugin_enable
	fi
	if [ "$req_on" != "$on" ]; then
		exit 1
	fi
	exit 0
fi

if [ "$1" = "" -o "$1" = "clean" ]; then
  gendummy clean
  rm -f $LIB $PINC $PCONF
  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

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


if [ "$PDIRS" != " " ]; then
  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 ] && \
	[ -f $SRCDIR/$d/Makefile ] && ([ ! -f $SRCDIR/$d/configure.ac ] || \
	[ -f $SRCDIR/$d/configure ]); then
      enable=`cat $DESTDIR/$d/$CONF/${PREF}enable`
    else
      enable=no
    fi
    # Plugin configure directories
    if [ -f $SRCDIR/$d/$CONFIGURE ]; then
      echo -n " $DESTDIR/$d" >> $PCONF
    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 special headers...
        for h in $HEADERS; do
          if [ -f $SRCDIR/$d/$CONF/$PREF$h.h ]; then
            echo "#include \"../$PLUG/$d/$CONF/$PREF$h.h\"" >>$INC/$PREF$h.h
          fi
        done
      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
