#!/bin/sh
# 
# Generic Software Wrappers Toolkit
#
# Configuration shell script
# 
# Copyright (C) 1999, 2000, 2001  Network Associates, Incorporated (NAI)
# All rights reserved.
#
# This file is part of the NAI Generic Software Wrappers Toolkit
# (GSWTK).  See ftp://ftp.tislabs.com/pub/wrappers/ for the latest
# distribution.
#
# The GSWTK is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# The GSWTK is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# Java(tm) is a registered trademark of Sun Microsystems, Inc.
#

if [ ! -d "`dirname $0`/CVS" ]; then
   release=true
else
   release=
fi

#
# Determine version information
#
if [ ! -f VERSION ]; then
   echo "`basename $0`: missing VERSION file";
   exit 1;
fi

toolkit_vers=`egrep "version [0-9.]*" VERSION | \
              sed -e 's/^.* \([0-9.]*\).*$/\1/' -e 's/\./ /'`;

if [ -z "$toolkit_vers" ]; then
   toolkit_vers=`egrep " [A-Z]* version" VERSION | \
                 sed -e 's/^.* \([A-Z]*\) version.*$/\1/'`;
   devel=true
fi

if [ -z "$toolkit_vers" ]; then
   echo "`basename $0`: unable to parse VERSION file";
   exit 1;
fi


#
# Display version and copyright information
#
cat VERSION

cat <<EOF

 Copyright (c) 1999, 2000, 2001  Network Associates, Incorporated
 All rights reserved.

 Redistribution and use are governed by the terms detailed in the
 license document ("LICENSE") included with the toolkit.
EOF

#
# Initialize configuration variables
#

# Linux kernel requirements... 
KERN_VERSIONS="131584 131840"
CPU_ARCH="i386 i486 i586 i686"
cfg_need_kernel_source="yes"; export cfg_need_kernel_source

. mak/base.cfg
. wr.mak/${cfg_osname}.cfg
. mak/make.cfg
. mak/perl.cfg
. mak/compiler.cfg
. mak/flex.cfg
. mak/bison.cfg
. mak/java.cfg

cfg_make_init
cfg_perl_init
cfg_compiler_init kernel "Kernel" KCC userc "C" CC usercxx "C++" CXX
cfg_flex_init
cfg_bison_init
cfg_java_init java javac javap javah jar

wrapcc_path=
binutils_path=
binutils_vers=
enable_dbintd=
ddk_build_path=
ddk_build_vers=
ddk_path=
bindir=${exec_prefix}/bin
sbindir=${exec_prefix}/sbin
datadir=${prefix}/share
sysconfdir=${prefix}/etc
localstatedir=${state_prefix}/gswtk
libdir=${exec_prefix}/lib
includedir=${prefix}/include
mandir=${prefix}/man
wrapdir=${prefix}/wrappers
classdir=${datadir}/classes

showdetect=0


#
# Command to display usage information
#
usage()
{
   echo "
Usage: `basename $0` [options]

  options:
    [long form]            [short form]

    --version              -v            Display version information
    --help                 -h            Display usage message

Directory and file names:
    --prefix=PREFIX                      Install architecture-indepedent files
                                         in PREFIX [$prefix]
    --exec-prefix=EPREFIX                Install architecture-dependent files
                                         in EPREFIX [$exec_prefix]
    --state-prefix=SPREFIX               Install modifiable state data in
                                         SPREFIX [$state_prefix]
    --bindir=DIR                         User executables in DIR [EPREFIX/bin]
    --sbindir=DIR                        System admin executables in DIR
                                         [EPREFIX/sbin]
    --datadir=DDIR                       Read-only architecture-indepedent
                                         data in DDIR [PREFIX/share]
    --sysconfdir=DIR                     Read-only single-machine data in DIR
                                         [PREFIX/etc]
    --localstatedir=DIR                  Modifiable single-machine data in
                                         STATEDIR [SPREFIX/gswtk]
    --libdir=DIR                         Object code libraries in DIR
                                         [EPREFIX/lib]
    --includedir=DIR                     C header files in DIR [PREFIX/include]
    --mandir=DIR                         Man documentation in DIR [PREFIX/man]
    --wrapdir=DIR                        Wrapper object code files in DIR
                                         [PREFIX/wrappers]
    --classdir=DIR                       Java class files in DIR [DDIR/classes]
    --kmoddir=DIR                        Kernel modules in DIR [$kmoddir]

Features and packages:
    --enable-dbintd                      Enable DBIntD support (Experimental)

"
}


