de54fc2c3e4a85b93701aeae0780155e94739ce8
[odlparent.git] / karaf / opendaylight-karaf-resources / src / main / resources / bin / stop
1 #!/bin/sh
2 #
3 #    Licensed to the Apache Software Foundation (ASF) under one or more
4 #    contributor license agreements.  See the NOTICE file distributed with
5 #    this work for additional information regarding copyright ownership.
6 #    The ASF licenses this file to You under the Apache License, Version 2.0
7 #    (the "License"); you may not use this file except in compliance with
8 #    the License.  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #    Unless required by applicable law or agreed to in writing, software
13 #    distributed under the License is distributed on an "AS IS" BASIS,
14 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #    See the License for the specific language governing permissions and
16 #    limitations under the License.
17 #
18
19 realpath() {
20   OURPWD="$PWD"
21   cd "$(dirname "$1")"
22   LINK=$(readlink "$(basename "$1")")
23   while [ "$LINK" ]; do
24     cd "$(dirname "$LINK")"
25     LINK=$(readlink "$(basename "$1")")
26   done
27   REALPATH="$PWD/$(basename "$1")"
28   cd "$OURPWD"
29   echo "$REALPATH"
30 }
31
32 REALNAME=$(realpath "$0")
33 DIRNAME=$(dirname "$REALNAME")
34 PROGNAME=$(basename "$REALNAME")
35
36 #
37 # Sourcing environment settings for karaf similar to tomcats setenv 
38 #
39 KARAF_SCRIPT="stop"
40 export KARAF_SCRIPT
41 if [ -f "$DIRNAME/setenv" ]; then
42   . "$DIRNAME/setenv"
43 fi
44
45 warn() {
46     echo "${PROGNAME}: $*"
47 }
48
49 die() {
50     warn "$*"
51     exit 1
52 }
53
54 detectOS() {
55     # OS specific support (must be 'true' or 'false').
56     cygwin=false;
57     darwin=false;
58     aix=false;
59     os400=false;
60     case "`uname`" in
61         CYGWIN*)
62             cygwin=true
63             ;;
64         Darwin*)
65             darwin=true
66             ;;
67         AIX*)
68             aix=true
69             ;;
70         OS400*)
71             os400=true
72             ;;
73     esac
74     # For AIX, set an environment variable
75     if $aix; then
76          export LDR_CNTRL=MAXDATA=0xB0000000@DSA
77          echo $LDR_CNTRL
78     fi
79 }
80
81 locateHome() {
82     if [ "x$KARAF_HOME" != "x" ]; then
83         warn "Ignoring predefined value for KARAF_HOME"
84     fi
85
86     # In POSIX shells, CDPATH may cause cd to write to stdout
87     (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
88
89     KARAF_HOME=`cd $DIRNAME/..; pwd`
90     if [ ! -d "$KARAF_HOME" ]; then
91         die "KARAF_HOME is not valid: $KARAF_HOME"
92     fi
93 }
94
95 locateBase() {
96     if [ "x$KARAF_BASE" != "x" ]; then
97         if [ ! -d "$KARAF_BASE" ]; then
98             die "KARAF_BASE is not valid: $KARAF_BASE"
99         fi
100     else
101         KARAF_BASE=$KARAF_HOME
102     fi
103 }
104
105 locateData() {
106     if [ "x$KARAF_DATA" != "x" ]; then
107         if [ ! -d "$KARAF_DATA" ]; then
108             die "KARAF_DATA is not valid: $KARAF_DATA"
109         fi
110     else
111         KARAF_DATA=$KARAF_BASE/data
112     fi
113 }
114
115 locateEtc() {
116     if [ "x$KARAF_ETC" != "x" ]; then
117         if [ ! -d "$KARAF_ETC" ]; then
118             die "KARAF_ETC is not valid: $KARAF_ETC"
119         fi
120     else
121         KARAF_ETC=$KARAF_BASE/etc
122     fi
123 }
124
125 init() {
126     # Determine if there is special OS handling we must perform
127     detectOS
128
129     # Locate the Karaf home directory
130     locateHome
131
132     # Locate the Karaf base directory
133     locateBase
134
135     # Locate the Karaf data directory
136     locateData
137
138     # Locat the Karaf etc directory
139     locateEtc
140 }
141
142 run() {
143     if $cygwin; then
144         KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
145         KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
146         KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
147         KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
148         if [ ! -z "$CLASSPATH" ]; then
149             CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
150         fi
151     fi
152     # Ensure the log directory exists -- we need to have a place to redirect stdout/stderr
153     if [ ! -d "$KARAF_DATA/log" ]; then
154         mkdir -p "$KARAF_DATA/log"
155     fi
156     exec "$KARAF_HOME"/bin/karaf stop "$@"
157 }
158
159 main() {
160     init
161     run "$@"
162 }
163
164 main "$@"
165