#!/bin/sh

#****************************************************************************
#
# $Id: configure,v 1.17 1994/05/24 20:25:51 mjl Exp $
#
# configure - configures plplot for build
#
# Maurice LeBrun (mjl@dino.ph.utexas.edu)
# IFS, University of Texas at Austin
# 24-Sep-93
# Loosely based on old TPC configure script by Geoff Furnish.
# Type "configure h" for info on usage.
#
# The parameters set by this script are typically things that are changed
# relatively frequently.  For example, optimization, debug, warnings, etc,
# are set depending on what stage of development you are at.  no_share,
# no_tk, no_dp, and no_x are useful in creating multiple versions of the
# libraries.  Macros that must be defined for specific systems or sites
# (and typically only the first time you port to it) should instead be
# handled in the config file for that system or in a site-specific config
# file (if you modify cf_plplot.m4 and put it in a directory ~/config,
# configure will use that the next time you run it).
#
#****************************************************************************

# Set up defaults
# The value from the environment is used, if present.
# This way you can customize the default build settings on a per site
# basis through your normal login.

if [ ! "$debug" ]; then
    debug=off
fi

if [ ! "$double" ]; then
    double=off
fi

if [ ! "$f2c" ]; then
    f2c=off
fi

if [ ! "$gcc" ]; then
    gcc=off
fi

if [ ! "$no_dp" ]; then
    no_dp=off
fi

if [ ! "$no_share" ]; then
    no_share=off
fi

if [ ! "$no_tk" ]; then
    no_tk=off
fi

if [ ! "$no_x" ]; then
    no_x=off
fi

if [ ! "$opt" ]; then
    opt=1
fi

if [ ! "$profile" ]; then
    profile=off
fi

if [ ! "$system" ] ; then
    uname 1>/dev/null 2>&1 && system=`uname`
fi

if [ ! "$warn" ]; then
    warn=off
fi

# Define a symbol for echo to save a little bit of space.

e=echo

# Define some handy functions

#****************************************************************************

# Spits out the current variable settings.

Settings () {

$e "The current settings are:"
$e ""
$e "	debug		$debug"
$e "	double		$double"
$e "	f2c		$f2c"
$e "	gcc		$gcc"
$e "	no_dp		$no_dp"
$e "	no_tk		$no_tk"
$e "	no_share	$no_share"
$e "	no_x		$no_x"
$e "	opt		$opt"
$e "	prefix		$prefix"
$e "	profile		$profile"
$e "	system		$system"
$e "	warn		$warn"
$e ""
}

#****************************************************************************

# Spits out help message, then exits
#
# Some example command lines:
#
#	configure debug double
#	configure opt=2 projdir=../src/myproj
#
# Specifying debug turns optimization off (opt=0), and specifying
# opt=1 or opt=2 turns debugging off.  Other weird combinations have
# to be detected by the makefile.

Help () {

$e "Syntax:"
$e "	configure [flags]"
$e ""
$e "where recognized flags include:"
$e ""
$e "	-debug	   [=on|off]	debugging"
$e "	-double	   [=on|off]	double precision"
$e "	-f2c	   [=on|off]	use f2c instead of native compiler"
$e "	-gcc	   [=on|off]	use gcc instead of native compiler"
$e "	-h			help (this message)"
$e "	-no_dp	   [=on|off]	no Tcl-DP linkage"
$e "	-no_tk	   [=on|off]	no Tcl/TK linkage"
$e "	-no_share  [=on|off]	do not build shared libraries"
$e "	-no_x	   [=on|off]	no X linkage"
$e "	-opt	   [=0|1|2]	optimization level"
$e "	-prefix    =<value>	top level install directory"
$e "	-profile   [=on|off]	profiling"
$e "	-system	   =<name>	system name"
$e "	-warn	   [=on|off]	turn on all compiler warnings"
$e ""
$e "Options are set via the command line, environment, and internal defaults,"
$e "in that order.  The option value must be specified without spaces, e.g."
$e "debug=off.  The value can often be omitted, in which case booleans are set"
$e "to \"on\" and multi-state variables are set to some typical \"on\" value."
$e "Flags can usually be truncated to their shortest unique length.  Leading"
$e "dashes are optional."
$e ""
	Settings
	exit 1
}

