#! /usr/bin/bash
# This machine-generated portable shell script is part of GNU Jitter.
# The human-written source file for this script is ./bin/jitter-config.in.m4sh .
# The initial comments in the source are not reproduced here; see
# the source file for copyright and license notices.
#
# The shebang line below is, of course, ignored.  See the comments
# in amend-autom4te-output , distributed along with the Jitter
# sources, for an explanation.
#
# What follows is generated by autom4te from an M4sh source.

#! /bin/sh
# Generated from jitter-script.m4sh by GNU Autoconf 2.71.
## -------------------- ##
## M4sh Initialization. ##
## -------------------- ##

# Be more Bourne compatible
DUALCASE=1; export DUALCASE # for MKS sh
as_nop=:
if test ${ZSH_VERSION+y} && (emulate sh) >/dev/null 2>&1
then :
  emulate sh
  NULLCMD=:
  # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
  # is contrary to our usage.  Disable this feature.
  alias -g '${1+"$@"}'='"$@"'
  setopt NO_GLOB_SUBST
else $as_nop
  case `(set -o) 2>/dev/null` in #(
  *posix*) :
    set -o posix ;; #(
  *) :
     ;;
esac
fi



# Reset variables that may have inherited troublesome values from
# the environment.

# IFS needs to be set, to space, tab, and newline, in precisely that order.
# (If _AS_PATH_WALK were called with IFS unset, it would have the
# side effect of setting IFS to empty, thus disabling word splitting.)
# Quoting is to prevent editors from complaining about space-tab.
as_nl='
'
export as_nl
IFS=" ""	$as_nl"

PS1='$ '
PS2='> '
PS4='+ '

# Ensure predictable behavior from utilities with locale-dependent output.
LC_ALL=C
export LC_ALL
LANGUAGE=C
export LANGUAGE

# We cannot yet rely on "unset" to work, but we need these variables
# to be unset--not just set to an empty or harmless value--now, to
# avoid bugs in old shells (e.g. pre-3.0 UWIN ksh).  This construct
# also avoids known problems related to "unset" and subshell syntax
# in other old shells (e.g. bash 2.01 and pdksh 5.2.14).
for as_var in BASH_ENV ENV MAIL MAILPATH CDPATH
do eval test \${$as_var+y} \
  && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || :
done

# Ensure that fds 0, 1, and 2 are open.
if (exec 3>&0) 2>/dev/null; then :; else exec 0</dev/null; fi
if (exec 3>&1) 2>/dev/null; then :; else exec 1>/dev/null; fi
if (exec 3>&2)            ; then :; else exec 2>/dev/null; fi

# The user is always right.
if ${PATH_SEPARATOR+false} :; then
  PATH_SEPARATOR=:
  (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {
    (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||
      PATH_SEPARATOR=';'
  }
fi


# Find who we are.  Look in the path if we contain no directory separator.
as_myself=
case $0 in #((
  *[\\/]* ) as_myself=$0 ;;
  *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
for as_dir in $PATH
do
  IFS=$as_save_IFS
  case $as_dir in #(((
    '') as_dir=./ ;;
    */) ;;
    *) as_dir=$as_dir/ ;;
  esac
    test -r "$as_dir$0" && as_myself=$as_dir$0 && break
  done
IFS=$as_save_IFS

     ;;
esac
# We did not find ourselves, most probably we were run as `sh COMMAND'
# in which case we are not to be found in the path.
if test "x$as_myself" = x; then
  as_myself=$0
fi
if test ! -f "$as_myself"; then
  printf "%s\n" "$as_myself: error: cannot find myself; rerun with an absolute file name" >&2
  exit 1
fi


if test "x$CONFIG_SHELL" = x; then
  as_bourne_compatible="as_nop=:
if test \${ZSH_VERSION+y} && (emulate sh) >/dev/null 2>&1
then :
  emulate sh
  NULLCMD=:
  # Pre-4.2 versions of Zsh do word splitting on \${1+\"\$@\"}, which
  # is contrary to our usage.  Disable this feature.
  alias -g '\${1+\"\$@\"}'='\"\$@\"'
  setopt NO_GLOB_SUBST
