1544a38ddSChristopher Lenz#! /bin/sh -e
2544a38ddSChristopher Lenz
3544a38ddSChristopher Lenz# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4350811a3SNoah Slater# use this file except in compliance with the License. You may obtain a copy of
5544a38ddSChristopher Lenz# the License at
6544a38ddSChristopher Lenz#
7544a38ddSChristopher Lenz#   http://www.apache.org/licenses/LICENSE-2.0
8544a38ddSChristopher Lenz#
9544a38ddSChristopher Lenz# Unless required by applicable law or agreed to in writing, software
10544a38ddSChristopher Lenz# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11350811a3SNoah Slater# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12544a38ddSChristopher Lenz# License for the specific language governing permissions and limitations under
13544a38ddSChristopher Lenz# the License.
14544a38ddSChristopher Lenz
15544a38ddSChristopher LenzSCRIPT_OK=0
16544a38ddSChristopher LenzSCRIPT_ERROR=1
17544a38ddSChristopher Lenz
18544a38ddSChristopher LenzDEFAULT_VERSION=170
19544a38ddSChristopher Lenz
20c6a29006STrond Norbyebasename=`basename $0`
21544a38ddSChristopher Lenz
22544a38ddSChristopher Lenzdisplay_version () {
23544a38ddSChristopher Lenz    cat << EOF
24c6a29006STrond Norbye$basename - Apache CouchDB ${COUCHDB_VERSION}
25544a38ddSChristopher Lenz
26544a38ddSChristopher LenzLicensed under the Apache License, Version 2.0 (the "License"); you may not use
27350811a3SNoah Slaterthis file except in compliance with the License. You may obtain a copy of the
28544a38ddSChristopher LenzLicense at
29544a38ddSChristopher Lenz
30544a38ddSChristopher Lenz  http://www.apache.org/licenses/LICENSE-2.0
31544a38ddSChristopher Lenz
32544a38ddSChristopher LenzUnless required by applicable law or agreed to in writing, software distributed
33544a38ddSChristopher Lenzunder the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
34350811a3SNoah SlaterCONDITIONS OF ANY KIND, either express or implied. See the License for the
35544a38ddSChristopher Lenzspecific language governing permissions and limitations under the License.
36544a38ddSChristopher LenzEOF
37544a38ddSChristopher Lenz}
38544a38ddSChristopher Lenz
39544a38ddSChristopher Lenzdisplay_help () {
40544a38ddSChristopher Lenz    cat << EOF
41544a38ddSChristopher LenzUsage: $basename [FILE]
42544a38ddSChristopher Lenz
43c6a29006STrond NorbyeThe $basename command runs the Apache CouchDB JavaScript interpreter.
44544a38ddSChristopher Lenz
45544a38ddSChristopher LenzThe exit status is 0 for success or 1 for failure.
46544a38ddSChristopher Lenz
47544a38ddSChristopher LenzOptions:
48544a38ddSChristopher Lenz
49544a38ddSChristopher Lenz  -h  display a short help message and exit
50544a38ddSChristopher Lenz  -V  display version information and exit
51df7ac136SPaul Joseph Davis  -H  install couchjs cURL bindings (only avaiable
52df7ac136SPaul Joseph Davis      if CouchDB was built with cURL available)
53544a38ddSChristopher Lenz
54c6a29006STrond NorbyeReport bugs at <https://www.couchbase.com/issues>.
55544a38ddSChristopher LenzEOF
56544a38ddSChristopher Lenz}
57544a38ddSChristopher Lenz
58544a38ddSChristopher Lenzdisplay_error () {
59544a38ddSChristopher Lenz    if test -n "$1"; then
60544a38ddSChristopher Lenz        echo $1 >&2
61544a38ddSChristopher Lenz    fi
62544a38ddSChristopher Lenz    echo >&2
63544a38ddSChristopher Lenz    echo "Try \`"$basename" -h' for more information." >&2
64544a38ddSChristopher Lenz    exit $SCRIPT_ERROR
65544a38ddSChristopher Lenz}
66544a38ddSChristopher Lenz
67544a38ddSChristopher Lenzrun_couchjs () {
68c6a29006STrond Norbye    exec "${CMAKE_INSTALL_PREFIX}/lib/couchdb/bin/couchjs" "$@"
69544a38ddSChristopher Lenz}
70544a38ddSChristopher Lenz
71544a38ddSChristopher Lenzparse_script_option_list () {
7293bd2f4bSNoah Slater    set +e
73df7ac136SPaul Joseph Davis    options=`getopt hVH $@`
7493bd2f4bSNoah Slater    if test ! $? -eq 0; then
75544a38ddSChristopher Lenz        display_error
76544a38ddSChristopher Lenz    fi
7793bd2f4bSNoah Slater    set -e
780e7a43f5SNoah Slater    eval set -- $options
79544a38ddSChristopher Lenz    while [ $# -gt 0 ]; do
80544a38ddSChristopher Lenz        case "$1" in
81544a38ddSChristopher Lenz            -h) shift; display_help; exit $SCRIPT_OK;;
82544a38ddSChristopher Lenz            -V) shift; display_version; exit $SCRIPT_OK;;
83544a38ddSChristopher Lenz            --) shift; break;;
84544a38ddSChristopher Lenz            *) break;;
85544a38ddSChristopher Lenz        esac
86544a38ddSChristopher Lenz    done
8798d7e8f0SDustin Sallings    script_name=`echo "$@" | sed -e 's/.*--[[:blank:]]*//'`
88df7ac136SPaul Joseph Davis    if test -z "$script_name"; then
89544a38ddSChristopher Lenz        display_error "You must specify a FILE."
90544a38ddSChristopher Lenz    fi
9198d7e8f0SDustin Sallings    options=`echo "$@" | sed -e 's/--//'`
9298d7e8f0SDustin Sallings    run_couchjs "$options"
93544a38ddSChristopher Lenz}
94544a38ddSChristopher Lenz
95544a38ddSChristopher Lenzparse_script_option_list $@
96