Stop requiring bash in test-network.sh. Make it POSIX compliant
[tor.git] / src / test / test-network.sh
blob3a765d51f06db52cffdaf4031ebfb2a4a4abbfc8
1 #!/bin/sh
3 # This script calls the equivalent script in chutney/tools
5 # If we already know CHUTNEY_PATH, don't bother with argument parsing
6 TEST_NETWORK="$CHUTNEY_PATH/tools/test-network.sh"
7 # Call the chutney version of this script, if it exists, and we can find it
8 if [ -d "$CHUTNEY_PATH" ] && [ -x "$TEST_NETWORK" ]; then
9 # we can't produce any output, because we might be --quiet
10 # this preserves arguments with spaces correctly
11 exec "$TEST_NETWORK" "$@"
14 # We need to go looking for CHUTNEY_PATH
16 # Do we output anything at all?
17 ECHO="${ECHO:-echo}"
18 # Output is prefixed with the name of the script
19 myname=$(basename "$0")
21 # We need to find CHUTNEY_PATH, so that we can call the version of this script
22 # in chutney/tools with the same arguments. We also need to respect --quiet.
23 CHUTNEY_PATH=$(echo "$@" | awk -F '--chutney-path ' '{sub(" .*","",$2); print $2}')
24 TOR_DIR=$(echo "$@" | awk -F '--tor-dir ' '{sub(" .*","",$2); print $2}')
26 if echo "$@" | grep -e "--quiet" > /dev/null; then
27 ECHO=true
30 echo "CHUTNEY_PATH = $CHUTNEY_PATH"
31 echo "TOR_DIR = $TOR_DIR"
32 echo "ECHO = $ECHO"
34 # optional: $TOR_DIR is the tor build directory
35 # it's used to find the location of tor binaries
36 # if it's not set:
37 # - set it to $BUILDDIR, or
38 # - if $PWD looks like a tor build directory, set it to $PWD, or
39 # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
40 if [ ! -d "$TOR_DIR" ]; then
41 if [ -d "$BUILDDIR/src/core/or" ] && [ -d "$BUILDDIR/src/tools" ]; then
42 # Choose the build directory
43 # But only if it looks like one
44 $ECHO "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
45 TOR_DIR="$BUILDDIR"
46 elif [ -d "$PWD/src/core/or" ] && [ -d "$PWD/src/tools" ]; then
47 # Guess the tor directory is the current directory
48 # But only if it looks like one
49 $ECHO "$myname: \$TOR_DIR not set, trying \$PWD"
50 TOR_DIR="$PWD"
51 else
52 $ECHO "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
53 unset TOR_DIR
57 # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
58 # if it's not set:
59 # - if $PWD looks like a chutney directory, set it to $PWD, or
60 # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
61 # - fail and tell the user how to clone the chutney repository
62 if [ ! -d "$CHUTNEY_PATH" ] || [ ! -x "$CHUTNEY_PATH/chutney" ]; then
63 if [ -x "$PWD/chutney" ]; then
64 $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
65 CHUTNEY_PATH="$PWD"
66 elif [ -d "$TOR_DIR" ] && [ -d "$TOR_DIR/../chutney" ] && \
67 [ -x "$TOR_DIR/../chutney/chutney" ]; then
68 $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
69 CHUTNEY_PATH="$TOR_DIR/../chutney"
70 else
71 $ECHO "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
72 $ECHO "$myname: Get chutney: git clone https://git.torproject.org/\
73 chutney.git"
74 $ECHO "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
75 CHUTNEY_PATH=\`pwd\`/chutney"
76 unset CHUTNEY_PATH
77 exit 1
81 TEST_NETWORK="$CHUTNEY_PATH/tools/test-network.sh"
82 # Call the chutney version of this script, if it exists, and we can find it
83 if [ -d "$CHUTNEY_PATH" ] && [ -x "$TEST_NETWORK" ]; then
84 $ECHO "$myname: Calling newer chutney script $TEST_NETWORK"
85 # this may fail if some arguments have spaces in them
86 # if so, set CHUTNEY_PATH before calling test-network.sh, and spaces
87 # will be handled correctly
88 exec "$TEST_NETWORK" "$@"
89 else
90 $ECHO "$myname: Could not find tools/test-network.sh in CHUTNEY_PATH."
91 $ECHO "$myname: Please update your chutney using 'git pull'."
92 # We have failed to do what the user asked
93 exit 1