#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - nosqlmain    *
#  ###  ##        ####### ####### ##     **      $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
# Main NoSQL v2 driver
#
#############################################################################

#############################################################################
# Auto-configuration START
#############################################################################
#
# Source the system- and user-configuration files.
#

# If NSQLIB is unset try auto-detection.
if test -z "${NSQLIB}"
then
  # Try PREFIX/lib (e.g. /usr/lib/ ).
  NSQLIB="$(expr $(which nosql) : '\(.*\).*/.*/.*$')/lib/nosql"
fi

export NSQLIB

if test -f ${NSQLIB}/nosql.conf
then
  if test ! $(${NSQLIB}/bin/modeof ${NSQLIB}/nosql.conf) -gt 664
  then
    . ${NSQLIB}/nosql.conf
  else
    echo "nosql: ${NSQLIB}/nosql.conf insecure file permissions" >&2
  fi
fi

if test -f ${HOME}/.nosql.conf
then
  if test -O ${HOME}/.nosql.conf \
    -a ! $(${NSQLIB}/bin/modeof ${HOME}/.nosql.conf) -gt 664
  then
    . ${HOME}/.nosql.conf
  else
    echo "nosql: ${HOME}/.nosql.conf insecure file permissions" >&2
  fi
fi

#
# Set important variables to reasonable defaults if still unset.
#
export NSQAWK=${NSQAWK:=awk}
export NSQSH=${NSQSH:=sh}
export NSQPERL=${NSQPERL:=perl}
export NSQMYLIB=${NSQMYLIB:=${NSQLIB}/mylib}
export NSQLOCKER="${NSQLOCKER:=${NSQSH} ${NSQLIB}/sh/lock}"
export NSQTEMPF="${NSQTEMPF:=${NSQSH} ${NSQLIB}/sh/tempfile}"

#############################################################################
# Auto-configuration END
#############################################################################

nosql () {

  # Stop right away if NSQLIB is unset.
  test -n "${NSQLIB}" || return 1

  # Same thing if no arguments were supplied.
  test $# -eq 0 && return 1

  #####################################################################
  # Allow for the default environment to be overridden upon invocation.
  # Warning: overrides last even when returning to the calling shell,
  # until they are manually overridden again with new values.
  #####################################################################

  export NSQAWK NSQSH NSQPERL NSQMYLIB NSQLOCKER NSQTEMPF


  # Misc. vars.
  local __nosql_cmd=
  local __nosql_args=
  local __nosql_myfile=
  local __nosql_tmpvar=
  local __nosql_tmpfile=
  local __nosql_path=${PATH}
  local __nosql_locklist=
  local __nosql_rc=0
  local __nosql_version=

  # Command name
  __nosql_cmd=$1; shift

  # Each called program has its own specific set of switches and
  # parameters; nosqlmain() takes any command line options and 
  # arguments and simply passes them all on to the called program,
  # either as the usual "$@" string in case of executable programs,
  # or as __nosql_args="$*" for awk scripts.

  # Run the requested command.
  case ${__nosql_cmd} in
    addcol)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/addcol
      __nosql_rc=$?
    ;;
    addtypes)
      ${NSQSH} ${NSQLIB}/sh/addtypes "$@"
      __nosql_rc=$?
    ;;
    body)
      tail +3
      __nosql_rc=$?
    ;;
    cat|rollback)
      ${NSQSH} ${NSQLIB}/sh/cat "$@"
      __nosql_rc=$?
    ;;
    column|col|project)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/column
      __nosql_rc=$?
    ;;
    compute)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/compute
      __nosql_rc=$?
    ;;
    dashline)
      sed -n 2p
      __nosql_rc=$?
    ;;
    datatype)
      nosql addtypes "$@" | ${NSQAWK} -f ${NSQLIB}/awk/datatype
      __nosql_rc=$?
    ;;
    edit)
      ${NSQSH} ${NSQLIB}/sh/edit "$@"
      __nosql_rc=$?
    ;;
    fcompute)
      __nosql_tmpfile=$(${NSQTEMPF})
      ${NSQAWK} -v __nosql_args="$*" \
        -v __nosql_tmpfile=${__nosql_tmpfile} -f ${NSQLIB}/awk/fcompute
      __nosql_rc=$?
      /bin/rm -f ${__nosql_tmpfile}
    ;;
    field)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/field
      __nosql_rc=$?
    ;;
    fieldsof)
      # Use switch "-s" to see surrounding spaces in field names.
      if test "$1" = "-s"
      then
        head -1 | tr '\t' '\n' | cat -vte
        __nosql_rc=$?
      else
        head -1 | tr '\t' '\n'
        __nosql_rc=$?
      fi
    ;;
    fromRDB)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/fromRDB
      __nosql_rc=$?
    ;;
    header)
      head -2
      __nosql_rc=$?
    ;;
    headline)
      head -1
      __nosql_rc=$?
    ;;
    help)
      cat <<-_EOF_ >&2

          For help on NoSQL please point an HTML browser at
          the file URL file:$(expr ${NSQLIB} : '\(.*\).*/.*/.*$')/doc/nosql/NoSQL.html

          For example:
          lynx file:$(expr ${NSQLIB} : '\(.*\).*/.*/.*$')/doc/nosql/NoSQL.html

