#!/usr/bin/bash
# -*- shell-script -*-
# Top-level debugger program. This program may be initially invoked.
#
#   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008,
#   2009, 2010, 2011, 2019 Rocky Bernstein <rocky@gnu.org>
#
#   This program 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 2, or
#   (at your option) any later version.
#
#   This program 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 this program; see the file COPYING.  If not, write to
#   the Free Software Foundation, 59 Temple Place, Suite 330, Boston,
#   MA 02111 USA.
#

# The alternate way to invoke debugger, "bash --debugger", has some
# advantages: it sets $0 correctly and doesn't show this script in
# the call trace. However the bash has been a bit inflexible and
# quirky so sadly this script seems to be needed more than it would
# normally.

# Name we refer to ourselves by
typeset _Dbg_debugger_name='bashdb'

# The shell we are configured to run under.
typeset _Dbg_shell='/usr/bin/bash'

# The release name we are configured to run under.
typeset _Dbg_release='5.2-1.2.0'

# Wrapper for debugger called from within bash via bash --debugger
typeset _Dbg_bashdb_main=''

# The short shell name. Helps keep code common in bash, zsh, and ksh debuggers.
typeset _Dbg_shell_name=${_Dbg_shell##*/}  # Equivalent to basename(_Dbg_shell)

# Original $0. Note we can't set this in an include.
typeset _Dbg_orig_0=$0

# Equivalent to basename $0; the short program name
typeset _Dbg_pname=${0##*/}

## Stuff set by autoconf/configure ###
# ${prefix}/share/bashdb often uses $prefix, _Dbg_libdir must be a global variable
_Dbg_assign_libdir() { typeset prefix="/usr"; _Dbg_libdir="${prefix}/share/bashdb"; }; _Dbg_assign_libdir
###

# We agonize a bit over _Dbg_libdir: the root directory for where
# debugger code is stored.
[[ -d "$_Dbg_libdir" ]] || _Dbg_libdir="${_Dbg_bashdb_main%/*}"  # dirname(_Dbg_bashdb_main)
[[ -d "$_Dbg_libdir" ]] || _Dbg_libdir='.'

# More _Dbg_libdir finding: respect any library option the user has
# supplied over any of the above guesses. Go over options and parse
# just the library option.
typeset -xa _Dbg_script_args; _Dbg_script_args=("$@")

typeset -i _Dbg_i
for ((_Dbg_i=0; _Dbg_i<${#_Dbg_script_args[@]}-1; _Dbg_i++)) ; do
    typeset _Dbg_script_arg=${_Dbg_script_args[$_Dbg_i]}
    if [[ $_Dbg_script_arg == '-L' || $_Dbg_script_arg == '--library' ]] ; then
	((_Dbg_i++))
	_Dbg_libdir="${_Dbg_script_args[$_Dbg_i]}"
	break
    elif [[ $_Dbg_script_arg == '--' ]] ; then
	# We hit the end of bashdb args
	break
    fi
done

# All _Dbg_libdir setting done above. Have we succeeded?
if [[ ! -d "$_Dbg_libdir" ]] ; then
  echo "${_Dbg_pname}: Can't read debugger library directory '${_Dbg_libdir}'." >&2
  echo "${_Dbg_pname}: Perhaps bashdb is installed wrong (if its installed)." >&2
  echo "${_Dbg_pname}: Try running bashdb using -L (with a different directory)." >&2
  echo "${_Dbg_pname}: Run bashdb --help for a list and explanation of options." >&2
  exit 1
fi

typeset _Dbg_main="$_Dbg_libdir/dbg-main.sh"
if [[ ! -r "$_Dbg_main" ]] ; then
  print "${_Dbg_pname}: Can't read debugger library file '${_Dbg_main}'." >&2
  print "${_Dbg_pname}: Perhaps bashdb is installed wrong (if its installed)." >&2
  print "${_Dbg_pname}: Try running bashdb using -L (with a different directory)." >&2
  print "${_Dbg_pname}: Run bashdb --help for a list and explanation of options." >&2
  exit 1
fi

. "${_Dbg_libdir}/bashdb-part2.sh"

if [[ -n $_Dbg_script_file ]] ; then
    # Set $0. This is a new bash 5.0 feature.
    BASH_ARGV0="$_Dbg_script_file"
fi
typeset _Dbg_dollar_0="$0"

# Resolve and save mapping for main script, and resolve
# the starting directory.
_Dbg_full_filename="$(_Dbg_is_file "$_Dbg_script_file")"
_Dbg_file2canonic["${_Dbg_script_file}"]="$_Dbg_full_filename"
# Note: expand_filename is expanding a *directory* here, not a filename.
# This might cause a problem in the future if _Dbg_expand_filename becomes
# more specific about this aspect.
_Dbg_init_cwd="$(_Dbg_expand_filename "${_Dbg_script_file%/*}")"

trap '_Dbg_debug_trap_handler 0 "$BASH_COMMAND" "$@"' DEBUG
set -o functrace
. "$_Dbg_script_file"