#****************************************************************************

# Parses argument list
# The first trip through sed strips any leading dashes, so you can use
# a single or double leading dash in arguments if you prefer.

Parse_args () {

    while [ $# -gt 0 ]
    do
	arg=`echo $1 | sed 's/-*//'`
	case "$arg" in

	de=*|deb=*|debu=*|debug=*)
	    debug=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    if [ $debug = "on" ]; then opt=0; fi
	    ;;

	de|deb|debu|debug)
	    debug=on
	    opt=0
	    ;;

	dbl=*|dou=*|doub=*|doubl=*|double=*)
	    double=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	dbl|dou|doub|doubl|double)
	    double=on
	    ;;

	f2c=*)
	    f2c=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	f2c)
	    f2c=on
	    ;;

	gcc=*)
	    gcc=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	gcc)
	    gcc=on
	    ;;

	h|he|hel|help)
	    give_me_help=1
	    ;;

	no_d=*|no_dp=*)
	    no_dp=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	no_dp)
	    no_dp=on
	    ;;

	no_s=*|no_sh=*|no_sha=*|no_shar=*|no_share=*)
	    no_share=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	no_share)
	    no_share=on
	    ;;

	no_t=*|no_tk=*)
	    no_tk=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	no_tk)
	    no_tk=on
	    ;;

	no_x=*)
	    no_x=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	no_x)
	    no_x=on
	    ;;

	o=*|op=*|opt=*)
	    opt=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    if [ $opt -gt 0 ]; then debug=off; fi
	    ;;

	o|op|opt)
	    opt=1; debug=off
	    ;;

	pre=*|pref=*|prefi=*|prefix=*)
	    prefix=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	pro=*|prof=*|profi=*|profil=*|profile=*)
	    profile=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	pro|prof|profi|profil|profile)
	    profile=on
	    ;;

	s=*|sy=*|sys=*|syst=*|syste=*|system=*)
	    system=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	wa=*|war=*|warn=*)
	    warn=`echo $arg | sed 's/[-a-z_0-9 ]*=//'`
	    ;;

	wa|war|warn)
	    warn=on
	    ;;

	*)
	    give_me_help=1
	    ;;

	esac
	shift
    done
}

#****************************************************************************

# Sets up all the build options.

Setup_options () {

# Allow various abbreviations for system name, including output of `uname`.  

    case "$system" in

    aix*|AIX*|rs*|RS*|ibm*|IBM*)
	system="AIX"
	;;

    alpha*|ALPHA*|Alpha*|OSF*)
	system="ALPHAOSF"
	;;

    bsd*|BSD*)
	system="BSD"
	;;

    convex*|ConvexOS*)
	system="CONVEX"
	;;

    dg*|DG*)
	system="DGUX"
	;;

    hp*|HP*)
	system="HP"
	;;

    irix*|IRIX*|Irix*|sgi*|SGI*)
	system="IRIX"
	;;

    linux*|LINUX*|Linux*)
	system="LINUX"
	;;

    next*|NeXT*|NEXT*)
	system="NEXT"
	;;

    sun*|SUN*|Sun*)
	system="SUN"
	;;

    sx*|Sx*|SX*|monte*|Monte*|MONTE*)
	system="SX"
	;;

    sysv*|SYSV*|Sysv*)
	system="SYSV"
	;;

    ultrix*|ULTRIX*)
	system="ULTRIX"
	;;

    unicos*|Unicos*|UNICOS*|Cray*|cray*|sn*)
	system="UNICOS"
	;;

    *)	
	echo "Unrecognized system"
	Help
		;;
    esac

    system_flag="-DSYSTEM=$system"

# Set up machine name if necessary.  Use output from `uname -m`.

    case "$system" in 

    UNICOS)
	if [ ! "$machine" ]; then
	    machine=`uname -m`
	fi

	case "$machine" in

	"CRAY C90"|"CRAY Y-MP"|"cray y-mp"|y*|Y*|CRAY*1|cray*1|1)
	    machine="CRAY_1"
	    ;;

	CRAY*2|cray*2|2)
	    machine="CRAY_2"
			;;
	esac
	machine_flag="-DMACHINE=$machine"
	;;
    esac

