1#!/bin/sh -e
2
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#   http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15### BEGIN INIT INFO
16# Provides:          couchdb
17# Required-Start:    $local_fs $remote_fs
18# Required-Stop:     $local_fs $remote_fs
19# Default-Start:     2 3 4 5
20# Default-Stop:      0 1 6
21# Short-Description: Apache CouchDB init script
22# Description:       Apache CouchDB init script for the database server.
23### END INIT INFO
24
25SCRIPT_OK=0
26SCRIPT_ERROR=1
27
28DESCRIPTION="database server"
29NAME=couchdb
30SCRIPT_NAME=`basename $0`
31COUCHDB=%bindir%/%couchdb_command_name%
32CONFIGURATION_FILE=%sysconfdir%/default/couchdb
33RUN_DIR=%localstaterundir%
34LSB_LIBRARY=/lib/lsb/init-functions
35
36if test ! -x $COUCHDB; then
37    exit $SCRIPT_ERROR
38fi
39
40if test -r $CONFIGURATION_FILE; then
41    . $CONFIGURATION_FILE
42fi
43
44log_daemon_msg () {
45    # Dummy function to be replaced by LSB library.
46
47    echo $@
48}
49
50log_end_msg () {
51    # Dummy function to be replaced by LSB library.
52
53    if test "$1" != "0"; then
54      echo "Error with $DESCRIPTION: $NAME"
55    fi
56    return $1
57}
58
59if test -r $LSB_LIBRARY; then
60    . $LSB_LIBRARY
61fi
62
63start_couchdb () {
64    # Start Apache CouchDB as a background process.
65
66    command="$COUCHDB -b"
67    if test -n "$COUCHDB_STDOUT_FILE"; then
68        command="$command -o $COUCHDB_STDOUT_FILE"
69    fi
70    if test -n "$COUCHDB_STDERR_FILE"; then
71        command="$command -e $COUCHDB_STDERR_FILE"
72    fi
73    if test -n "$COUCHDB_RESPAWN_TIMEOUT"; then
74        command="$command -r $COUCHDB_RESPAWN_TIMEOUT"
75    fi
76    if test -n "$COUCHDB_OPTIONS"; then
77        command="$command $COUCHDB_OPTIONS"
78    fi
79    mkdir -p "$RUN_DIR"
80    if test -n "$COUCHDB_USER"; then
81        chown $COUCHDB_USER "$RUN_DIR"
82        if su $COUCHDB_USER -c "$command" > /dev/null; then
83            return $SCRIPT_OK
84        else
85            return $SCRIPT_ERROR
86        fi
87    else
88        if $command > /dev/null; then
89            return $SCRIPT_OK
90        else
91            return $SCRIPT_ERROR
92        fi
93    fi
94}
95
96stop_couchdb () {
97    # Stop the running Apache CouchDB process.
98
99    command="$COUCHDB -d"
100    if test -n "$COUCHDB_OPTIONS"; then
101        command="$command $COUCHDB_OPTIONS"
102    fi
103    if test -n "$COUCHDB_USER"; then
104        if su $COUCHDB_USER -c "$command" > /dev/null; then
105            return $SCRIPT_OK
106        else
107            return $SCRIPT_ERROR
108        fi
109    else
110        if $command > /dev/null; then
111            return $SCRIPT_OK
112        else
113            return $SCRIPT_ERROR
114        fi
115    fi
116}
117
118display_status () {
119    # Display the status of the running Apache CouchDB process.
120
121    $COUCHDB -s
122}
123
124parse_script_option_list () {
125    # Parse arguments passed to the script and take appropriate action.
126
127    case "$1" in
128        start)
129            log_daemon_msg "Starting $DESCRIPTION" $NAME
130            if start_couchdb; then
131                log_end_msg $SCRIPT_OK
132            else
133                log_end_msg $SCRIPT_ERROR
134            fi
135            ;;
136        stop)
137            log_daemon_msg "Stopping $DESCRIPTION" $NAME
138            if stop_couchdb; then
139                log_end_msg $SCRIPT_OK
140            else
141                log_end_msg $SCRIPT_ERROR
142            fi
143            ;;
144        restart)
145            log_daemon_msg "Restarting $DESCRIPTION" $NAME
146            if stop_couchdb; then
147                if start_couchdb; then
148                    log_end_msg $SCRIPT_OK
149                else
150                    log_end_msg $SCRIPT_ERROR
151                fi
152            else
153                log_end_msg $SCRIPT_ERROR
154            fi
155            ;;
156        status)
157            display_status
158            ;;
159        *)
160            cat << EOF >&2
161Usage: $SCRIPT_NAME {start|stop|restart|status}
162EOF
163            exit $SCRIPT_ERROR
164            ;;
165    esac
166}
167
168parse_script_option_list $@
169