#!/bin/bash
#
# Copyright (C) 2016 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.3-a8
#
set -o nounset

#
# Settings
#
JACK_HOME="${JACK_HOME:=$HOME/.jack-server}"
CLIENT_SETTING="${CLIENT_SETTING:=$HOME/.jack-settings}"
TMPDIR=${TMPDIR:=/tmp}

LAUNCHER_JAR="$JACK_HOME/launcher.jar"
LAUNCHER_NAME=com.android.jack.launcher.ServerLauncher
CURRENT_CHARSET=$(locale charmap)
if [ -z "$CURRENT_CHARSET" ]; then
  CHARSET_ARGUMENT=
else
  CHARSET_ARGUMENT=";charset=$CURRENT_CHARSET"
fi

JACK_LOGS_DIR="$JACK_HOME"/logs
JACK_OUT_ERR="$JACK_LOGS_DIR"/outputs.txt
JACK_CONNECTION_TIMEOUT=300

#
# Load client settings
#
if [ -f "$CLIENT_SETTING" ]; then
  source "$CLIENT_SETTING"
fi


usage () {
  echo "Usage : $0"
}

checkCurl() {
  CURL_COMMAND=$(which curl)

  if [ $? -ne 0 ] ; then
    echo "curl not found in PATH"
    return 255
  fi

  if ! "$CURL_COMMAND" --version >/dev/null 2>&1 ; then
    echo "Failed to execute '$CURL_COMMAND'"
    return 255
  fi

  if "$CURL_COMMAND" --version | grep -q "SecureTransport" ; then
    echo "'$CURL_COMMAND' unsupported, please use a curl not based on SecureTransport"
  fi

  if "$CURL_COMMAND" --version | grep -q "OpenSSL/1.0.0" ; then
    echo "'$CURL_COMMAND' is using a too old SSL library, please update curl and its dependencies"
  elif curl --version | grep -q "OpenSSL/0." ; then
    echo "'$CURL_COMMAND' is using a too old SSL library, please update curl and its dependencies"
  fi
}


checkJava() {
  JAVA_COMMAND=$(which java)

  if [ $? -ne 0 ] ; then
    echo "java not found in PATH"
    return 255
  fi

  if ! "$JAVA_COMMAND" -version >/dev/null 2>&1 ; then
    echo "Failed to execute '$JAVA_COMMAND'"
    return 255
  fi


  JAVA_VERSION=$("$JAVA_COMMAND" -version 2>&1 | head -1 | grep --only-matching -e \"1\\.[0-9]* | cut -c 4-)
  if [ -z "$JAVA_VERSION" ] ; then
    echo "'$CURL_COMMAND': Failed to parse version, please ensure you're running a supported java"
    return 255
  fi

  if [ "$JAVA_VERSION" -lt 7 ] ; then
    echo "'$JAVA_COMMAND' is too old, please update to 1.7 or newer"
  fi
}


checkKeytool() {
  KEYTOOL_COMMAND=$(which keytool)

  if [ $? -ne 0 ] ; then
    echo "keytool not found in PATH"
    return 255
  fi

  if ! $KEYTOOL_COMMAND -help >/dev/null 2>&1 ; then
    echo "failed to execute '$KEYTOOL_COMMAND'"
    return 255
  fi


  if ! $KEYTOOL_COMMAND -help 2>&1 | grep -q -e "-genkeypair" ; then
    echo "'$KEYTOOL_COMMAND' unsupported, please ensure PATH allows finding keytool from a supported Java Runtime Environment ie 1.7 or newer"
  fi
}


#
# $1: port number
#
checkport() {
  PID_USING_PORT=$(lsof -F p -i TCP:$1 -sTCP:LISTEN | cut -c 2-)
  if [ -z "$PID_USING_PORT" ] ; then
    # port is free nothing to check
    return 0
  fi

  PS_OUT=$(ps -p $PID_USING_PORT -o  "pid uid args" | tail -1)
  if [ $? -ne 0 ] ; then
    # process exited before we could get any info, weird but means the port is now available
    return 0
  fi

  if ! echo $PS_OUT | grep -q $LAUNCHER_NAME  ; then
    echo "Port $1 is used by another process (pid=$(echo $PS_OUT | awk '{print $1}')), please ensure to free the port or change port configuration in '$CLIENT_SETTING' and '$JACK_HOME/config.properties'"
    return 255
  fi

  if [ "$(echo $PS_OUT | awk '{print $2}')" -ne "$(id -u)" ] ; then
    echo "Port $1 is used b a Jack server from another user uid=$(echo $PS_OUT | awk '{print $2}'), please change port configuration in '$CLIENT_SETTING' and '$JACK_HOME/config.properties'"
    return 255
  fi
}

checkCurl

checkJava

checkKeytool

checkport $SERVER_PORT_ADMIN
checkport $SERVER_PORT_SERVICE

# Exit

exit 0