else \$as_nop
  case \`(set -o) 2>/dev/null\` in #(
  *posix*) :
    set -o posix ;; #(
  *) :
     ;;
esac
fi
"
  as_required="as_fn_return () { (exit \$1); }
as_fn_success () { as_fn_return 0; }
as_fn_failure () { as_fn_return 1; }
as_fn_ret_success () { return 0; }
as_fn_ret_failure () { return 1; }

exitcode=0
as_fn_success || { exitcode=1; echo as_fn_success failed.; }
as_fn_failure && { exitcode=1; echo as_fn_failure succeeded.; }
as_fn_ret_success || { exitcode=1; echo as_fn_ret_success failed.; }
as_fn_ret_failure && { exitcode=1; echo as_fn_ret_failure succeeded.; }
if ( set x; as_fn_ret_success y && test x = \"\$1\" )
then :

else \$as_nop
  exitcode=1; echo positional parameters were not saved.
fi
test x\$exitcode = x0 || exit 1
blah=\$(echo \$(echo blah))
test x\"\$blah\" = xblah || exit 1
test -x / || exit 1"
  as_suggested=""
  if (eval "$as_required") 2>/dev/null
then :
  as_have_required=yes
else $as_nop
  as_have_required=no
fi
  if test x$as_have_required = xyes && (eval "$as_suggested") 2>/dev/null
then :

else $as_nop
  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
as_found=false
for as_dir in /bin$PATH_SEPARATOR/usr/bin$PATH_SEPARATOR$PATH
do
  IFS=$as_save_IFS
  case $as_dir in #(((
    '') as_dir=./ ;;
    */) ;;
    *) as_dir=$as_dir/ ;;
  esac
  as_found=:
  case $as_dir in #(
	 /*)
	   for as_base in sh bash ksh sh5; do
	     # Try only shells that exist, to save several forks.
	     as_shell=$as_dir$as_base
	     if { test -f "$as_shell" || test -f "$as_shell.exe"; } &&
		    as_run=a "$as_shell" -c "$as_bourne_compatible""$as_required" 2>/dev/null
then :
  CONFIG_SHELL=$as_shell as_have_required=yes
		   break 2
fi
	   done;;
       esac
  as_found=false
done
IFS=$as_save_IFS
if $as_found
then :

else $as_nop
  if { test -f "$SHELL" || test -f "$SHELL.exe"; } &&
	      as_run=a "$SHELL" -c "$as_bourne_compatible""$as_required" 2>/dev/null
then :
  CONFIG_SHELL=$SHELL as_have_required=yes
fi
fi


      if test "x$CONFIG_SHELL" != x
then :
  export CONFIG_SHELL
             # We cannot yet assume a decent shell, so we have to provide a
# neutralization value for shells without unset; and this also
# works around shells that cannot unset nonexistent variables.
# Preserve -v and -x to the replacement shell.
BASH_ENV=/dev/null
ENV=/dev/null
(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV
case $- in # ((((
  *v*x* | *x*v* ) as_opts=-vx ;;
  *v* ) as_opts=-v ;;
  *x* ) as_opts=-x ;;
  * ) as_opts= ;;
esac
exec $CONFIG_SHELL $as_opts "$as_myself" ${1+"$@"}
# Admittedly, this is quite paranoid, since all the known shells bail
# out after a failed `exec'.
printf "%s\n" "$0: could not re-execute with $CONFIG_SHELL" >&2
exit 255
fi

    if test x$as_have_required = xno
then :
  printf "%s\n" "$0: This script requires a shell more modern than all"
  printf "%s\n" "$0: the shells that I found on your system."
  if test ${ZSH_VERSION+y} ; then
    printf "%s\n" "$0: In particular, zsh $ZSH_VERSION has bugs and should"
    printf "%s\n" "$0: be upgraded to zsh 4.3.4 or later."
  else
    printf "%s\n" "$0: Please tell bug-autoconf@gnu.org about your system,
$0: including any error possibly output before this
$0: message. Then install a modern shell, or manually run
$0: the script under such a shell if you do have one."
  fi
  exit 1
fi
fi
fi
SHELL=${CONFIG_SHELL-/bin/sh}
export SHELL
# Unset more variables known to interfere with behavior of common tools.
CLICOLOR_FORCE= GREP_OPTIONS=
unset CLICOLOR_FORCE GREP_OPTIONS

## --------------------- ##
## M4sh Shell Functions. ##
## --------------------- ##
# as_fn_unset VAR
# ---------------
# Portably unset VAR.
as_fn_unset ()
{
  { eval $1=; unset $1;}
}
as_unset=as_fn_unset

# as_fn_nop
# ---------
# Do nothing but, unlike ":", preserve the value of $?.
as_fn_nop ()
{
  return $?
}
as_nop=as_fn_nop

## -------------------- ##
## Main body of script. ##
## -------------------- ##



# Include other M4 macro definitions.
################################################################

# Here we include general-purpose M4 macro definitions from the Autoconf
# definition file for Jitter, ignoring the Autoconf definitions themselves.
#
# This is easier than the more intuitive alternative of having the Autoconf
# macros include a library of definitions: having dependencies between an
# installed Autoconf macro file and a generic M4sugar file, also installed, is
# messy.

# Redefine AC_DEFUN to expand to nothing.  This way we can simply include the
# file containing AC_DEFUN definitions, and use everything else.
# Line comments starting with "#" will remain in the expansion, but that is
# harmless.


# Include.
# Autoconf macros for Jitter.
# Copyright (C) 2017, 2018, 2019, 2020, 2021 Luca Saiu
# Written by Luca Saiu

# This file is part of GNU Jitter.

# GNU Jitter 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 3 of the License, or
# (at your option) any later version.

# GNU Jitter 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 GNU Jitter.  If not, see <https://www.gnu.org/licenses/>.


