#!/usr/bin/bash.exe
#   Copyright (C) 2002, 2003, 2004, 2005 Rocky Bernstein rocky@panix.com
#
#   Bash 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.
#
#   Bash 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 Bash; see the file COPYING.  If not, write to the Free Software
#   Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
#
# Alternate way to invoke debugger. bash --debugger however is preferred.

typeset _Dbg_ver=\
'$Id: bashdb.in,v 1.6.2.1 2004/08/21 13:29:56 rockyb Exp $'

declare -a _Dbg_script_args="$@"

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

# Show basename only in location listing. This is needed in regression tests
typeset -i _Dbg_basename_only=${BASHDB_BASENAME_ONLY:-0}

typeset _Dbg_main=dbg-main.inc
typeset _Dbg_libdir=/usr/share/bashdb
typeset _Dbg_bindir=$(dirname $0)
typeset _Dbg_tmpdir=/tmp

typeset _Dbg_cmd='' # If command string given on command line, this is it.


_Dbg_usage() {
  printf "_Dbg_usage:
    ${_Dbg_pname} [OPTIONS] <script_file>

Runs script_file under a (primitive) debugger.

options: 
    -B           basename only on source listings. (Needed in regression tests)
    -h           print this help
    -n           Don't run initialization files
    -c command   Run this passed command of a script
    -q           Quiet. Do not print introductory and quiet messages.
    -x cmdfile   execute commands from cmdfile
    -L libdir    set directory location of library helper file: $_Dbg_main
                 the default directory is: $_Dbg_libdir
    -T tmpdir    set directory location for temporary files: $_Dbg_tmpdir
    -t tty       set debuger terminal
    -V           show version number and no-warranty and exit.
" 1>&2
}

# 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

while getopts BhnqVc:t:x:L:T: opt; do
  case $opt in
    B) _Dbg_basename_only=1 ;;
    c) _Dbg_cmd="$OPTARG" ;;
    h) _Dbg_usage; exit 100 ;;
    n) _Dbg_no_init=1 ;;
    q) _Dbg_quiet=1 ;;
    x) BASHDB_INPUT="$BASHDB_INPUT $OPTARG" ;;  
    L) _Dbg_libdir=$OPTARG ;;
    T) _Dbg_tmpdir=$OPTARG ;;
    t) 
      if ! $(touch $OPTARG >/dev/null 2>/dev/null); then 
	echo "${_Dbg_pname}: Can't access $OPTARG for writing."
      elif [[ ! -w $OPTARG ]] ; then
	echo "${_Dbg_pname}: terminal $OPTARG needs to be writable."
      else
	_Dbg_tty=$OPTARG
      fi
      ;;
    V) show_version=1 ;;
    *) _Dbg_usage; exit 2 ;;
  esac
done
shift $(($OPTIND - 1))

[[ $# == 0 && -z $show_version && -z $_Dbg_cmd ]] && {
  echo "${_Dbg_pname}: Need to give a script name to debug."
  exit 1
}

if [[ ! -d $_Dbg_libdir ]] && [[ ! -d $_Dbg_libdir ]] ; then 
  echo "${_Dbg_pname}: cannot read $_Dbg_libdir. " \
  "Perhaps bashdb is installed wrong." >&2
  echo "${_Dbg_pname}: or try using -L (with a different directory)." >&2
  exit 1
fi

_source_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

# Cygwin installation hack - 'make check' on VPATH doesn't quite work...
[[ -r $_Dbg_libdir/$_Dbg_main ]] ||
[[ -r $_Dbg_libdir/.build/$_Dbg_main ]] || {
  echo "${_Dbg_pname}: cannot read debugger file $_Dbg_libdir/$_Dbg_main." >&2
  echo "${_Dbg_pname}: Perhaps bashdb is installed incorrectly." >&2
  exit 1
}

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

# Cygwin installation hack - 'make check' on VPATH doesn't quite work...
if [ -r ${_Dbg_libdir}/dbg-pre.inc ] ; then
. ${_Dbg_libdir}/dbg-pre.inc
else
. ${_Dbg_libdir}/.build/dbg-pre.inc
fi

if [[ -z $_Dbg_quiet ]] ; then 
  echo "Bourne-Again Shell Debugger, release $_Dbg_release"
  cat <<EOF
Copyright 2002, 2003, 2004 Rocky Bernstein
This is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.

EOF
fi

if (( show_version == 1 )) ; then 
cat <<EOF
There is absolutely no warranty for BASHDB.  Type "show warranty" for details.
EOF
  exit 1
fi

if [[ ! -r "$_source_file" ]] && [[ -z $_Dbg_cmd ]] ; then
  echo "${_Dbg_pname}: cannot read program to debug: $_source_file." >&2
  exit 1
else
  typeset -r _Dbg_source_file=$(_Dbg_expand_filename $_source_file)
fi

# Cygwin installation hack - 'make check' on VPATH doesn't quite work...
if [ -r ${_Dbg_libdir}/dbg-pre.inc ] ; then
. $_Dbg_libdir/dbg-main.inc
else
. $_Dbg_libdir/.build/dbg-main.inc
fi
set -o functrace
if [[ -z $_Dbg_cmd ]] ; then 
  . $_source_file
else 
  eval $_Dbg_cmd
fi


# end of bashdb

#;;; Local Variables: ***
#;;; mode:shell-script ***
#;;; eval: (sh-set-shell "bash") ***
#;;; End: ***