############################
# Check command-line flags #
############################

while [ $# -gt 0 ]
do
   case $1 in
        --version|-v) exit 0;
                      ;;
           --help|-h) usage
                      exit 0;
                      ;;
     --showdetect|-s) showdetect=1
                      ;;
     --enable-dbintd) case $osname in
                         FreeBSD) osrel=`sysctl -n kern.osreldate`;
                                  if [ $osrel -lt 300000 ]; then
                                     nosupport "DBIntD on $osname" \
"The Toolkit only has DBIntD support for FreeBSD 3.x"
                                     exit 1;
                                  fi
                                  enable_dbintd=1
                                  ;;
                               *) nosupport "DBIntD on $osname" \
"The Toolkit only has DBIntD support for FreeBSD 3.x"
                                  exit 1;
                                  ;;
                      esac
                      ;;
          --prefix=*) nprefix=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
     --exec-prefix=*) nexec_prefix=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
    --state-prefix=*) nstate_prefix=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
          --bindir=*) nbindir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
         --sbindir=*) nsbindir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
         --datadir=*) ndatadir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
      --sysconfdir=*) nsysconfdir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
   --localstatedir=*) nlocalstatedir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
          --libdir=*) nlibdir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
      --includedir=*) nincludedir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
          --mandir=*) nmandir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
         --wrapdir=*) nwrapdir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
        --classdir=*) nclassdir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
         --kmoddir=*) nkmoddir=`echo "$1" | sed 's/^[^=]*=//'`
                      ;;
                   *) echo "
Unknown option: $1"
                      exit 1;
                      ;;
   esac
   shift
done


#######################
# Start configuration #
#######################

necho "
Configuring for $osname $osvers ";
if [ ! -z "$cygwin" ]; then
   necho "(w/Cygwin $cygwin tools) ";
fi
echo "system...
"

#
# Read in configuration information, if present
#
if [ -f ./configuration ]; then
   echo "
[ Reading configuration cache ]"
   . ./configuration
fi


#
# Modify installation paths if changed on command-line
#
if [ ! -z "$nprefix" ]; then
   prefix=${nprefix}
   datadir=${prefix}/share
   classdir=${datadir}/classes
   sysconfdir=${prefix}/etc
   includedir=${prefix}/include
   mandir=${prefix}/man
   wrapdir=${prefix}/wrappers
fi
if [ ! -z "$nexec_prefix" ]; then
   exec_prefix=${nexec_prefix}
   bindir=${exec_prefix}/bin
   sbindir=${exec_prefix}/sbin
   libdir=${exec_prefix}/lib
fi
if [ ! -z "$nstate_prefix" ]; then
   state_prefix=${nstate_prefix}
   localstatedir=${state_prefix}/gswtk
fi

if [ ! -z "$ndatadir" ]; then
   datadir=${ndatadir}
   classdir=${datadir}/classes
fi
if [ ! -z "$nclassdir" ]; then
   classdir=${nclassdir}
fi
if [ ! -z "$nsysconfdir" ]; then
   sysconfdir=${nsysconfdir}
fi
if [ ! -z "$nincludedir" ]; then
   includedir=${nincludedir}
fi
if [ ! -z "$nmandir" ]; then
   mandir=${nmandir}
fi
if [ ! -z "$nwrapdir" ]; then
   wrapdir=${nwrapdir}
fi
if [ ! -z "$nbindir" ]; then
   bindir=${nbindir}
fi
if [ ! -z "$nsbindir" ]; then
   sbindir=${nsbindir}
fi
if [ ! -z "$nlibdir" ]; then
   libdir=${nlibdir}
fi
if [ ! -z "$nkmoddir" ]; then
   kmoddir=${nkmoddir}
fi


#
# Check for necessary tools
#
echo "
[ Required Tools Check ]"

# check for bmake or gmake
cfg_make_detect

# Check for necessary compiler
if [ "$osname" = "Windows NT" ]; then
   # Windows NT/2000
   cfg_compiler_check kernel userc usercxx -- msvc 12.0.8.8168