# The Autoconf manual says (about writing Autoconf macros):
# No Autoconf macro should ever enter the user-variable name space;
# i.e., except for the variables that are the actual result of running the
# macro, all shell variables should start with 'ac_'.  In addition, small
# macros or any macro that is likely to be embedded in other macros should
# be careful not to use obvious names.


# Generic M4 machinery.
################################################################

# jitter_tocpp([my-text])
# -----------------------
# Expand to the argument where each letter is converted to upper case, and
# each dash is replaced by an underscore.
# Example: jitter_tocpp([my-teXt_4]) --> MY_TEXT_4
# About the namespace, see the comment for jitter_using_automake.

# jitter_define_iterator([iterator_name], [list])
# -----------------------------------------------
# Expand to the *definition* of an iterator like jitter_for_dispatch or
# jitter_for_flag , below.  Notice that there is an empty line appended to
# each repetition of the body.
# This higher-order macro is used internally to define convenient, nestable
# iterator macros.

# FIXME: no, this doesn't get quoting quite right.  Not using it.  Test case:
# jitter_for_dispatch([j],
#   [echo "quoted-[j] is unquoted-j"])
# =>
# echo "quoted-switch is unquoted-switch"
# echo "quoted-direct-threading is unquoted-direct-threading"
# echo "quoted-minimal-threading is unquoted-minimal-threading"
# echo "quoted-no-threading is unquoted-no-threading"
#
# Instead, by using directly m4_foreach , I get what I want:
# m4_foreach([i],
#            [jitter_dispatches],
#   [echo "quoted-[i] is unquoted-i"
# ])
# =>
# echo "quoted-i is unquoted-switch"
# echo "quoted-i is unquoted-direct-threading"
# echo "quoted-i is unquoted-minimal-threading"
# echo "quoted-i is unquoted-no-threading"

# jitter_for_dispatch([a_dispatch_variable], [body])
# --------------------------------------------------
# Expand to a sequence of bodies, each copy with a_dispatch_variable replaced
# by each of the dispatches in the order of jitter_dispatches; each body
# repetition has a newline at the end.
# Example:
# jitter_for_dispatch([a],
#   [jitter_for_dispatch([b],
#     [echo [Here is a dispatch pair: ]a[, ]b])])
#
# FIXME: this commented-out definition is not correct with respect to
# quoting: see the comment above.
# # jitter_define_iterator([jitter_for_dispatch],
# #                        [jitter_dispatches])


# jitter_for_flag([a_flag_variable], [body])
# ------------------------------------------
# Like jitter_for_dispatch, using jitter_flags instead of jitter_dispatches .
#
# FIXME: this commented-out definition is not correct with respect to
# quoting: see the comment above.
# jitter_define_iterator([jitter_for_flag],
#                        [jitter_flags])



# Jitter global definitions.
################################################################

# jitter_dispatches
# -----------------
# An M4 quoted list of every existing Jitter dispatch, including the ones which
# are not enabled, lower-case with dashes.


# jitter_flags
# ------------
# An M4 quoted list of flag names, lower-case with dashes.



# Jitter internal Autoconf macros.
################################################################

# The Autoconf macros in this section are subject to change in the future, and
# should not be directly invoked by the user.

# JITTER_USING_AUTOMAKE
# ---------------------
# This is only used internally.  If Automake is being used, then define
# the shell variable jitter_using_automake to "yes".
 # JITTER_USING_AUTOMAKE


# Sub-packages to be configured immediately, not on AC_OUTPUT.
################################################################

# JITTER_CONFIG_SUBDIRS_NOW([dir ...])
# ------------------------------------
# Behave like AC_CONFIG_SUBDIRS , but call the recursive configure immediately
# rather that at the time of AC_OUTPUT.  A final explicit AC_OUTPUT call by
# the user will still be required, as usual.
#
# Rationale: If the sub-package is configured immediately, the rest of
# the outer package configure script can make its configuration depend
# on the inner package.
 # JITTER_CONFIG_SUBDIRS_NOW


# Jitter Autoconf macros, not meant for the user.
################################################################

# JITTER_CHECK_AUTOCONF_VERSION
# -----------------------------
# Define the following M4 macros determining whether Autoconf is recent enough
# to support a certain fetaure:
# * jitter_autoconf_version
#   Define to the Autoconf version number as suitable as an argument for
#   m4_version_compare.  Define as the three-character string old if Autoconf
#   is too old to support this feature.
# * jitter_ac_prog_cc_can_check_c_dialect
#   Define to a non-empty value if we are sure that AC_PROG_CC sets
#   ac_prog_cc_stdc.  Empty otherwise;
# * jitter_ac_prog_lex_with_arguments
#   Define to a non-empty value if we are sure that AC_LEX supports arguments.
#   Empty otherwise.
 # JITTER_CHECK_AUTOCONF_VERSION

