#! /bin/sh -e

# bin/couchjs.tpl.  Generated from couchjs.tpl.in by configure.

# Copyright (C) 2007 Noah Slater <nslater@bytesexual.org>.

# This file 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 of the License, or (at your option) any later
# version.

# This file 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 file; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

SCRIPT_OK=0
SCRIPT_ERROR=1

DEFAULT_VERSION=170

display_version () {
    # Display version and copyright information.

    cat << EOF
couchjs - CouchDB 0.7.2

Copyright (C) 2007 Damien Katz <damien_katz@yahoo.com>.
Copyright (C) 1998 Netscape Communications Corporation.

This program is free software; you can redistribute it and/or modify it under
the terms of the Mozilla Public License as published by the Mozilla Foundation;
either version 1.1 of the License, 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 Mozilla Public License for more details.

You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.

Written by Damien Katz <damien_katz@yahoo.com> and the Netscape
Communications Corporation.
EOF
}

display_help () {
    # Display a short description of the script's behaviour.

    basename=$1
    cat << EOF
Usage: $basename [OPTION]... [FILE] [ARG]...

The $basename command runs the CouchDB JavaScript interpreter shell.

CouchDB uses a custom version of the SpiderMonkey engine originally
writen by the Netscape Communications Corporation.

For more details about SpiderMonkey see:

  <http://developer.mozilla.org/en/docs/SpiderMonkey>

The exit status is 0 for success or 1 for failure.

Options:

  -h          display a short help message and exit
  -V          display version information and exit
  -b LIMIT    set the branch LIMIT
  -c SIZE     set the stack chunk SIZE
  -C          compile the program but do not run it
  -e STRING   execute the literal STRING
  -f FILE     run the JavaScript program FILE
  -i          enable interactive mode
  -s          enable strict warning mode
  -S SIZE     set the maximum stack SIZE
  -v VERSION  set the JavaScript version (defaults to 170 for JavaScript 1.7)
  -w          enable warning messages
  -W          disable warning messages
  -x          enable E4X XML mode

Report bugs via the web at <http://groups.google.com/group/couchdb>.

Report bugs via email to <couchdb@googlegroups.com>.
EOF
}

display_error () {
    # Display a short instruction referring users to further documentation.

    basename=$1
    echo "Try \`"$basename" -h' for more information."
}

run_couchjs () {
    # Run the JavaScript interpreter shell with options.

    /usr/lib/couchdb/bin/_couchjs $@
}

parse_script_option_list () {
    # Parse the script option list and take the appropriate action.

    basename=$(basename $0)
    if ! argument_list=$(getopt hVbcCefisSvwWx $@); then
        echo
        display_error $basename
        exit $SCRIPT_ERROR
    fi
    eval set -- "$argument_list"
    while [ $# -gt 0 ]; do
        case "$1" in
            -h)
                shift
                display_help $basename
                exit $SCRIPT_OK
                ;;
            -V)
                shift
                display_version
                exit $SCRIPT_OK
                ;;
            -v)
                shift
                DEFAULT_VERSION=$1
                shift
                ;;
            --)
                shift
                break
                ;;
            *)
                break
                ;;
        esac
    done
    option_list=$(echo $@ | sed 's/--//')
    run_couchjs -v $DEFAULT_VERSION $option_list
}

parse_script_option_list $@