else
   # Unix/Linux
   if  [ "$osname" = "FreeBSD" ]; then
      cfg_compiler_check kernel userc -- gcc 2.7.2.1
      cfg_compiler_check usercxx -- g++ 2.7.2.1
   elif [ "$osname" = "Linux" ]; then
      cfg_compiler_check kernel -- kgcc 1.1.1 1.1.2
   fi
   cfg_compiler_check kernel userc -- egcs 1.0.0 1.1.2
   cfg_compiler_check usercxx -- egcs++ 1.0.0 1.1.2
   cfg_compiler_check kernel userc -- gcc 2.95 2.96
   cfg_compiler_check usercxx -- g++ 2.95 2.96
fi
cfg_compiler_detect

# check for flex 2.5.x or better
cfg_flex_detect 2.5.0 2.5.4

# check for bison 1.24 or better
cfg_bison_detect 1.24 1.28

# check for perl 5.003 or better
cfg_perl_detect 5.003 5.6.0

# check for JDK 1.1.6 through 1.2.2
java_missing_msg="Warning: A system without java installed will not be able to utilize the
         Wrapper Viewer and associated Java(tm)-based tools. [See README.JAVA]";
if [ "$osname" = "FreeBSD" ]; then
   cfg_java_detect 1.1.6 1.2.2
else
   cfg_java_detect 1.1.8 1.3.0
fi

# check for GNU binutils 2.9.1 or better on Solaris
if [ "$osname" = "Solaris" ]; then
   if [ -z "$binutils_path" -o -z "$binutils_vers" ]; then
      binutils_path=`checktool as`;
      if [ $? -ne 0 ]; then
         nosupport "A system without GNU binutils installed" \
"Please install a supported version of the GNU binutils on your system.
We have tested the Wrappers Toolkit with the GNU binutils 2.9.1 and know that
these will work.  You must install a version of at least 2.9.1 in order to
utilize the Toolkit."
         exit 1;
      else
         if [ $showdetect -eq 1 ]; then
            echo "Checking GNU assembler:";
         fi
         for path in $binutils_path
         do
            if [ $showdetect -eq 1 ]; then
               necho "   > $path";
            fi
            binutils_output_string=`echo | $path -v -xc - 2>&1`;
            binutils_path=`echo "$binutils_output_string" | grep 'as -V' | awk '{print $1}'`;
            binutils_version_string=`echo "$binutils_output_string" | grep 'GNU assembler version '`;
            binutils_vers=`echo "$binutils_version_string" | sed 's/^GNU assembler version \([0-9][0-9.]*\) .*$/\1/'`;

            if [ ! -z "$binutils_vers" ]; then
               binutils_major=`echo $binutils_vers | cut -d. -f1`;
               binutils_minor=`echo $binutils_vers | cut -d. -f2`;
               binutils_rele=`echo $binutils_vers | cut -d. -f3`;

               if [ "$binutils_major" -ne 2 -o "$binutils_minor" -lt 9 -o "$binutils_rele" -lt 1 ]; then
                  if [ $showdetect -eq 1 ]; then
                     echo " [BADVERSION]";
                  fi
                  binutils_vers=""
               elif [ "$binutils_minor" -gt 9 -o "$binutils_rele" -gt 1 ]; then
                  if [ $showdetect -eq 1 ]; then
                     echo " [OK]";
                  fi
                  echo "Warning: only GNU binutils 2.9.1 have been tested as working with the Toolkit"
               else
                  if [ $showdetect -eq 1 ]; then
                     echo " [OK]";
                  fi
               fi
            else
               if [ $showdetect -eq 1 ]; then
                  echo " [INVALID]";
               fi
            fi
            if [ ! -z "$binutils_vers" ]; then
               binutils_path="$path"
               echo "GNU assembler $binutils_vers found. [$binutils_path]";
               break;
            fi
         done
         if [ -z "$binutils_vers" ]; then
            nosupport "$binutils_version_string
" \
"Please install a supported version of the GNU binutils on your system.
We have tested the Wrappers Toolkit with the GNU binutils 2.9.1 and know that
these will work.  You must install a version of at least 2.9.1 in order to
utilize the Toolkit."
            exit 1;
         fi
      fi
   else
      echo "GNU assembler $binutils_vers found. [$binutils_path] -- cached";
   fi