# JITTER_PROG_CC_AND_CHECK_C_DIALECT_OLD_STYLE
# --------------------------------------------
# This is one of the two definitions of JITTER_PROG_CC_AND_CHECK_C_DIALECT; it
# is less precise than JITTER_PROG_CC_AND_CHECK_C_DIALECT_NEW_STYLE but works
# with older Autoconf versions as well.
 # JITTER_PROG_CC_AND_CHECK_C_DIALECT_OLD_STYLE

# JITTER_PROG_CC_AND_CHECK_C_DIALECT_NEW_STYLE
# --------------------------------------------
# This is one of the two definitions of JITTER_PROG_CC_AND_CHECK_C_DIALECT; this
# requires that the Autoconf macros are recent enough for AC_PROG_CC to set
# ac_prog_cc_stdc.
 # JITTER_PROG_CC_AND_CHECK_C_DIALECT_NEW_STYLE

# JITTER_PROG_CC_AND_CHECK_C_DIALECT
# ----------------------------------
# Use AC_PROG_CC and possibly other macros to check for a C compiler, and warn
# if the supported dialect is not recent enough; try to continue in any case.
#
# This is defined conditionally according to the Autoconf version; more recent
# versions of Autoconf allow for more precise checks.
 # JITTER_PROG_CC_AND_CHECK_C_DIALECT

# JITTER_WITH_JITTER_COMMAND_LINE_OPTION
# --------------------------------------
# Provide a configure-time option --with-jitter="PREFIX", setting a prefix
# to search for jitter-config and jitter.
# If the option is not given then the two executables are searched for in
# $PATH; if the option is given then they are searched for *only* in the
# "bin" subdirectory of the given prefix.
#
# This macro sets the environment variable jitter_jitter_path if the option is
# given; it leaves the variable unset otherwise.
#
# This option is intended for packages using Jitter as a dependency, rather than
# as a sub-package; in sub-package mode the executables will be searched for in
# the appropriately named subdirectories of the super-package source and build
# directories.


# JITTER_PROG_JITTER_CONFIG
# -------------------------
# Make sure that the system can compile Jittery VMs; this also tests
# for a C compiler using AC_PROG_CC.
# Look for the jitter-config script:
# * if the shell variable JITTER_SUBPACKAGE_DIRECTORY is defined as a non-empty
#   value, in ${JITTER_SUBPACKAGE_DIRECTORY}/bin relative to the super-package
#   build directory;
# * if the environment variable jitter_jitter_path is set, in
#   ${jitter_jitter_path}/bin (only);
# * otherwise, in $PATH;
#
# Choose the default dispatch, using --with-jitter-dispatch="DISPATCH" or
# the best available dispatch if the option is not given.
#
# Define the substitution JITTER_CONFIG to either the full pathname of a
# jitter-config script which appears to be working, or nothing in case of
# problems.
# When Automake is used, also define the following Automake conditional:
# * JITTER_HAVE_JITTER_CONFIG  (true iff jitter-config has been found).
#
# In case jitter-config is found, also substitute:
# * JITTER_CONFIG_VERSION      (the version of Jitter the jitter-config script
#                               comes from)
# * JITTER_DISPATCHES          (all the enabled dispatched, small caps, with
#                               dashes separating words, one space separating
#                               dispatch names);
# * JITTER_BEST_DISPATCH       (the best dispatch in JITTER_DISPATCHES);
# * JITTER_DEFAULT_DISPATCH    (the dispatch chosen by default);
# * JITTER_CFLAGS              (CFLAGS with the default dispatch);
# * JITTER_CPPFLAGS            (CPPFLAGS with the default dispatch);
# * JITTER_LDADD               (LDADD with the default dispatch);
# * JITTER_LDFLAGS             (LDFLAGS with the default dispatch);
# * JITTER_LIBADD              (LIBADD with the default dispatch);
# * for every dispatch $D (in all caps, with underscores separating words):
#   - JITTER_$D_CFLAGS         (CFLAGS with dispatch $D);
#   - JITTER_$D_CPPFLAGS       (CPPFLAGS with dispatch $D);
#   - JITTER_$D_LDADD          (LDADD with dispatch $D);
#   - JITTER_$D_LDFLAGS        (LDFLAGS with dispatch $D);
#   - JITTER_$D_LIBADD         (LIBADD with dispatch $D).
# When Automake is used, also define the following Automake conditionals
# for each dispatch $D (in all caps, with underscores separating
# words):
# * JITTER_ENABLE_DISPATCH_$D  (true iff $D is enabled).
#
# Moreover, define the following for the libjitter-libtextstyle wrapper:
# * JITTER_LIBTEXTSTYLE_CPPFLAGS  (the command line C preprocessor flags to
#                                  use for *using* the wrapper library;
#                                  without these falgs the wrapper is not
#                                  used, even if available);
# * JITTER_LIBTEXTSTYLE_LDADD     (LDADD for linking the wrapper and GNU
#                                  libtextstyle itself into an executable);
# * JITTER_LIBTEXTSTYLE_LIBADD    (LDADD for linking the wrapper and GNU
#                                  libtextstyle itself into a library).
# When Automake is used, also defined the following Automake conditional:
# * JITTER_HAVE_LIBTEXTSTYLE      (true iff GNU Libtextstyle and its wrapper
#                                  are available).
#
# Moreover, define the following for the libjitter-readline wrapper, which
# is always built:
# * JITTER_READLINE_LDADD         (LDADD for linking the wrapper (and GNU
#                                  readline itself, where used) into an
#                                  executable);
# * JITTER_READLINE_LIBADD        (LDADD for linking the wrapper (and GNU
#                                  readline itself, where used) into a
#                                  library).
 # JITTER_PROG_JITTER_CONFIG


