#!/bin/sh
#
#  Name:
#     optsetup    sh function declaration of setup_options_file
#     
#  Usage: (not for interactive use. To be sourced and called as a function)
#
#     . optsetup
#     setup_options_file [<filename>]
#
#   Description:
#     When invoked without a parameter, setup_options_file generates a list
#     of options files from which to select one to be copied to the default
#     options file location and name.
#
#     When invoked with a parameter, setup_options_file copies the
#     specified file to the default options file location and name.
#
#     setup_options_file is context sensitive. When invoked from mex, it
#     uses 'mexopts.sh' as the default options file target. When invoked
#     from mbuild, it uses 'mbuildopts.sh' as the default options file
#     target. 
#
#  See Also:
#     MATLAB API Guide
#
# Copyright (c) 1984-2000 by The MathWorks, Inc.
#  $Revision: 1.15 $  $Date: 2000/08/17 18:59:14 $
#____________________________________________________________________________
#
#****************************************************************************
#


setup_describe () {

SCRIPT_NAME=`basename $arg0_`

cat << EOF

    Using the '$SCRIPT_NAME -setup' command selects an options file that is
    placed in ~/.matlab/R12 and used by default for '$SCRIPT_NAME'. An options 
    file in the current working directory or specified on the command line 
    overrides the default options file in ~/.matlab/R12.

    Options files control which compiler to use, the compiler and link command
    options, and the runtime libraries to link against.
 
    To override the default options file, use the '$SCRIPT_NAME -f' command
    (see '$SCRIPT_NAME -help' for more information).
EOF
 
}


setup_options_file () {


    TAG="#SELECTION_TAG"
    DEFAULT_OPTIONS_FILE="mbuildopts.sh"

    #
    # Determine what script invoked this routine
    #
    SOURCE_SCRIPT=`basename $arg0_`

    #
    # if source script is MEX set the selection tag for MEX
    #
    if [ "$SOURCE_SCRIPT" = "mex" ]; then 
      TAG="#SELECTION_TAG_MEX_OPT:"
      DEFAULT_OPTIONS_FILE="mexopts.sh"
    fi

    #
    # if source script is MBUILD set the selection tag for all non generic...
    #
    if [ "$SOURCE_SCRIPT" = "mbuild" ]; then 
      TAG="#SELECTION_TAG_ML_OPT:"
      DEFAULT_OPTIONS_FILE="mbuildopts.sh"
    fi

    #
    # When given an input file that exists process it
    #
    if [ "X$1" != "X" -a -f "$1" ]; then
	FOUND_FILE=$1
    else

	setup_describe

	#
	# Inform the user of the available options files
	#
        echo
        echo The options files available for $SOURCE_SCRIPT are:
        echo

	#
	# For each *opt*.sh file in the options file path generate a prompt and
	# identifier for that file to be selected to copy to the default options
	# file. 
	#
	# Find *opts.sh files in the list of search directories
	#
	#  $TMW_ROOT/bin/<filename> 
	#
        file_list="`ls $TMW_ROOT/bin/*opts.sh 2> /dev/null`"

        opt_count=0
        file_array=""
        for file in $file_list ; do 
            file_comment=`egrep "^$TAG" $file | cut -f 2,3,4,5,6,7,8 -d:`
            if [ "$file_comment" != "" ]; then 
	        opt_count=`expr $opt_count + 1` ;
	        file_array="$file_array$file:"
	        echo "  $opt_count: $file : "
		echo "     $file_comment"
		echo ' '
            fi
        done
 
	#
	# Prompt user for a file selection, unless there's only one file
	# in which case just do the selection.
	#

	if [ "$opt_count" = "1" ]; then
	    fname_number=1
        else
            echo
	    echo Enter the number of the options file to use as your default options file:
            echo
            read fname_number
	fi

	#
	# providing the file selection was within the valid range, copy it to...
	#
        if [ `expr $fname_number` -ge 1 -a `expr $fname_number` -le $opt_count ]; then
	    FOUND_FILE="`echo $file_array | cut -f $fname_number -d:`"
        else
            describe no_options_file >&2
            trap ""
            exit 1
        fi
    fi

    #
    # Create $HOME/.matlab/R12 for the user, if it doesn't already exist
    #
    if [ ! -d $HOME/.matlab/R12 ]; then
        echo
        echo $HOME/.matlab/R12 does not exist. It will be created.
        echo
	mkdir -p $HOME/.matlab/R12
    fi

    if [ -d $HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE ]; then
        echo "$HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE is a directory. Please remove it."
        exit 1
    fi
    if [ -f $HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE ]; then
        echo "Overwrite $HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE ([y]/n)? "
        read action
        if [ "$action" = "n" ]; then
            echo "Will not overwrite existing file. Options file not changed."
            exit 1
        fi
        rm -f $HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE
    fi
    echo
    echo "$FOUND_FILE is being copied to "
    echo "$HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE"
    echo
    cp $FOUND_FILE $HOME/.matlab/R12/$DEFAULT_OPTIONS_FILE

}
# end setup_options_file ()
#
#****************************************************************************
#