fi

# On Windows NT, check for DDK
if [ "$osname" = "Windows NT" ]; then
   if [ -z "$ddk_path" ]; then
      ddk_build_path=`checktool build`;
      if [ $? -ne 0 ]; then
         echo \
"Warning: you must install the DDK in order to use the ring protection
         system included with the Windows port of the GSWTK."
      else
         if [ $showdetect -eq 1 ]; then
            echo "Checking DDK:";
         fi
         for path in $ddk_build_path
         do
            if [ $showdetect -eq 1 ]; then
               necho "   > $path";
            fi
            ddk_build_vers=`$path -? 2>&1 | grep 'BUILD: Version' | awk '{print $3}'`;
            ddk_build_dirpath=`dirname $path`;

            if [ ! -z "$ddk_vers" ]; then
               ddk_build_major=`echo $ddk_vers | cut -d. -f1`;
               ddk_build_minor=`echo $ddk_vers | cut -d. -f2`;
               ddk_build_rele=`echo $ddk_vers | cut -d. -f3`;

               if [ "$ddk_build_major" -ne 4 -o "$ddk_build_minor" -lt 2 -o "$ddk_build_rele" -lt 1371 ]; then
                  if [ $showdetect -eq 1 ]; then
                     echo " [BADVERSION]";
                  fi
                  ddk_build_vers=""
               else
                  if [ $showdetect -eq 1 ]; then
                     echo " [OK]";
                  fi
               fi
            else
               if [ $showdetect -eq 1 ]; then
                  echo " [INVALID]";
               fi
            fi
            if [ ! -z "$ddk_build_vers" ]; then
               ddk_path=`dirname $ddk_build_dirpath`;
               echo "Windows DDK Build $ddk_build_vers found. [$ddk_path]";
               break;
            fi
         done
         if [ -z "$ddk_build_vers" ]; then
            echo \
"Warning: you must install the DDK in order to use the ring protection
         system included with the Windows port of the GSWTK."
         fi
      fi
   else
      echo "Windows DDK Build $ddk_build_vers found. [$ddk_path] -- cached";
   fi
fi


#
# Check for platform quirks
#
echo "
[ Platform Quirks Check ]"
if [ "$osname" = "Linux" ]; then
   if [ -z "$linux_kill_fasync_nargs" ]; then
      cat >testfasync.c <<EOF
#define __KERNEL__ 1
#include <linux/fs.h>
#include <linux/signal.h>

int main() { 
    struct fasync_struct *sigq;
    kill_fasync(sigq, SIGIO);
}
EOF
      if $compiler_kernel_path testfasync.c -o /dev/null 2>&1 | grep "too few arguments" >/dev/null; then
         linux_kill_fasync_nargs=3;
      else
         linux_kill_fasync_nargs=2;
      fi
      rm -f testfasync.c

      echo "kill_fasync requires $linux_kill_fasync_nargs arguments.";
   else
      echo "kill_fasync requires $linux_kill_fasync_nargs arguments. -- cached";
   fi
fi


#
# Convert paths to short names on Windows NT
#

if [ "$osname" = "Windows NT" ]; then
   wrapcc_path=`longtoshortwindows ${compiler_kernel_path}.EXE|sed 's/\\\\/\\\\\\\\\\\\\\\\/g'`
   conf_wrapcc_path=`echo "${wrapcc_path}"|sed 's/.exe$//'`
   if [ ! -z "$ddk_path" ]; then
      ddk_path=`longtoshort $ddk_path`
      ddk_path=`cygpath -w "$ddk_path" | tr '\134' '/'`
   fi
else
   wrapcc_path=$compiler_path
   conf_wrapcc_path=${wrapcc_path}
fi


#
# Display installation paths
#
echo "
[ Installation paths are as follows ]"
cat <<EOF

                  prefix: $prefix
             exec prefix: $exec_prefix
            state prefix: $state_prefix
           bin directory: $bindir
          sbin directory: $sbindir
          data directory: $datadir
   system conf directory: $sysconfdir
   local state directory: $localstatedir
           lib directory: $libdir
       include directory: $includedir
           man directory: $mandir
      wrappers directory: $wrapdir
   class files directory: $classdir
kernel modules directory: $kmoddir

EOF