_EOF_
    ;;
    index)
      ${NSQPERL} ${NSQLIB}/perl/index "$@"
      __nosql_rc=$?
    ;;
    inscol)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/inscol
      __nosql_rc=$?
    ;;
    islist)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/islist
      __nosql_rc=$?
    ;;
    istable)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/istable
      __nosql_rc=$?
    ;;
    join)
      __nosql_tmpfile=$(${NSQTEMPF})
      ${NSQAWK} -v __nosql_args="$*" \
        -v __nosql_tmpfile=${__nosql_tmpfile} -f ${NSQLIB}/awk/join
      __nosql_rc=$?
      /bin/rm -f ${__nosql_tmpfile}
    ;;
    justify)
      nosql addtypes "$@" | ${NSQAWK} -f ${NSQLIB}/awk/justify
      __nosql_rc=$?
    ;;
    listtotable)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/listtotable
      __nosql_rc=$?
    ;;
    lock)
      for __nosql_tmpvar in $@ ; do
        __nosql_locklist="${__nosql_locklist} ${__nosql_tmpvar}.LCK"
      done

      ${NSQLOCKER} ${__nosql_locklist}
      __nosql_rc=$?
    ;;
    maketable)
      ${NSQAWK} -f ${NSQLIB}/awk/maketable
      __nosql_rc=$?
    ;;
    merge)
      ${NSQPERL} ${NSQLIB}/perl/merge "$@"
      __nosql_rc=$?
    ;;
    mjoin)
      ${NSQPERL} ${NSQLIB}/perl/mjoin "$@"
      __nosql_rc=$?
    ;;
    modeof)
      ${NSQLIB}/bin/modeof "$@"
      __nosql_rc=$?
    ;;
    myprog)
      # PATH can be 'last', 'asis' or 'first' (default).
      case $1 in
        -l*|--path-last) __nosql_path=${__nosql_path}:${NSQMYLIB} ; shift ;;
        -a*|--path-asis) shift ;;
        *) __nosql_path=${NSQMYLIB}:${__nosql_path} ;;
      esac
      test -z "$1" && return 1
      __nosql_myfile=$1 ; shift
      PATH=${__nosql_path} ${NSQMYLIB}/${__nosql_myfile} "$@"
      __nosql_rc=$?
    ;;
    not)
      if test $# -lt 1
      then
        echo "nosql not: missing command name" >&2
        __nosql_rc=1
      else
        nosql "$@"
        if test $? -eq 0
        then
          __nosql_rc=255
        else
          __nosql_rc=0
        fi
      fi
    ;;
    null)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/null
      __nosql_rc=$?
    ;;
    pick)
      ${NSQLIB}/bin/pick "$@"
      __nosql_rc=$?
    ;;
    print)
      nosql addtypes | ${NSQAWK} -f ${NSQLIB}/awk/preprint |
        ${NSQPERL} ${NSQLIB}/perl/print "$@" | tr -d '\f'
      __nosql_rc=$?
    ;;
    pull)
      if test -z "$1"
      then
        echo "Usage: nosql pull table" >&2
        __nosql_rc=1
      else
        __nosql_tmpvar="$(${NSQAWK} '
        NR == 3 { FS = OFS = "\t"; printf( "^%s" ,$1 ) }
        NR > 3 { printf( "|^%s" ,$1 ) }')"
        head -2 $1
        tail +3 $1 | grep -E -w "${__nosql_tmpvar}"
        __nosql_rc=$?
      fi
    ;;
    pwmatch8)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/pwmatch8
      __nosql_rc=$?
    ;;
    pwcrypt8)
      ${NSQAWK} -f ${NSQLIB}/awk/pwcrypt8
      __nosql_rc=$?
    ;;
    record)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/record
      __nosql_rc=$?
    ;;
    rename)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/rename
      __nosql_rc=$?
    ;;
    repair)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/repair
      __nosql_rc=$?
    ;;
    rmcol)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/rmcol
      __nosql_rc=$?
    ;;
    row|select)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/row
      __nosql_rc=$?
    ;;
    search)
      ${NSQPERL} ${NSQLIB}/perl/search "$@"
      __nosql_rc=$?
    ;;
    see)
      cat -vte "$@"
      __nosql_rc=$?
    ;;
    setfirst)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/setfirst
      __nosql_rc=$?
    ;;
    shelltotable)
      ${NSQAWK} -v __nosql_args="$*" \
        -f ${NSQLIB}/awk/shelltotable
      __nosql_rc=$?
    ;;
    show)
      ${NSQSH} ${NSQLIB}/sh/show "$@"
      __nosql_rc=$?
    ;;
    sort)
      ${NSQAWK} \
        -v __nosql_args="$*" -f ${NSQLIB}/awk/sort
      __nosql_rc=$?
    ;;
	soundex)
	  ${NSQSH} ${NSQLIB}/sh/soundex "$@"
	  __nosql_rc=$?
	;;
    split)
      ${NSQAWK} \
        -v __nosql_args="$*" -f ${NSQLIB}/awk/split
      __nosql_rc=$?
    ;;
    subtotal)
      ${NSQPERL} ${NSQLIB}/perl/subtotal "$@"
      __nosql_rc=$?
    ;;
    summ)
      ${NSQPERL} ${NSQLIB}/perl/summ "$@"
      __nosql_rc=$?
    ;;
    tabletolist)
      ${NSQAWK} -f ${NSQLIB}/awk/tabletolist
      __nosql_rc=$?
    ;;
    tabletoshell)
      ${NSQAWK} -v __nosql_args="$*" \
        -f ${NSQLIB}/awk/tabletoshell
      __nosql_rc=$?
    ;;
    template)
      ${NSQAWK} -f ${NSQLIB}/awk/template
      __nosql_rc=$?
    ;;
    trim)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/trim
      __nosql_rc=$?
    ;;
    ucfirst)
      ${NSQAWK} -f ${NSQLIB}/awk/ucfirst
      __nosql_rc=$?
    ;;
    uniq)
      ${NSQPERL} ${NSQLIB}/perl/uniq "$@"
      __nosql_rc=$?
    ;;
    unlock)
      while [ $# -gt 0 ]
      do
        __nosql_tmpvar="${__nosql_tmpvar} $1.LCK" ; shift
      done
      [ "${__nosql_tmpvar}" ] && /bin/rm -f ${__nosql_tmpvar}
    ;;
    version)
      __nosql_version=$(cat ${NSQLIB}/Version)

      cat <<-_EOF_

      NoSQL version ${__nosql_version}

      NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
      This program comes with ABSOLUTELY NO WARRANTY; for details
      refer to the GNU General Public License.

      Type 'nosql show c' for the full copyright terms, and
      'nosql show w' for the complete writer's information.

      Please report bugs to nosql-bugs@mi.linux.it

_EOF_
    ;;
    weed)
      ${NSQAWK} -v __nosql_args="$*" -f ${NSQLIB}/awk/weed
      __nosql_rc=$?
    ;;
    whatis)
      if test -n "$*"
      then
        nosql cat ${NSQLIB}/cmdlist.rdb |
		  nosql row 'Command ~ "'"$*"'" || Description ~ "'"$*"'"' |
          nosql column Command Description |
          nosql print --trunc=60 --fold --page-size=0
      else
        nosql cat ${NSQLIB}/cmdlist.rdb |
          nosql column Command Description |
          nosql print --trunc=60 --fold --page-size=0
      fi
      __nosql_rc=$?
    ;;
    write|tee)
      ${NSQSH} ${NSQLIB}/sh/write "$@"
      __nosql_rc=$?
    ;;
    *)
      echo -e "nosql: unknown command '${__nosql_cmd}'" >&2
      __nosql_rc=1
    ;;
  esac

  return ${__nosql_rc}
}