# JITTER_PROG_JITTER
# ------------------
# Look for jitter, the C code generator program in $PATH, or if the option
# --with-jitter="PREFIX" is given in PREFIX/bin (only).
#
# Substitute:
# * JITTER                            (the jitter program full path, or empty
#                                      if not found)
# * JITTER_VERSION                    (the version of Jitter the C generator
#                                      program comes from)
#
# When Automake is used, also define the following Automake conditional:
# * JITTER_HAVE_JITTER_PROG_JITTER    (true iff jitter has been found).
 # JITTER_PROG_JITTER


# Internal helpers for public utility macros.
################################################################

# The common factor of JITTER_PROG_FLEX_OLD_STYLE and
# JITTER_PROG_FLEX_NEW_STYLE.
 # JITTER_PROG_FLEX_COMMON

# One of the two implementations of JITTER_PROG_FLEX, intended for older
# Autoconf versions.


# One of the two implementations of JITTER_PROG_FLEX, intended for recent
# Autoconf versions.



# Jitter Autoconf macros, intended for the user.
################################################################

# The macros in this sections are meant for other packages using Jitter as a
# dependency or as a sub-package.  They are not used in the configuration of
# Jitter.


# JITTER_JITTER
# -------------
# Check for jitter-config and jitter as by calling both
# JITTER_PROG_JITTER_CONFIG and JITTER_PROG_JITTER , performing the same
# substitutions and supporting the same command-line options.
#
# Add the configure option --with-jitter , as per
# JITTER_WITH_JITTER_COMMAND_LINE_OPTION above.
#
# Warn if jitter-config and jitter have different versions.
#
# This is the only macro the user needs to call to check for a Jitter
# installation as a dependency.
 # JITTER_JITTER


# JITTER_JITTER_SUBPACKAGE([subdirectory])
# ----------------------------------------
# Configure Jitter in sub-package mode, using the Jitter source directory
# from the given subdirectory relative to the super-package source directory.
#
# This is the only macro the user needs to call to configure Jitter as a
# sub-package included in a source directory.  It defines the same entities
# as JITTER, but differently from it does not rely on, or support, an
# already installed copy of Jitter as a dependency.
 # JITTER_JITTER_SUBPACKAGE


# Jitter utility macros, possibly of interest to the user.
################################################################

# The macros in this section were prepared for internal use, but are
# sufficiently general to be of interest to external users as well.

# Check for Flex.  Set the output variable LEX and the cache variable
# ac_cv_prog_LEX to the program name, if found.
 # JITTER_PROG_FLEX

# Check for Bison.  Set the output variable YACC and the cache variable
# ac_cv_prog_YACC to the program name, if found.
 # JITTER_PROG_BISON



# Define some useful shell variables and functions.
################################################################

# The following assumes that the following variables are defined by the
# including script:
# * script_printed_name


# The actually used name for this script.
script_name="$0"

# Path definitions.  These have to be kept in sync with the ones at the
# beginning of Makefile.am ; see the comment there about the reason why
# they are just all set with AC_SUBST by configure .
prefix="/usr"
exec_prefix="/usr"
datarootdir="${prefix}/share"
includedir="/usr/include"
flagdir="/usr/lib/jitter/UNKNOWN/flags"
templatedir="${prefix}/share/jitter/UNKNOWN/templates"

# By default argp generates a --help text with section heading starting at the
# second column.  This doesn't play well with help2man, and while with argp I
# can use the environment setting ARGP_HELP_FMT='header-col=0' to override the
# look when calling help2man, here I can't.  So I'll set an indentation string
# to "" by default.
# Changing to the default argp look entails setting the variable to " ".  This
# script is not worth the trouble of really emulating argp's behavior with
# respect to the environment.
help_section_indentation=""

#####################
### FIXME: remove
set -e
# echo prefix is $prefix
# echo exec_prefix is $exec_prefix
# echo datadir is $datadir
# echo includedir is $includedir
# echo flagdir is $flagdir
# echo templatedir is $templatedir
####################


# Functions for printing information.
################################################################

# According to the Autoconf manual using echo with an argument starting with a
# dash is a common source of problems.  This crude replacement seems good
# enough.
my_echo ()
{
    cat <<EOF
$@
EOF
}


# Functions for printing information and exiting.
################################################################

# Run my_echo with the arguments, then exit with success.
echo_and_exit ()
{
    my_echo $@
    exit 0
}