#
# Write out cache information
#
echo "
[ Writing out configration cache ]"
echo "#" > ./configuration
cat VERSION | sed 's/^/# /' >>./configuration
cat <<EOF >>./configuration
#
# Cached configuration for $osname $osvers
#
osdirectory=$osdir
binutils_path="$binutils_path"
binutils_vers="$binutils_vers"
enable_dbintd="$enable_dbintd"
ddk_build_vers="$ddk_build_vers"
ddk_path="$ddk_path"
EOF

if [ "$osname" = "Linux" ]; then
   cat <<EOF >>./configuration
linux_kill_fasync_nargs=$linux_kill_fasync_nargs
EOF
fi

cfg_make_write_cache >>./configuration
cfg_perl_write_cache >>./configuration
cfg_compiler_write_cache >>./configuration
cfg_flex_write_cache >>./configuration
cfg_bison_write_cache >>./configuration
cfg_java_write_cache >>./configuration

cat <<EOF >>./configuration
prefix="$prefix"
exec_prefix="$exec_prefix"
state_prefix="$state_prefix"
bindir="$bindir"
sbindir="$sbindir"
datadir="$datadir"
sysconfdir="$sysconfdir"
localstatedir="$localstatedir"
libdir="$libdir"
includedir="$includedir"
mandir="$mandir"
wrapdir="$wrapdir"
classdir="$classdir"
kmoddir="$kmoddir"
EOF


#
# Generate config.mak
#
echo "
[ Generating config.mak ]"
cat <<EOF >mak/config.mak
#
# config.mak
#
#    File automatically generated by configure script, do not edit!
#
GSWTK_BUILD=yes

CPU=$cpu
OSDIRECTORY=$osdir
GSWTK_CONFIG_MACH=$osmach

WRAPCC=$wrapcc_path

EOF

if [ "$osname" = "Windows NT" ]; then
   pwd=`pwd`
   if [ ! "$cygwin" = "Release 1.0" ]; then
      bison_path="env BISON_SIMPLE=$pwd/misc/bison.simple $bison_path"
      export bison_path
   else
      echo "YACC_FLAGS=-S $pwd/misc/bison.simple
" >> mak/config.mak
   fi
fi

cfg_make_write_conf >>mak/config.mak
cfg_perl_write_conf >>mak/config.mak
cfg_compiler_write_conf >>mak/config.mak
cfg_flex_write_conf >>mak/config.mak
cfg_bison_write_conf >>mak/config.mak

if [ ! -z "$java_path" ]; then
   cfg_java_write_conf >>mak/config.mak
   if [ -z "$config_subdir" ]; then
      config_subdir="\${JAVA_SUBDIR}"
   else
      config_subdir="${config_subdir} \${JAVA_SUBDIR}"
   fi
fi

if [ ! -z "$ddk_path" ]; then
   cat <<EOF >>mak/config.mak
DDKPATH=$ddk_path
EOF
   if [ -z "$config_subdir" ]; then
      config_subdir="\${DRIVER_SUBDIR}"
   else
      config_subdir="${config_subdir} \${DRIVER_SUBDIR}"
   fi
fi

if [ ! -z "${PERLLIB}" ]; then
   perllib="${PERLLIB}\${PATHSEP}\${TOPDIR}/mak/perl\${PATHSEP}\${TOPDIR}/toolkits/nailabs-ptk/modules";
else
   perllib="\${TOPDIR}/mak/perl\${PATHSEP}\${TOPDIR}/toolkits/nailabs-ptk/modules";
fi

cat <<EOF >>mak/config.mak
PERLLIB=$perllib

CONFIG_SUBDIR=$config_subdir

EOF

lcname=`echo $osname | tr '[A-Z]' '[a-z]' | tr ' ' '_'`;
echo "CFLAGS += -DEFS_SYSTEM_HDR=\"\\\"${lcname}/eventfs_${lcname}.h\\\"\"" >> mak/config.mak
echo "CXXFLAGS = -DEFS_SYSTEM_HDR=\"\\\"${lcname}/eventfs_${lcname}.h\\\"\"" >> mak/config.mak
case "$osname" in
         Solaris) echo 'HASEVENTFS=fs/eventfs
' >> mak/config.mak
                  ;;
   FreeBSD|Linux) echo 'HASEVENTFS=eventfs
