#! /usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more contributor license
# agreements.  See the NOTICE file distributed with this work for additional information regarding
# copyright ownership.  The ASF licenses this file to you 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.

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
   bin="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
   SOURCE="$(readlink "$SOURCE")"
   [[ $SOURCE != /* ]] && SOURCE="$bin/$SOURCE"
done
# Set up variables needed by fluo-env.sh
export bin="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
export basedir="$( cd -P ${bin}/.. && pwd )"
export conf="$basedir/conf"
export lib="$basedir/lib"
export cmd="$1"
export FLUO_HOME="$basedir"
FLUO_VERSION=2.0.0

if [ -f "$conf/fluo-env.sh" ]; then
  . "$conf"/fluo-env.sh
fi

export CLASSPATH

# stop if any command fails
set -e

APP=$2
APP_DIR=$FLUO_HOME/apps/$APP
APP_LIB_DIR=$APP_DIR/lib
APP_CONF_DIR=$APP_DIR/conf
APP_LOG_DIR=$APP_DIR/logs

function print_usage {
  echo -e "Usage: local-fluo <command> (<argument> ...)\n"
  echo -e "This script is now deprecated.  Use 'fluo' script to start local processes\n"
  echo -e "Possible commands:\n"
  echo "  start-oracle <app>    Starts local Fluo oracle for application"
  echo "  start-worker <app>    Starts local Fluo worker for application"
  echo "  kill-oracle <app>     Kill Fluo oracle on local machine"
  echo "  kill-worker <app>     Kill Fluo worker on local machine"
  echo " "
  exit 1
}

function validate_app_full {
  if [ -z "$APP" ]; then
    echo -e "The application name (set by <app>) cannot be an empty string!\n"
    print_usage
  fi
  if [[ $APP = *"-h"* ]]; then
    print_usage
  fi
  java org.apache.fluo.cluster.util.ValidateAppName $APP
}

function check_dirs {
  if [[ ! -d $APP_DIR || ! -d $APP_CONF_DIR || ! -d $APP_LIB_DIR  ]]; then
    echo "ERROR - The Fluo '$APP' application needs to be configured in apps/ with a conf/ and lib/ directory.  Use 'fluo new $APP' to create this configuration"
    exit 1
  fi
  mkdir -p $APP_LOG_DIR 2>/dev/null
}

function kill_if_running {
  validate_app_full
  PID=`jps -m | grep $1 | grep $APP | cut -f 1 -d ' '`
  if [ -z "$PID" ]; then
    echo "ERROR - A $1 process for the Fluo application '$APP' is not running"
  else
    kill $PID
    echo "Killed a $1 process for the Fluo '$APP' application"
  fi
}

LOGHOST=$(hostname)
LOCAL_OPTS="$APP_CONF_DIR/fluo.properties"

case "$1" in
start-oracle)
  validate_app_full
  check_dirs
  SERVICE="oracle"
  java -Dfluo.app=$APP org.apache.fluo.core.oracle.FluoOracleImpl $LOCAL_OPTS >${APP_LOG_DIR}/${SERVICE}_${LOGHOST}.out 2>${APP_LOG_DIR}/${SERVICE}_${LOGHOST}.err &
  echo "Started oracle for Fluo '$APP' application.  View its logs at $APP_LOG_DIR"
  ;;
kill-oracle)
  kill_if_running FluoOracleImpl
  ;;
start-worker)
  validate_app_full
  check_dirs
  SERVICE="worker"
  export CLASSPATH="$APP_LIB_DIR/*:$CLASSPATH"
  java -Dfluo.app=$APP org.apache.fluo.core.worker.FluoWorkerImpl $LOCAL_OPTS >${APP_LOG_DIR}/${SERVICE}_${LOGHOST}.out 2>${APP_LOG_DIR}/${SERVICE}_${LOGHOST}.err &
  echo "Started worker for Fluo '$APP' application.  View its logs at $APP_LOG_DIR"
  ;;
kill-worker)
  kill_if_running FluoWorkerImpl
  ;;
*)
  print_usage
esac
