#!/bin/sh
# **********************************************************************
#
# Copyright (c) 1999
# Object Oriented Concepts, Inc.
# Billerica, MA, USA
#
# All Rights Reserved
#
# **********************************************************************

#
# Remove echo incompatibilities
#
if test "`echo -e xxx`" = "xxx"
then
    echo="echo -e"
else
    echo=echo
fi

#
# Unset everything that starts with "config_" and "env_"
#
for i in `set | grep "^config_[a-z_]*=" | sed -e 's/=.*$//'`;
do
    unset $i
done
for i in `set | grep "^env_[A-Z_]*=" | sed -e 's/=.*$//'`;
do
    unset $i
done

#
# Read default configuration
#
if test ! -f "./config/defaults"
then
    $echo "$0: error: can't load \`./config/defaults'"
    exit
fi

. ./config/defaults

#
# Read current configuration
#
if test -f "./config/current"
then
    . ./config/current
fi

#
# Print welcome message
#
$echo
$echo "************************"
$echo "* OOC C++ Configurator *"
$echo "************************"

#
# Ask for shell type
#
$echo
input="x"
while test -n "$input" -a "$input" != "b" -a "$input" != "c"
do
    $echo "Enter 'c' if you use a C shell, or 'b' for a bourne shell:"
    $echo "[$config_shell] \c"
    read input
done
if test -n "$input"
then
    config_shell="$input"
fi

#
# Check if an ORBacus program is being configured
#
if test -d ob -o -d naming -o -d property -o -d event -o -d ssl -o -d obtcl -o -d notify
then
    orbacus="yes"
else
    orbacus="no"
fi

#
# Ask for platform and C++ compiler
#
$echo
$echo "Please select from the following C++ / operating system combinations:"
$echo
cpp_sgi_72=1
$echo "(1) SGI C++ 7.2.x or 7.3.x              SGI IRIX 6.2, 6.3 or 6.5"
cpp_sun=2
$echo "(2) SUN C++ 4.2 or 5.0                  SUN Solaris 2.5, 2.6 or 2.7"
cpp_hp=3
$echo "(3) HP aC++ A.01.12                     HP-UX B.10.20"
cpp_aix=4
$echo "(4) AIX C Set ++ xlC 3.1.4.6            AIX Version 4.2.1"
cpp_dec=5
$echo "(5) DEC C++ 6.2-024                     DEC OSF/1 V4.0"
cpp_gcc_28=6
$echo "(6) EGCS 1.0.3 or GCC 2.8.1             Any supported OS"
cpp_egcs_11=7
$echo "(7) EGCS 1.1.x                          Any supported OS"
cpp_gcc_295=8
$echo "(8) GCC 2.95.x                          Any supported OS"

cpp_last=$cpp_gcc_295

$echo
input=0
while test -n "$input" -a \( "0$input" -gt $cpp_last -o "0$input" -lt 1 \)
do
    $echo "Please choose your platform/compiler combination:"
    $echo "[$config_platform] \c"
    read input
done
if test -n "$input"
then
    config_platform="$input"
fi

#
# Ask for shared libraries
#
$echo
input="x"
while test -n "$input" -a "$input" != "yes" -a "$input" != "no"
do
    $echo "Do you want to create shared libraries?"
    $echo "[$config_shared] \c"
    read input
done
if test -n "$input"
then
    config_shared="$input"
fi

#
# Ask for optimized code
#
$echo
input="x"
while test -n "$input" -a "$input" != "yes" -a "$input" != "no"
do
    $echo "Do you want optimized code to be generated?"
    $echo "[$config_optimized] \c"
    read input
done
if test -n "$input"
then
    config_optimized="$input"
fi

#
# Ask for debug code
#
$echo
input="x"
while test -n "$input" -a "$input" != "yes" -a "$input" != "no"
do
    $echo "Add debug information to the generated code?"
    $echo "[$config_debug] \c"
    read input
done
if test -n "$input"
then
    config_debug="$input"
fi