' >> mak/config.mak
                  ;;
esac

if [ "$osname" = "Linux" ]; then
   if [ "$linux_kill_fasync_nargs" = "3" ]; then
      echo 'CFLAGS += -DPD_LINUX_KILL_FASYNC_NEED_3_ARGS
' >> mak/config.mak
   fi
fi

if [ ! -z "$enable_dbintd" ]; then
   echo 'EXTENSION_DBINTD=dbintd
' >> mak/config.mak
fi

if [ -z "$release" ]; then
   echo "OBJDIR=\${TOPDIR}/../obj-${osname}-${osvers}" >>mak/config.mak
   echo 'COPYBINDIR=${TOPDIR}/../bin
' >>mak/config.mak
else
   echo "OBJDIR=\${TOPDIR}/obj-${osname}-${osvers}" >>mak/config.mak
   echo 'COPYBINDIR=${TOPDIR}/bin
' >>mak/config.mak
fi

cat <<EOF >>mak/config.mak

#
# Set install paths
#
PREFIX=$prefix
EPREFIX=$exec_prefix
SPREFIX=$state_prefix
BINDIR=$bindir
SBINDIR=$sbindir
DATADIR=$datadir
SYSCONFDIR=$sysconfdir
LOCALSTATEDIR=$localstatedir
LIBDIR=$libdir
INCDIR=$includedir
MANDIR=$mandir
WRAPDIR=$wrapdir
CLASSDIR=$classdir
KMODDIR=$kmoddir

#
# Set defaults
#
INSTALLSUBDIR=\${SUBDIR}
RELEASESUBDIR=\${SUBDIR}

#
# Include appropriate .mak files
#
include \${TOPDIR}/wr.mak/wss-options.mak
include \${TOPDIR}/mak/default.mak
include \${TOPDIR}/wr.mak/default.mak

EOF

#
# Generate wr_configuration.h
#
echo "
[ Generating wr_configuration.h ]"

if [ "$devel" = "true" ]; then
   toolkit_ver_major=0
   toolkit_ver_minor=0
   toolkit_ver_string="$toolkit_vers"
else
   toolkit_ver_major=`echo $toolkit_vers | awk '{ print $1; }'`;
   toolkit_ver_minor=`echo $toolkit_vers | awk '{ printf("%d%-3.3d", $2, $3); }'`
   toolkit_ver_string=`echo $toolkit_vers | awk '{ printf("%d.%d.%d", $1, $2, $3); }'`
fi

if [ "$osname" = "Windows NT" ]; then
   prefix=`cygpath -p -w "$prefix"|sed 's/\\\\/\\\\\\\\/g'`
   exec_prefix=`cygpath -p -w "$exec_prefix"|sed 's/\\\\/\\\\\\\\/g'`
   state_prefix=`cygpath -p -w "$state_prefix"|sed 's/\\\\/\\\\\\\\/g'`
   bindir=`cygpath -p -w "$bindir"|sed 's/\\\\/\\\\\\\\/g'`
   sbindir=`cygpath -p -w "$sbindir"|sed 's/\\\\/\\\\\\\\/g'`
   datadir=`cygpath -p -w "$datadir"|sed 's/\\\\/\\\\\\\\/g'`
   sysconfdir=`cygpath -p -w "$sysconfdir"|sed 's/\\\\/\\\\\\\\/g'`
   localstatedir=`cygpath -p -w "$localstatedir"|sed 's/\\\\/\\\\\\\\/g'`
   libdir=`cygpath -p -w "$libdir"|sed 's/\\\\/\\\\\\\\/g'`
   includedir=`cygpath -p -w "$includedir"|sed 's/\\\\/\\\\\\\\/g'`
   mandir=`cygpath -p -w "$mandir"|sed 's/\\\\/\\\\\\\\/g'`
   wrapdir=`cygpath -p -w "$wrapdir"|sed 's/\\\\/\\\\\\\\/g'`
   classdir=`cygpath -p -w "$classdir"|sed 's/\\\\/\\\\\\\\/g'`
   kmoddir=`cygpath -p -w "$kmoddir"|sed 's/\\\\/\\\\\\\\/g'`
fi

cat <<EOF >include/wr_configuration.h
/*
 * wr_configuration.h
 *
 * File automatically generated by configure script, do not edit!
 *
 */