# Print a usage message and exit with failure, as a response to an incorrect
# command line.  The message to print is the only function argument.
fail_usage ()
{
    message="$1"
    cat <<EOF
$script_name: $1.
Try '$script_name --help' for more information.
EOF
    exit 1
}

# Print a usage message and exit with failure, as a response to a semantic
# problem in the request.  This is similar to fail_usage , but does not invite the
# user to check --help .
fail_semantic ()
{
    message="$1"
    cat <<EOF
$script_name: $1.
EOF
    exit 1
}

# Print version information and exit with success.
version ()
{
    cat <<EOF
${script_printed_name} (GNU Jitter) UNKNOWN
Copyright (C) 2016-2023 Luca Saiu.
GNU Jitter comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Jitter under the terms of the GNU General
Public License, version 3 or any later version published by the Free Software
Foundation.  For more information see the file named COPYING in the source
distribution.

Written by Luca Saiu <https://ageinghacker.net>.
EOF
    exit 0
}

# A help function would be impractical to define here, as the output for --help
# depends strongly on the script.


# Define the globals required by jitter-script.m4sh :
script_printed_name=jitter-config


# Global settings and main variables.
################################################################

# Print help information and exit with success.
help ()
{
    cat <<EOF
Usage: ${script_name} OPTION [OPTION...]

Print GNU Jitter configuration information and compiler/linker flags to be used
for generated code.

${help_section_indentation}Configuration options:
  --best-dispatch            Print the best enabled dispatch and exit
  --dispatches               Print all the enabled dispatches in order of
                             increasing efficiency, all on the same line, and
                             exit
  --has-dispatch=DISPATCH    Exit immediately, with success if the dispatch
                             named DISPATCH is enabed, with failure otherwise

${help_section_indentation}Dispatch selection:
  --dispatch=DISPATCH        Choose the given dispatch for the following
                             (default: the best one which is enabled); see
                             --dispatches for the list of enabled dispatches
                             and --best-dispatch for the best one

${help_section_indentation}Preprocessing, compilation and linking flags:
  --cflags                   Print C compiler flags for the chosen dispatch.
                             These flags are *mandatory* to use when compiling
                             the vmprefix-vm2.c file generated by Jitter, to
                             ensure correctness; while the same flags are also
                             safe to use on other files they could negatively
                             affect performance.
  --cppflags                 Print C preprocessor flags for the chosen
                             dispatch.
                             These flags must be used to preprocess any file
                             including any Jitter header.
  --ldadd                    Print C compiler (driver) '-l' options for liking
                             the chosen dispatch library.
                             These flags are to be supplied at link time when
                             linking the Jitter runtime library.
  --ldflags                  Print C compiler (driver) linking flags for
                             linking the chosen dispatch library.
                             These flags are to be supplied at link time when
                             linking the Jitter runtime library.
  --libadd                   Print arguments consisting in Libtool convenience
                             libraries, to be linked into a library rather
                             than a program.
                             These arguments are to be supplied at link time
                             when linking the Jitter runtime library.

${help_section_indentation}Installation path options:
  --template-path            Print the directory where template files are
                             installed, then exit
  --flag-path                Print the directory where flag files are
                             installed, containing one subdirectory per
                             dispatch, then exit
  --include-path             Print the full path of the directory containing
                             the jitter/ header directory, then exit

${help_section_indentation}Architecture options:
  --build                    Print the system build triplet and exit
  --host                     Print the system host triplet and exit
  --assembly                 Print the Jitter assembly name for the host
                             system, if any, and exit

${help_section_indentation}GNU Libtextstyle wrapper options:
  --have-libtextstyle        Exit immediately, with success if the
                             Libtextstyle wrapper is available, with failure
                             otherwise.
  --libtextstyle-cppflags    Print C preprocessor flags needed to actaully use
                             the GNU Libtextstyle wrapper, if available; print
                             nothing if the wrapper is not available.
                             The user should conditionalise her C code
                             checking whether JITTER_WITH_LIBTEXTSTYLE is
                             defined: the flags shown by this macros either
                             define it from the command line or do nothing.
  --libtextstyle-ldadd       Print C compiler (driver) '-l' options for
                             linking Jitter's Libtextstyle wrapper library and
                             the Libtextstyle library itself into a program.
                             These flags are to be supplied at link time when
                             linking a program using the wrapper.
  --libtextstyle-libadd      Print C compiler (driver) arguments for linking
                             Jitter's Libtextstyle wrapper library and
                             the Libtextstyle library itself into another
                             library, using GNU LibTool.
                             These arguments are to be supplied at link time
                             when linking a larger library which inclues the
                             wrapper.

${help_section_indentation}GNU Readline wrapper/replacement options:
  --readline-ldadd           Print C compiler (driver) '-l' options for
                             linking Jitter's Readline wrapper/replacement
                             library and GNU Readline itself (when used) into
                             a program.
                             These flags are to be supplied at link time when
                             linking a program using the wrapper/replacement.
  --readline-libadd          Print C compiler (driver) arguments for linking
                             Jitter's Readline wrapper/replcament library and
                             GNU Readline itself (when used) into another
                             library, using GNU LibTool.
                             These arguments are to be supplied at link time
                             when linking a larger library which inclues the
                             wrapper.

${help_section_indentation}Debugging and scripting:
  --dump-version             Print the Jitter version only, without any
                             surrounding text; this is convenient for scripts

${help_section_indentation}Common GNU-style options:
  -?, --help                 Give this help list
  -V, --version              Print program version

Only long-format options can have arguments.

Report GNU Jitter bugs to: <bug-jitter@gnu.org>.
GNU Jitter home page: <https://www.gnu.org/software/jitter>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
EOF
    exit 0
}