# Set up everything else.

    if [ "$debug" = "on" ]; then
	debug_flag="-DDEBUG"
    fi

    if [ "$double" = "on" ]; then
	double_flag="-DDOUBLE"
    fi

    if [ "$f2c" = "on" ]; then
	f2c_flag="-DF2C"
    fi

    if [ "$gcc" = "on" ]; then
	gcc_flag="-DGCC"
    fi

    if [ "$no_x" = "on" ]; then
	no_x_flag="-DNO_X"
    fi

    if [ "$no_tk" = "on" ]; then
	no_tk_flag="-DNO_TK"
    fi

    if [ "$no_dp" = "on" ]; then
	no_dp_flag="-DNO_DP"
    fi

    if [ "$no_share" = "on" ]; then
	no_share_flag="-DNO_SHARE"
    fi

    if [ "$opt" -eq 1 ]; then
	opt_flag="-DOPT=1"
    fi

    if [ "$opt" -eq 2 ]; then
	opt_flag="-DOPT=2"
    fi

    if [ "$prefix" ]; then
	prefix_flag="-DPREFIX=$prefix"
	echo "prefix: $prefix, prefix_flag: $prefix_flag"
    fi

    if [ "$profile" = "on" ]; then
	profile_flag="-DPROF"
    fi

    if [ "$warn" = "on" ]; then
	warn_flag="-DWARN"
    fi

    all_flags="$system_flag $machine_flag $double_flag $debug_flag \
$opt_flag $warn_flag $profile_flag $gcc_flag $f2c_flag $prefix_flag \
$no_x_flag $no_tk_flag $no_dp_flag $no_share_flag"
}

#****************************************************************************

# Actually configures the code.

Configure () {

# First figure out where we are and go to the tmp directory if possible.

    pwd=`pwd`
    curdir=`basename $pwd`

    if [ "$curdir" != "tmp" ] && [ -d tmp ]; then
	cd tmp
    fi

# One last check

    if [ ! -d ../src ] || [ ! -d ../drivers ] || [ ! -d ../include ]; then
	echo \
"Must run configure from root or tmp PLplot directory."
	exit 1
    fi

# Create links to source code.

    echo "Creating links.."

    ln -s \
	../src/*.c \
	../src/stubc/*.c \
	../examples/C/*.c \
	../examples/tk/* \
	../utils/*.c \
	../fonts/*.c \
	../scripts/pl* \
	../include/*.h \
	../drivers/*.c \
	../drivers/tk/*.* \
	../drivers/tk/tclIndex \
	../lib/*.fnt \
	.

# Create links to the Fortran files.  
# Double precision Fortran files in the stub interface are obtained
# entirely by m4 macro expansion.  This should work anywhere.  Double
# precision example programs are obtained through automatic compiler
# promotion.  Your compiler needs to be able to automatically promote
# all real variables and constants for this to work (note: the HPUX 8.x
# compiler did not support this, but it's supported under 9.0).


    ln -s \
	../src/stubf/*.* \
	../examples/f77/*.* \
	.

# Create links to m4 config files.

    ln -s ../m4/*.* .

# Try to locate site-specific config file.
# Look in ${configdir} first if it exists, then ${HOME}/config.  If not
# found, the default one in ../m4 (typically just a template) is retained.

    configfile=
    if [ -f ${configdir}/cf_plplot.m4 ]; then 
	configfile=${configdir}/cf_plplot.m4

    elif [ -f ${HOME}/config/cf_plplot.m4 ]; then 
	configfile=${HOME}/config/cf_plplot.m4
    fi

    if [ "$configfile" ]; then
	rm cf_plplot.m4
	ln -s $configfile .
	echo "Using $configfile as config file"
    fi

# Now time to build the makefile.

    echo "Building makefile in tmp directory with command:"
    echo ""
    echo "m4 $all_flags makefile.m4 >Makefile"

    m4 $all_flags makefile.m4 >Makefile
}

#****************************************************************************

# Now to actually do the work.

Parse_args $*
Setup_options

if [ "$give_me_help" ]; then
    Help
fi

Settings
Configure

echo "Done with configuring PLPLOT."
exit 0