#ifndef __WR_CONFIGURATION_H
#define __WR_CONFIGURATION_H 1

#define OSDIRECTORY $osdir
#define GSWTK_CONFIG_CPU $osmach

/* XXX this will need to be determined */
#define GSWTK_CONFIG_RANDOM_BITS 31

#define WR_VERSION_MAJOR  $toolkit_ver_major
#define WR_VERSION_MINOR  $toolkit_ver_minor
#define WR_VERSION_STRING "$toolkit_ver_string"

#define CONF_PREFIX        "$prefix"
#define CONF_EPREFIX       "$exec_prefix"
#define CONF_SPREFIX       "$state_prefix"
#define CONF_BINDIR        "$bindir"
#define CONF_SBINDIR       "$sbindir"
#define CONF_DATADIR       "$datadir"
#define CONF_SYSCONFDIR    "$sysconfdir"
#define CONF_LOCALSTATEDIR "$localstatedir"
#define CONF_LIBDIR        "$libdir"
#define CONF_INCDIR        "$includedir"
#define CONF_MANDIR        "$mandir"
#define CONF_WRAPDIR       "$wrapdir"
#define CONF_CLASSDIR      "$classdir"
#define CONF_KMODDIR       "$kmoddir"

#endif /* __WR_CONFIGURATION_H */
EOF

if [ "$osname" = "Windows NT" ]; then
   #
   # Generate gswtk.reg
   #
   echo "
[ Generating gswtk.reg ]"

   cat <<EOF > gswtk.reg
REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows]
AppInit_DLLs="lwm.ini"

[HKEY_LOCAL_MACHINE\SOFTWARE\NAI Labs\Wrappers]
"DBSize"=dword:00000010
"Home"="$localstatedir"
"DBPath"="$localstatedir\\wss.db"
"LogPath"="$localstatedir\\wss.log"
EOF
else
   #
   # Generate configuration.h
   #
   echo "
[ Generating wss.conf ]"

   cat <<EOF > wss.conf
#
# wss.conf - Configuration file for GSWTK
#
home = $localstatedir
dbpath = ${localstatedir}/wss.db
logpath = ${localstatedir}/wss.log
incpath = ${includedir}/gswtk
wssconf = ${sysconfdir}/wss.conf
wrapdir = ${wrapdir}
binpath = ${bindir}
sbinpath = ${sbindir}
EOF
fi


if [ ! -z "$java_path" -a -z "$java_home" ]; then
   echo "
Warning: unable to determine Java(tm) home directory, please edit the generated
         config.mak file in the mak directory and set JAVAHOME to the root of
         the JDK directory."
fi


#
# Do system-specific configuration in the tree
#
if [ "$osname" = "Solaris" -a ! "${OS}${OSTYPE}" = "solaris" ]; then
   (cd mak; rm -f "${OS}${OSTYPE}.mak"; ln -s solaris.mak "${OS}${OSTYPE}.mak");
   (cd wr.mak; rm -f "${OS}${OSTYPE}.mak"; ln -s solaris.mak "${OS}${OSTYPE}.mak");
fi
if [ "$osname" = "Linux" -a ! "${OS}${OSTYPE}" = "Linux" ]; then
   (cd mak; rm -f "${OS}${OSTYPE}.mak"; ln -s Linux.mak "${OS}${OSTYPE}.mak");
   (cd wr.mak; rm -f "${OS}${OSTYPE}.mak"; ln -s Linux.mak "${OS}${OSTYPE}.mak");
fi


#
# All set!
#
cat <<EOF

Configuration complete.

EOF


#
# Exit to allow user to 'fix' things if need arises
#
if [ ! -z "$java_path" -a -z "$java_home" ]; then
   exit 1;
fi
if [ "$osname" = "Windows NT" ]; then
   if [ ! "$MAKE_MODE" = "UNIX" ]; then
      echo "
NOTICE: You will need to set a variable in your environment in order for
        make to work properly with the makefiles included in this package.

        At the command-prompt type the following:
        SET MAKE_MODE=UNIX

        Or set MAKE_MODE to UNIX in the System Control Panel.

        If you do not want to do either, you can always run make with the
        --unix flag and things will work properly.
"
      exit 1;
   fi
fi