# Print enabled dispatches, then exit with success.
dispatches_and_exit ()
{
    # The output should be a single line, which one space separating each
    # dispatch name, and no space at the beginning or the end.
    output=""
    for d in  switch direct-threading; do
        output="$output $d"
    done
    my_echo $output
    exit 0
}

# Print the best enabled dispatch, then exit with success.
best_dispatch_and_exit ()
{
    my_echo direct-threading
    exit 0
}


# Utility functions for printing.
################################################################

# Print a copy of stdin to stdout, suppressing spaces at the beginning and the
# at the end of lines, and contracting multiple-space sequences into single
# spaces.
# No arguments.
suppress_redundant_spaces ()
{
    /usr/bin/sed 's/^ *//;s/ *$//;s/  */ /g'
}

# Append the value of the given option for $dispatch to the output variable.
# One argument: the option name, lower-case.
append_to_output ()
{
    flagname="$1"
    if test "x$dispatch" = "x"; then
        dispatch="direct-threading"
    else
        dispatch="$dispatch"
    fi
    case "$dispatch" in
         switch)
              case "$flagname" in
                 cflags) new=" -O2 -fomit-frame-pointer -fno-reorder-functions -fno-lto -g0 -fno-var-tracking -fno-var-tracking-assignments -mregnames";;
                 cppflags) new="-DJITTER_DISPATCH_SWITCH=1";;
                 ldadd) new="-ljitter--switch";;
                 ldflags) new="-L/usr/lib64";;
                 libadd) new="libjitter--switch.la";;
                  *) fail_semantic "impossible: flag $flagname for switch";;
              esac;;
         direct-threading)
              case "$flagname" in
                 cflags) new=" -O2 -fomit-frame-pointer -fno-reorder-functions -fno-lto -g0 -fno-var-tracking -fno-var-tracking-assignments -mregnames";;
                 cppflags) new="-DJITTER_DISPATCH_DIRECT_THREADING=1";;
                 ldadd) new="-ljitter--direct-threading";;
                 ldflags) new="-L/usr/lib64";;
                 libadd) new="libjitter--direct-threading.la";;
                  *) fail_semantic "impossible: flag $flagname for direct-threading";;
              esac;;
         minimal-threading)
              case "$flagname" in
                 cflags) new="";;
                 cppflags) new="";;
                 ldadd) new="";;
                 ldflags) new="";;
                 libadd) new="";;
                  *) fail_semantic "impossible: flag $flagname for minimal-threading";;
              esac;;
         no-threading)
              case "$flagname" in
                 cflags) new="";;
                 cppflags) new="";;
                 ldadd) new="";;
                 ldflags) new="";;
                 libadd) new="";;
                  *) fail_semantic "impossible: flag $flagname for no-threading";;
              esac;;
        *)
            fail_semantic "this should never happen: dispatch $dispatch";;
    esac
    output="$output $new"
}

# Append the value of the given dispatch-independent option to the output
# variable.  One argument: the option name, lower-case.
append_to_output_dispatch_independent ()
{
    optionname="$1"
    case "$optionname" in
        libtextstyle-cppflags)
            new="-DJITTER_WITH_LIBTEXTSTYLE=1";;
        libtextstyle-ldadd)
            new="-ljitter-libtextstyle -ltextstyle -lncurses -lm ";;
        libtextstyle-libadd)
            new="/home/abuild/rpmbuild/BUILD/jitter-0.9.294/non-convenience-lib/libjitter-libtextstyle.la -ltextstyle -lncurses -lm ";;
        readline-ldadd)
            new="-ljitter-readline -lreadline -lncurses ";;
        readline-libadd)
            new="/home/abuild/rpmbuild/BUILD/jitter-0.9.294/non-convenience-lib/libjitter-readline.la -lreadline -lncurses ";;
        *)
            fail_semantic "this should never happen: optionname $optionname";;
    esac
    output="$output $new"
}

# Utility functions for common checks.
################################################################

# Fail with a fatal error if the current value of $option_argument is different
# from "".  This is used from the main loop, after recognizing that the current
# option $option supports no argument.
no_option_argument ()
{
    if test "x$option_argument" != "x"; then
        fail_usage "the option $option has no argument"
    fi
}

