#! /bin/sh
# I will put fairly copious comments here to explain what is going on
# even though those will carry a (slight)cost.
#
# First, I will want to place this script alongside various Reduce
# executables (and resources), but when I invoke this my current directory
# could be pretty arbitrary. So the first thing I will do will be to
# find the directory that this script is in and set a variable "here" to
# identify it.
here=`dirname "$0"`
here=`cd "$here" ; pwd`
#
# This is a shell script and it only gets places in Reduce directories for
# variants on Windows. There are however several cases that can arise! The
# ones I am concerned with are
#   cygwin64
#   cygwin32
#   mingw
# If there are any others then they will be treated as per the mingw case.
# The cygwin32 case is deprecated - indeed in general 32-bit working on
# a Windows platform now belongs in the past!
# With the two cygwin cases there is an additional issue as to whether
# the shell variable DISPLAY is set. If it is set at all I will assume it
# identifies an X11 screen that is available for use.
#
# Check which platform is in use...
case `uname -m -o` in
x86_64*Cygwin)
  for x in $*
  do
    if test "$x" = "--nogui" ||
       test "$x" = "-w-" ||
       test "$x" = "-w"
    then
      withgui="no"
      break
    fi
  done
# On Cygwin (and here I have the cygwin64 case) I will behave as follows:
#    --nogui or -w or -w- present on the command line: these indicate
# that a console style use of Reduce is required. So run the explicitly
# cygwin64 variant so it knows how to interact with a cygwin console.
# Also if the --nogui option is not selected so a GUI is requested and
# if DISPLAY is set also use the cygwin64 version that will use X11 for its
# display.
  if test "$withgui" = "no" || ! test -z "$DISPLAY"
  then
    $here/cygwin64-bootstrapreduce.exe $*
  else
# Otherwise the request was for the default behaviour, which is to use
# the GUI, and the Windows-native version should be the one used.
    $here/bootstrapreduce.com $*
  fi
  ;;

# The cygwin32 version is essentially the same.
i686*Cygwin)
  withgui="yes"
  for x in $*
  do
    if test "$x" = "--nogui" ||
       test "$x" = "-w-" ||
       test "$x" = "-w"
    then
      withgui="no"
      break
    fi
  done
# On Cygwin (and here I have the cygwin32 case) I will behave as follows:
#    --nogui or -w or -w- present on the command line: these indicate
# that a console style use of Reduce is required. So run the explicitly
# cygwin32 variant so it knows how to interact with a cygwin console.
# Also if the --nogui option is not selected so a GUI is requested and
# if DISPLAY is set also use the cygwin32 version that will use X11 for its
# display.
  if test "$withgui" = "no" || ! test -z "$DISPLAY"
  then
    $here/cygwin32-bootstrapreduce.exe $*
  else
# Otherwise the request was for the default behaviour, which is to use
# the GUI, and the Windows-native version should be the one used.
    $here/bootstrapreduce.com $*
  fi
  ;;
*)
# With non-cygwin cases always use the windows-native variant which will
# use the Windows console API for keyboard access and the Windows display
# scheme if it launches a GUI.
  $here/bootstrapreduce.com $*
  ;;
esac
exit

# When I first had this script the normal situation regarding cygwin was to
# run under the 32-bit cygwin system even when the underlying Widnows system
# was 64-bit. Only at a later stage was the 64-bit cygwin first available,
# then stable and finally the proper version to use by default. As of early
# 2020 Cygwin essentially deprecate their 32-bit offering. Also in those
# earlier days the console environment provided by and with cygwin was a bash
# shell running within a Windows console, whereas now for quite some time
# one has used "mintty" - that change had implications for low level terminal
# handling. So it USED to be that case that it was necessary to run any
# bootstrapreduce that was directly attached to the console and had been launched from
# a cygwin shell as the cygwin-specific version, but desirable to run batch
# uses (eg when input and output were provided via files or pipes) as the
# native windows version that could thereby take advantage of all system
# memory. So the predecessor of this script had checks for "not under cygwin"
# which tested in there was direct access to a Windows console available and
# "cygwin isatty" to see if stdin and stdout where attached to the default
# cygwin terminal.
# At some stage something changed within either cygwin or windows that rendered
# the tests for all that non-workable, and meanwhile with essentially every
# cygwin installation being a cygwin64 one there is no longer a motivation for
# using a windows binary for cases such as "redcsl <input >output" because the
# cygwin version has as much access to memory. So this script is now cheaper
# and simpler than it used to be. I believe that the main alteration will be
# that if a user is running from a cygwin32 shell and they launch bootstrapreduce with
# standard IO redirected or via a pipe that will now still get the cygwin32
# version run and will not benefit from any extra memory that the native
# windows might be able to provide. That seems a use-case that will be
# increasingly uncommon and such users can be directed at the cygwin.com
# web page and advised to update to use cygwin64. Of course if they are on
# a 32-bit version of windows still then the change should agian hardly make
# any difference to them!
#   Arthur Norman. January 2020
