#!/usr/bin/zsh -f
# -*- shell-script -*-
# Top-level debugger program. This program may be initially invoked.
#
#   Copyright (C) 2008-2011, 2014 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.

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

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

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

# 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=/usr  # ${prefix}/share/zshdb often uses $prefix
typeset _Dbg_libdir=${prefix}/share/zshdb
###

# We agonize a bit over _Dbg_libdir: the root directory for where
# debugger code is stored. Below is just the first guess...
[[ -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.
local -a libdir
zparseopts -a libdir -E L: -library:
if (( ${#libdir} > 0 )) ; then
    typeset -a lib_opts; eval "lib_opts=($libdir)"
    if [[ ! -d ${lib_opts[2]} ]] ; then
	print "${lib_opts[2]} is not a directory" >&2
	exit 1
    fi
    _Dbg_libdir=${lib_opts[2]}
    unset lib_opts
fi
unset libdir

# 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}'."
  echo "${_Dbg_pname}: Perhaps zshdb is installed wrong (if its installed)." >&2
  echo "${_Dbg_pname}: Try running zshdb using -L (with a different directory)." >&2
  echo "${_Dbg_pname}: Run zshdb --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}'."
  print "${_Dbg_pname}: Perhaps zshdb is installed wrong (if its installed)." >&2
  print "${_Dbg_pname}: Try running zshdb using -L (with a different directory)." >&2
  print "${_Dbg_pname}: Run zshdb --help for a list and explanation of options." >&2
  exit 1
fi

# Pull in the rest of the debugger code.
. $_Dbg_main

_Dbg_set_debugger_entry

# Note that this is called via zshdb rather than zshdb-trace
_Dbg_script=1

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

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

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

# Directory in which the script is located
typeset _Dbg_cdir=${_Dbg_source_file%/*}

if ((_Dbg_set_history)) && [[ -r $_Dbg_histfile ]] ; then
    fc -ap $_Dbg_histfile $_Dbg_history_length $_Dbg_history_length
fi

## Skip for now
## _Dbg_init_default_traps

if [[ -n $_Dbg_script_file ]] ; then
    # Set $0
    0=$_Dbg_script_file
fi
typeset _Dbg_dollar_0=$0

while : ; do
  # trap 'echo ERR encountered inside debugger' ERR

  _Dbg_set_to_return_from_debugger
  _Dbg_step_ignore=2
  emulate -R zsh
  trap '_Dbg_trap_handler $? "$0" "$@"
    case $_Dbg_rc in
    2 ) setopt errexit ;;
    255 ) return $_Dbg_return_rc ;;
    *   ) ;;
    esac' DEBUG
  . "$_Dbg_source_file" ${_Dbg_script_args[@]}
  trap '' DEBUG
  _Dbg_msg "Program terminated. Type 's' or 'R' to restart."
  _Dbg_process_commands
done
