#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/xemacspath                  */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Fri Jun 19 08:07:57 1998                          */
#*    Last change :  Thu Jan  3 10:08:13 2002 (serrano)                */
#*    -------------------------------------------------------------    */
#*    We search for the xemacs site-lisp path.                         */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
xemacs=xemacs
cc=gcc
path=
exe_suffix=
tmp=/tmp
user=bigloo

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --user=*)
      user="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cc=*|-cc=*)
      cc="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --xemacs=*|-xemacs=*)
      xemacs="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --path=*|-path=*)
      path="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --exe=*|-exe=*)
      exe_suffix="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --tmp=*|-tmp=*)
      tmp="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$tmp/actest$user
aout=$tmp/Xactest$user$exe_suffix

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
main( argc, argv )
int   argc;
char *argv[];
{
   printf( "%s", argv[ 1 ] );
   return 0;
}
EOF

$cc $cflags $file.c -o $aout >/dev/null 2> /dev/null


if [ ! "$path " = " " ]; then
   $aout $path
else
   path=`sh -c "$xemacs -batch -q -eval \"(princ (car load-path))\" -kill" 2> /dev/null`

   if [ ! "$status" = "0" ]; then
      if [ "$path" = "nil" ]; then
         $aout ""
      else
         # we always add a trailing / char
         case $path in
            */)
              ;;

            *)
              path="$path"/;;
         esac
            $aout $path
      fi
   else
      $aout ""
   fi
fi

/bin/rm -rf $file.c $file.o $aout $aout*
   