#
# Ask some ORBacus specific questions
#
if test "$orbacus" = "yes"
then
    if test ! -d ob
    then
	while true
	do
	    $echo
	    $echo "Please enter the ORBacus installation path:"
	    $echo "[$config_ob_path] \c"
	    input=
	    read input
	    if test -n "$input"
	    then
		config_ob_path="$input"
	    fi
	    if test ! -d "$config_ob_path"
	    then
		$echo "Directory $config_ob_path does not exist!"
	    else
		break
	    fi
	done
    else
	config_ob_path=
    fi

    $echo
    input="x"
    while test -n "$input" -a "$input" != "yes" -a "$input" != "no"
    do
	if test -d ob
	then
	    $echo "Compile ORBacus with JThreads/C++ support? (Required for Event Service)"
	else
	    $echo "Did you compile ORBacus with JThreads/C++ support? (Required for Event Service)"
	fi
        $echo "[$config_jtc] \c"
	read input
    done
    if test -n "$input"
    then
	config_jtc="$input"
    fi

    if test ! -d jtc -a "$config_jtc" = "yes"
    then
	while true
	do
	    $echo
	    $echo "Please enter the JThreads/C++ installation path:"
	    $echo "[$config_jtc_path] \c"
	    input=
	    read input
	    if test -n "$input"
	    then
		config_jtc_path="$input"
	    fi
	    if test ! -d "$config_jtc_path"
	    then
		$echo "Directory $config_jtc_path does not exist!"
	    else
		break
	    fi
	done
    else
	config_jtc_path=
    fi
else
    config_ob_path=
    config_jtc_path=
    if test -d jtc
    then
        config_jtc="yes"
    else
        config_jtc="no"
    fi
fi 

#
# Ask some JThreads/C++ specific questions
#
if test -d jtc
then
    input="x"
    while test -n "$input" -a "$input" != "yes" -a "$input" != "no"
    do
	$echo
	$echo "Compile JThreads/C++ with stop/suspend/resume support?"
	$echo "(Use \"no\" here for best performance.)"
	$echo "[$config_jtc_stop] \c"
	read input
    done
    if test -n "$input"
    then
	config_jtc_stop="$input"
    fi
else
    config_jtc_stop="no"
fi

#
# Ask for extra preprocessor flags
#
$echo
$echo "Please enter any extra preprocessor flags, like" \
      "\`-I/some/directory':"
$echo "[$config_extra_cpp_flags] \c"
input=
read input
if test -n "$input"
then
    config_extra_cpp_flags="$input"
fi

#
# Ask for extra compiler flags
#
$echo
$echo "Please enter any extra compiler flags, like" \
      "\`-pipe':"
$echo "[$config_extra_cxx_flags] \c"
input=
read input
if test -n "$input"
then
    config_extra_cxx_flags="$input"
fi

#
# Ask for extra linker flags
#
$echo
$echo "Please enter any extra linker flags, like" \
      "\`-L/some/directory':"
$echo "[$config_extra_ld_flags] \c"
input=
read input
if test -n "$input"
then
    config_extra_ld_flags="$input"
fi

#
# Ask for the installation path
#
while true
do
    $echo
    $echo "Where do you want to install everything?"
    $echo "[$config_install_path] \c"
    input=
    read input
    if test -n "$input"
    then
	config_install_path="$input"
    fi
    if test ! -d "$config_install_path"
    then
	$echo "Directory $config_install_path does not exist!"
    else
	break
    fi
done

#
# Write current configuration data
#
set | grep "^config_[a-z_]*=[^ ]" > ./config/current

#
# Set environment variables
#
env_AR=
env_ARFLAGS=
env_CC=
env_CPPFLAGS=
env_CXX=
env_CXXFLAGS=
env_LDFLAGS=
env_LDOPTS=
#env_LD_LIBRARY_PATH=
env_LIBEXT=
env_LIBPATH=
env_RANLIB=
#env_SHLIB_PATH=
xincludes=
xlibraries=

lib_dirs=
for i in jtc ob naming property event obtcl ssl notify
do
    if test -d $i
    then
	lib_dirs="`pwd`/$i/lib:$lib_dirs"
    fi
done

with_jtc=
with_jtc_stop=
if test "$config_jtc" = "yes"
then
    if test -n "$config_jtc_path"
    then
	with_jtc=" --with-jtc=\"$config_jtc_path\""
	lib_dirs="$config_jtc_path/lib:$lib_dirs"
    else
	with_jtc=" --with-jtc"
    fi

    if test "$config_jtc_stop" = "yes"
    then
        with_jtc_stop=" --with-jtc-stop"
    fi
