#!/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.
#

# Logging functions
function log_info() {
  echo "[INFO] | $(date -u +"%D %T") UTC | $1"
}

function log_error() {
  echo "[ERROR] | $(date -u +"%D %T") UTC | $1"
}

BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"

if [ ! $# -eq 2 ]; then
  log_error "env file and dq file must be provided!"
  exit 1
fi

env_file=$1
if [ ! -f "${env_file}" ]; then
  log_error "Not found env file: ${env_file}"
  exit
fi
shift

dq_file=$1
if [ ! -f "${dq_file}" ]; then
  log_error "Not found dq file: ${dq_file}"
  exit
fi
shift

cd ${BASEDIR}

export JAVA_OPTS="-Xmx2048m -Xms256m -server -XX:+UseG1GC"

LIB_JARS=$(echo ${BASEDIR}/lib/*.jar | tr ' ' ',')
APP_CLASS="org.apache.griffin.measure.Application"
APP_JAR=$(ls ${BASEDIR}/lib/measure-*.jar)

exec $(which spark-submit) --class ${APP_CLASS} --jars ${LIB_JARS} ${APP_JAR} ${env_file} ${dq_file}