# Fail with a fatal error if the current value of $option_argument is "".  This
# is used from the main loop, after recognizing that the current option $option
# requires a mandatory argument.
mandatory_option_argument ()
{
    if test "x$option_argument" = "x"; then
        fail_usage "the option $option has a mandatory argument"
    fi
}


# Command-line checks.
################################################################

# Fail if there are no command-line arguments.
if test "$#" = "0"; then
    fail_usage "no option given"
fi


# Main loop.
################################################################

# Initialize a string containing the output to be printed in the end.
output=""

# Scan every option left-to-right.
while test "$#" != "0"; do
    argument="$1"
    shift

    # Fail if the argument is not an option.
    if ! (my_echo "$argument" | grep '^-' > /dev/null); then
        fail_usage "$argument is a non-option argument"
    fi

    # At this point $argument is either an option or an option with an argument.
    # Split the two parts.
    if my_echo "$argument" | grep '=' > /dev/null; then
        # There is an option argument.  Split around the '=' character.
        # Notice that arguments with options only support the GNU-style syntax
        # --long-option=value ; the '=' character is part of $argument .  Short
        # options cannot have arguments here.
        option="$(my_echo $argument | /usr/bin/sed 's/=.*$//')"
        option_argument="$(my_echo $argument | /usr/bin/sed 's/^[^=]*=//')"
    else
        # There is no argument.
        option="$argument"
        option_argument=""
    fi

    case "$option" in
        # Configuration options
        --best-dispatch)
            no_option_argument
            best_dispatch_and_exit;;
        --dispatches)
            no_option_argument
            dispatches_and_exit;;
        --has-dispatch)
            mandatory_option_argument
            for m in  switch direct-threading; do
                if test "x$option_argument" = "x$m"; then
                    exit 0
                fi
            done
            exit 1;;

        # dispatch selection:
        --dispatch)
            mandatory_option_argument
            known="no"
            for m in  switch direct-threading; do
                if test "x$option_argument" = "x$m"; then
                    known="yes"
                fi
            done
            if test "x$known" != "xyes"; then
                fail_semantic "unknown or disabled dispatch \"$option_argument\""
            fi
            dispatch="$option_argument"
            ;;

        # Compilation/linking flag options for the best dispatch:
        --cflags)
            no_option_argument
            append_to_output cflags;;
        --cppflags)
            no_option_argument
            append_to_output cppflags
            # Unless cross-compiling, append a -I argument referring the
            # installation path.  This is defined separately from the rest, as
            # the installation prefix can be decided very late, at Jitter
            # installation time.
            # (When cross-compiling this would be difficult to do correctly.)
            if test "xno" != 'xyes'; then
              output="$output -I $includedir"
            fi;;
        --ldadd)
            no_option_argument
            append_to_output ldadd;;
        --ldflags)
            no_option_argument
            append_to_output ldflags;;
        --libadd)
            no_option_argument
            append_to_output libadd;;

        # Installation path options.
        --template-path)
            no_option_argument
            echo_and_exit "$templatedir";;
        --flag-path)
            no_option_argument
            echo_and_exit "$flagdir";;
        --include-path)
            no_option_argument
            echo_and_exit "$includedir";;

        # Architecture options.
        --host)
            no_option_argument
            echo_and_exit "powerpc64-suse-linux-gnu";;
        --build)
            no_option_argument
            echo_and_exit "powerpc64-suse-linux-gnu";;
        --assembly)
            no_option_argument
            if test "xdummy" != 'xdummy'; then
                my_echo "dummy"
            fi
            exit 0;;

        # GNU Libtextstyle wrapper options.
        --have-libtextstyle)
            no_option_argument
            if test "x-DJITTER_WITH_LIBTEXTSTYLE=1" != 'x'; then
                exit 0
            else
                exit 1
            fi;;
        --libtextstyle-cppflags)
            no_option_argument
            append_to_output_dispatch_independent libtextstyle-cppflags;;
        --libtextstyle-ldadd)
            no_option_argument
            append_to_output_dispatch_independent libtextstyle-ldadd;;
        --libtextstyle-libadd)
            no_option_argument
            append_to_output_dispatch_independent libtextstyle-libadd;;

        # GNU Readline wrapper (or replacement) options.
        --readline-ldadd)
            no_option_argument
            append_to_output_dispatch_independent readline-ldadd;;
        --readline-libadd)
            no_option_argument
            append_to_output_dispatch_independent readline-libadd;;

        # Debugging and scripting options.
        --dump-version)
            no_option_argument
            echo_and_exit "UNKNOWN";;

        # Common GNU-style options.
        --version|-V)
            no_option_argument
            version;;
        --help|-\?)
            no_option_argument
            help;;

        # Unrecognized option.
        *)
            fail_usage "invalid option $option";;
    esac
done

# If we've not exited yet, we have composed an output to be printed at the end
# at this point.  Do that and we can exit with success.
# Here we suppress redundant spaces.
my_echo "$output" | suppress_redundant_spaces