fi

with_ob=
if test -n "$config_ob_path"
then
    with_ob=" --with-ob=\"$config_ob_path\""
    lib_dirs="$config_ob_path/lib:$lib_dirs"
fi

prefix=
if test -n "$config_install_path" -a "$config_install_path" != "/usr/local"
then
    prefix=" --prefix=\"$config_install_path\""
fi

case $config_platform in

    $cpp_sgi_72 ) # SGI C++ 7.2
	env_CC="cc"
	env_CXX="CC"
	env_CXXFLAGS="-exceptions"
	if test "$config_shared" = "yes"
	then
	    env_AR="$env_CXX"
	    env_ARFLAGS="-exceptions -shared -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LD_LIBRARY_PATH="${lib_dirs}\$LD_LIBRARY_PATH"
	else
	    env_AR="$env_CXX"
	    env_ARFLAGS="-exceptions -ar -o"
	fi
	;;

    $cpp_sun ) # SUN C++
	env_CC="cc"
	env_CXX="CC"
	env_CXXFLAGS="-pta"
	if test "$config_shared" = "yes"
	then
	    env_CXXFLAGS="-PIC $env_CXXFLAGS"
            env_AR="$env_CXX"
	    env_ARFLAGS="-G -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LD_LIBRARY_PATH="${lib_dirs}\$LD_LIBRARY_PATH"
	else
            env_AR="$env_CXX"
	    env_ARFLAGS="-xar -o"
	fi
        if test "$config_jtc" = "yes"
        then
	    env_CXXFLAGS="-mt $env_CXXFLAGS"
	    env_ARFLAGS="-mt $env_ARFLAGS"
	fi
	;;

    $cpp_hp ) # HP aC++
	env_CC="cc -Ae"
	env_CXX="aCC"
	env_CXXFLAGS="+inst_none"
	if test "$config_shared" = "yes"
	then
	    env_CXXFLAGS="+Z $env_CXXFLAGS"
	    env_AR="$env_CXX"
	    env_ARFLAGS="-b -o"
	    env_RANLIB=":"
	    env_LIBEXT=".sl"
	    env_LDOPTS="+s"
	    env_SHLIB_PATH="${lib_dirs}\$SHLIB_PATH"
	fi
	;;

    $cpp_aix ) # AIX C Set ++
        if test "$config_jtc" = "yes"
        then
	    env_CC="xlc_r"
	    env_CXX="xlC_r"
	else
	    env_CC="xlc"
	    env_CXX="xlC"
	fi
	env_CXXFLAGS="-+ -qnotempinc"
        xincludes=" --x-includes=/usr/lpp/xlC/include"
	if test "$config_shared" = "yes"
	then
	    env_CXXFLAGS="$env_CXXFLAGS -brtl"
	    env_AR="/usr/lpp/xlC/bin/makeC++SharedLib"
	    env_ARFLAGS="-p 0 -G -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LIBPATH="${lib_dirs}\$LIBPATH"
	fi
	;;

    $cpp_gcc_28 ) # EGCS 1.0.x, GCC 2.8.1
	env_CC="gcc"
	env_CXX="c++"
	env_CXXFLAGS="--no-implicit-templates -Wall -Wno-return-type"
	if test "$config_shared" = "yes"
	then
	    env_AR="$env_CXX"
	    env_ARFLAGS="-shared -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LD_LIBRARY_PATH="${lib_dirs}\$LD_LIBRARY_PATH"
	fi
	;;

    $cpp_egcs_11 ) # EGCS 1.1.x
	env_CC="gcc"
	env_CXX="c++"
	env_CXXFLAGS="--no-implicit-templates -Wall -Wno-return-type"
	if test "$config_shared" = "yes"
	then
	    env_CXXFLAGS="-fPIC $env_CXXFLAGS"
	    env_AR="$env_CXX"
	    env_ARFLAGS="-fPIC -shared -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LD_LIBRARY_PATH="${lib_dirs}\$LD_LIBRARY_PATH"
	fi
	;;

    $cpp_gcc_295 ) # GCC 2.95.x
	env_CC="gcc"
	env_CXX="c++"
	env_CXXFLAGS="--no-implicit-templates -Wall -Wno-return-type -fpermissive"
	if test "$config_shared" = "yes"
	then
	    env_CXXFLAGS="-fPIC $env_CXXFLAGS"
	    env_AR="$env_CXX"
	    env_ARFLAGS="-fPIC -shared -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LD_LIBRARY_PATH="${lib_dirs}\$LD_LIBRARY_PATH"
	fi
	;;

    $cpp_dec ) # DEC C++
	env_CC="cc"
	env_CXX="cxx"
	env_CXXFLAGS="-nopt -distinguish_nested_enums"
	env_CXXFLAGS="$env_CXXFLAGS -msg_disable narrowptr"
	if test "$config_shared" = "yes"
	then
	    env_AR="$env_CXX"
	    env_ARFLAGS="'-Wl,-expect_unresolved,*' -shared -o"
	    env_RANLIB=":"
	    env_LIBEXT=".so"
	    env_LD_LIBRARY_PATH="${lib_dirs}\$LD_LIBRARY_PATH"
	fi
        if test "$config_jtc" = "yes"
        then
	    env_CXXFLAGS="-pthread $env_CXXFLAGS"
	fi
	;;

    * )
	$echo "$0: error: unsupported platform and/or compiler"
	exit
	;;

