#!/bin/sh

USE_GNOME=1
USE_GUI=1
USE_SCRIPT=1
PREFIX=/usr/local

for p in $1 $2 $3 $4 $5 $6 $7 $8 $9 ; do
if [ $p = --with-gnome ]; then
  USE_GNOME=1
elif [ $p = --without-gnome ]; then
  USE_GNOME=0
elif [ $p = --with-gui ]; then
  USE_GUI=1
elif [ $p = --without-gui ]; then
  USE_GUI=0
elif [ $p = --with-script ]; then
  USE_SCRIPT=1
elif [ $p = --without-script ]; then
  USE_SCRIPT=0
elif [ `echo $p|sed s/=.*//` = --prefix ]; then
    PREFIX=`echo $p|sed s/^[^=]*=//`
fi
done

echo PREFIX=$PREFIX > config.h
if [ $USE_GUI = 0 ]; then
  echo gui_cflags=`gtk-config --cflags` >> config.h
elif [ $USE_GNOME = 1 ]; then
  if [ -f /usr/local/gnome/lib/gnomeConf.sh ]; then
    GNOME_PATH=/usr/local/gnome
  elif [ -f /opt/gnome/lib/gnomeConf.sh ]; then
    GNOME_PATH=/opt/gnome
  elif [ -f /usr/lib/gnomeConf.sh ]; then
    GNOME_PATH=/usr
  else
    GNOME_PATH=`locate gnomeConf.sh|grep lib/gnomeConf.sh$|sed s/.lib.gnomeConf.sh$//`
  fi
  . $GNOME_PATH/lib/gnomeConf.sh
  echo USE_GUI=1 >> config.h
  echo USE_GNOME=1 >> config.h
  echo gnome_path=$GNOME_PATH >> config.h
  echo gnome_lflags=$GNOME_LIBDIR $GNOMEUI_LIBS >> config.h
  echo gnome_cflags=$GNOME_INCLUDEDIR >> config.h
elif [ $USE_GUI = 1 ]; then
  echo USE_GUI=1 >> config.h
  echo gui_cflags=`gtk-config --cflags` >> config.h
  echo gui_lflags=`gtk-config --libs` >> config.h
fi

if [ $USE_GUI = 1 ]; then
  if [ `gtk-config --version|sed s/\\\..*//` != 1 ]; then
    echo "GTK+ 1.0.0 or later not found"
    rm -f config.h
    exit 1
  fi
fi

if [ $USE_SCRIPT = 1 ]; then
  SCRIPT_PATH=`locate CORE|grep /CORE$|grep perl5`
  echo USE_SCRIPT=1 >> config.h
  echo perl_path=$SCRIPT_PATH >> config.h
fi

echo "int main(int argc, char *argp[]) { return 0; }" > yagconfig.test.c
rm -f yagconfig.test
gcc yagconfig.test.c -o yagconfig.test
if [ -x yagconfig.test ]; then
  echo cc=gcc >> config.h
else
  cc yagconfig.test.c -o yagconfig.test
  echo cc=cc >> config.h
fi

if [ ! -x yagconfig.test ]; then
  echo "C compiler doesn't work?"
  rm -f config.h
  exit 1
fi

if [ `ldd yagconfig.test|grep libc|sed s/\ =.*//` = libc.so.6 ]; then
  echo USE_CRYPT=1 >> config.h
fi

rm -f yagconfig.test yagconfig.test.c

echo "Configure OK."
