#!@SH_PROG@
# -*- shell-script -*-
#   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
#   2009  Rocky Bernstein rocky@gnu.org
#
#   bashdb 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.
#
#   bashdb 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 bashdb; 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.

[[ -z $_Dbg_ver ]] && typeset _Dbg_ver=\
'$Id: bashdb.in,v 1.48 2008/10/30 08:22:23 rockyb Exp $'

# This routine gets called via the -c or --command option and its sole
# purpose is to capture the command string such as via "x $*" or 
# in a traceback ("where").
function _Dbg_eval {
  eval $*  
}

# Name we refer to ourselves by
typeset _Dbg_debugger_name='@PACKAGE@'

# The shell we are configured to run under.
typeset _Dbg_shell='@SH_PROG@'

# The release name we are configured to run under.
typeset _Dbg_release='@PACKAGE_VERSION@'

# 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 ###
typeset prefix=@prefix@
typeset _Dbg_libdir=@PKGDATADIR@
###

[[ ! -d $_Dbg_libdir ]] && _Dbg_libdir='.'
# Parse just the libdir option
# Show basename only in location listing. This is needed in regression tests
typeset -i _Dbg_basename_only=${BASHDB_BASENAME_ONLY:-0}

typeset    _Dbg_bashdb_main=@BASHDB_MAIN@
typeset -x _Dbg_libdir=${_Dbg_bashdb_main%/*}  # dirname(_Dbg_bashdb_main)
# typeset    _Dbg_libdir=@PKGDATADIR@

typeset    _Dbg_main=dbg-main.sh
typeset    _Dbg_bindir=$(dirname $0)

# What to set for location of helper routines? 
if [[ ! -e $_Dbg_libdir/$_Dbg_main ]] ; then
  # Use bindir/../share as fallback
    _Dbg_libdir=
    if [[ -d $_Dbg_bindir/../share/bashdb ]] ; then
      _Dbg_libdir=$_Dbg_bindir/../share/bashdb
    fi
fi

# Parse just the library option
typeset -ax _Dbg_script_args=("$@")
typeset -i _Dbg_i
for ((_Dbg_i=0; _Dbg_i<${#_Dbg_script_args[@]}-1; _Dbg_i++)) ; do 
    typeset arg=${_Dbg_script_args[$_Dbg_i]}
    if [[ $arg == '-L' || $arg == '--library' ]] ; then
	((_Dbg_i++))
	_Dbg_libdir="${_Dbg_script_args[$_Dbg_i]}"
	break
    fi
done
if [[ ! -d $_Dbg_libdir ]] && [[ ! -d $_Dbg_libdir ]] ; then 
  echo "${_Dbg_pname}: Can't read debugger library directory '${_Dbg_libdir}'."
  echo "${_Dbg_pname}: Perhaps @PACKAGE@ is installed wrong (if its installed)." >&2
  echo "${_Dbg_pname}: Try running @PACKAGE@ using -L (with a different directory)." >&2
  echo "${_Dbg_pname}: Run @PACKAGE@ --help for a list and explanation of options." >&2
  exit 1
fi

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

. $_Dbg_libdir/dbg-main.sh ;

# I don't know why when this is done in dbg-opts.sh it doesn't have
# an effect.
((OPTLIND > 0)) && shift "$((OPTLIND - 1))"

if (($# == 0)) && [[ -z $_Dbg_EXECUTION_STRING ]] ; then
    echo >&2 "${_Dbg_pname}: need to give a script to debug or use the -c option."
    exit 1
fi
    
_Dbg_script_file="$1"
shift

if [[ ! -d $_Dbg_tmpdir ]] && [[ ! -w $_Dbg_tmpdir ]] ; then
  echo "${_Dbg_pname}: cannot write to temp directory $_Dbg_tmpdir." >&2
  echo "${_Dbg_pname}: Use -T try directory location." >&2
  exit 1
fi

# Note that this is called via bashdb rather than "bash --debugger"
_Dbg_script=1

if [[ -n $_Dbg_EXECUTION_STRING ]] ; then
    _Dbg_script_file=$(_Dbg_tempname cmd)
    echo "$_Dbg_EXECUTION_STRING" >$_Dbg_script_file
fi

if [[ ! -r "$_Dbg_script_file" ]] ; then
    echo "${_Dbg_pname}: cannot read program to debug: ${_Dbg_script_file}." >&2
    exit 1
fi

typeset -r _Dbg_Dbg_script_file=$(_Dbg_expand_filename $_Dbg_script_file)

if ((_Dbg_linetrace)) ; then 
  # No stepping.
  _Dbg_write_journal_eval "_Dbg_step_ignore=-1" 
  _Dbg_QUIT_ON_QUIT=1
else 
  # Set to skip over statements up to ". $_Dbg_script_file"
  _Dbg_write_journal_eval "_Dbg_step_ignore=3" 
fi
_Dbg_init_default_traps
trap '_Dbg_debug_trap_handler 0 "$BASH_COMMAND" "$@"' DEBUG
set -o functrace
. "$_Dbg_script_file"

# end of bashdb