esac

if test "$config_optimized" = "yes"
then
    env_CPPFLAGS="-DNDEBUG $env_CPPFLAGS"
    env_CXXFLAGS="-O $env_CXXFLAGS"
    if test "$env_AR" = "$env_CXX"
    then
        env_ARFLAGS="-O $env_ARFLAGS"
    fi
fi

if test "$config_debug" = "yes"
then
    env_CXXFLAGS="-g $env_CXXFLAGS"
    if test "$env_AR" = "$env_CXX"
    then
        env_ARFLAGS="-g $env_ARFLAGS"
    fi
fi

if test -n "$config_extra_cpp_flags"
then
    env_CPPFLAGS="$config_extra_cpp_flags $env_CPPFLAGS"
fi

if test -n "$config_extra_cxx_flags"
then
    env_CXXFLAGS="$config_extra_cxx_flags $env_CXXFLAGS"
fi

if test -n "$config_extra_ld_flags"
then
    env_LDFLAGS="$config_extra_ld_flags $env_LDFLAGS"
fi

#
# Write script to set the environment
#
{
    if test "$config_shell" = "b"
    then
	set | sed -e "s/'//g" -e 's/\"//g' | grep "^env_[A-Z_]*=$" | \
	sed -e 's/^env_\([A-Z_]*\)=$/unset \1/g'
	set | sed -e "s/'//g" -e 's/\"//g' | grep "^env_[A-Z_]*=." | \
	sed -e 's/^env_\([A-Z_]*\)=\(.*\)$/\1=\"\2\"; export \1/g'
    fi
    if test "$config_shell" = "c"
    then
        if test -n "$env_LD_LIBRARY_PATH"
	then
	    $echo "if ( \$?LD_LIBRARY_PATH == 0 ) then"
	    $echo "    setenv LD_LIBRARY_PATH \"\""
	    $echo "endif"
	    $echo
        fi

        if test -n "$env_SHLIB_PATH"
	then
	    $echo "if ( \$?SHLIB_PATH == 0 ) then"
	    $echo "    setenv SHLIB_PATH \"\""
	    $echo "endif"
	    $echo
	fi

	set | sed -e "s/'//g" -e 's/\"//g' | grep "^env_[A-Z_]*=$" | \
	sed -e 's/^env_\([A-Z_]*\)=$/unsetenv \1/g'
	set | sed -e "s/'//g" -e 's/\"//g' | grep "^env_[A-Z_]*=." | \
	sed -e 's/^env_\([A-Z_]*\)=\(.*\)$/setenv \1 \"\2\"/g'
    fi
} > config.env

#
# Tell the user how to run configure
#
$echo
$echo
$echo
$echo "To run \`configure', execute the following code in your shell, for"
$echo "example by using \"cut & paste\":"
$echo
if test "$config_shell" = "b"
then
    $echo ". ./config.env"
fi
if test "$config_shell" = "c"
then
    $echo "source ./config.env"
fi
$echo "rm -f config.cache"
$echo "./configure${prefix}${xincludes}${xlibraries}\c"
$echo "${with_jtc}${with_jtc_stop}${with_ob}"
