blob: 7fd6c89ecd627242e314d1aa135a0f61dd6976c5 [file]
#!/bin/bash
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Version: 1.2-a2
#
set -o nounset
umask 077
#
# Settings
#
JACK_VERSION=${JACK_VERSION:=2.19.RELEASE}
JACK_HOME="${JACK_HOME:=$HOME/.jack-home}"
CLIENT_SETTING="${CLIENT_SETTING:=$HOME/.jack-client}"
TMPDIR=${TMPDIR:=/tmp}
CLIENT_TMP_DIR=$TMPDIR/jack-$USER
#
# Load client settings
#
if [ -f "$CLIENT_SETTING" ]; then
source "$CLIENT_SETTING"
fi
#
# Create or update client settings if needed
#
if [[ ! -f "$CLIENT_SETTING" || $SETTING_VERSION -lt 4 ]]; then
echo "Writing client settings in" $CLIENT_SETTING
cat >"$CLIENT_SETTING.$$" <<-EOT
# Server settings
SERVER_HOST=${SERVER_HOST:=127.0.0.1}
SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8074}
SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8075}
# Internal, do not touch
SETTING_VERSION=4
EOT
ln -f "$CLIENT_SETTING.$$" "$CLIENT_SETTING"
rm "$CLIENT_SETTING.$$"
source "$CLIENT_SETTING"
fi
#
# Prepare compilation
#
JACK_PWD="$PWD"
JACK_DIR="$CLIENT_TMP_DIR/jack-task-$$/"
JACK_EXIT="$JACK_DIR/exit"
# Cleanup
trap 'rm -f "$JACK_EXIT" 2>/dev/null; rmdir "$JACK_DIR";' EXIT
mkdir "$CLIENT_TMP_DIR" 2>/dev/null || (exit 0)
set -o errexit
mkdir "$JACK_DIR"
abort () { exit 255; }
#
# Launch the compilation
#
set +o errexit
trap ERR
# put arguments in a non array variable
ARGS=""
for i in "$@"; do
ARGS="$ARGS $i"
done
RETRY_LAUNCH=1
RETRY_SESSION=3
# Launch compilation
while true; do
exec 3>&1
exec 4>&2
HTTP_CODE=$(curl -f \
--output >(tee >(sed -n -e 's/^E|\(.*\)$/\1/p' >&4 ) | tee >(sed -n -e 's/^X|\(.*\)$/\1/p' >$JACK_EXIT) | sed -n -e 's/^O|\(.*\)$/\1/p' >&3) \
--no-buffer --write-out '%{http_code}\n' --silent --connect-timeout 10 \
-X POST \
-H "Accept: application/vnd.jack.command-out;version=1" \
-F "cli=$ARGS;type=plain/text" \
-F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \
-F "pwd=$JACK_PWD;type=plain/text" \
--no-proxy ${SERVER_HOST}:$SERVER_PORT_SERVICE \
http://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \
)
CURL_CODE=$?
exec 3>&-
exec 4>&-
JACK_CODE=$(cat "$JACK_EXIT")
if [ $CURL_CODE -eq 0 ]; then
# No problem, let's go
break;
elif [ $CURL_CODE -eq 7 ]; then
# Failed to connect
echo "ERROR: Cannot connect to Jack server" >&2
abort
elif [ $CURL_CODE -eq 22 ]; then
# Http code not OK, let's decode and abort
if [ $HTTP_CODE -eq 401 ]; then
# 401: Unauthorized
echo "ERROR: Security problem, see Jack server log" >&2
abort
elif [ $HTTP_CODE -eq 400 ]; then
# 400: Bad request
echo "Bad request, see server log" >&2
abort
else
# Other
echo "ERROR: Internal unknown error ($HTTP_CODE), try other ports or see Jack server log" >&2
abort
fi
else
# In case of partial, timeout, empty respond, network error, let's retry
if [ $RETRY_SESSION -eq 0 ]; then
echo "ERROR: Communication error with Jack server ($CURL_CODE)" >&2
abort
else
let RETRY_SESSION=RETRY_SESSION-1
fi
fi
done
# Exit
exit $JACK_CODE