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


# Dlf command script
#
# Environment Variables
#
#   JAVA_HOME        The java implementation to use.  Overrides JAVA_HOME.
#
#   DLF_CLASSPATH    Extra Java CLASSPATH entries.
#
#
#   DLF_HEAPSIZE     The maximum amount of heap to use, in MB.
#                    Default is 1000.
#
#   DLF_OPTS         Extra Java runtime options.
#
#   DLF_CONF_DIR     Alternate conf dir. Default is ${DLF_HOME}/conf.
#

#old_path=`pwd`
#echo $old_path
export DLF_HOME=`dirname "$0"`
export DLF_HOME=`cd $DLF_HOME/..; pwd`
export DLF_CONF_DIR=$DLF_HOME/conf
#cd $old_path

VERSION="DLF Cli 1.0.0"

# if no args specified, show usage
if [ $# = 0  ]; then
    echo $VERSION
    echo "Copyright 2025 DLF, All rights reserved."
    echo ""
    echo "Usage: dlf <COMMAND> <SUB-COMMAND> [<SUB-COMMAND-ARGS>]"
    exit 0
fi

 
 function printHelp()
 {
    echo "Usage: dlf table list [<SUB-COMMAND-ARGS>]"

    echo "Example: "
    echo "     ../dlf-cli/bin/dlf table help"
    echo ""
 }


function killJava()
{
    local child=$1
    echo "Try to kill process DLF Cli, PID:$child ."
    killTime=1
    while [ true ]; do
        sleep 1
        output=`ps -p $child | grep -v PID`
        echo "Try to kill process DLF Cli, Times:$killTime PID:$child ."
        let killTime=${killTime}+1
        if [ -z "$output" ]; then
            echo "Kill completed ."
            break
        fi
        kill -15 $child
    done
}

# get arguments
COMMAND=$1

# some Java parameters
if [ "$JAVA_HOME" != "" ]; then
  #echo "run java in $JAVA_HOME"
  JAVA_HOME=$JAVA_HOME
fi
  
if [ "$JAVA_HOME" = "" ]; then
  echo "Error: JAVA_HOME not setting！"
  exit 1
fi

JAVA=$JAVA_HOME/bin/java
JAVA_HEAP_MAX=-Xmx1024m 

if [ "$DLF_HOME" = "" ]; then
  echo "Error: DLF_HOME not setting！"
  exit 1
fi


# check envvars which might override default args
if [ "$DLF_HEAPSIZE" != "" ]; then
  #echo "run with heapsize $DLF_HEAPSIZE"
  JAVA_HEAP_MAX="-Xmx""$DLF_HEAPSIZE""m"
  #echo $JAVA_HEAP_MAX
fi

# CLASSPATH initially contains $DLF_CONF_DIR
CLASSPATH="${DLF_CONF_DIR}"
CLASSPATH=${CLASSPATH}:$JAVA_HOME/lib/tools.jar

# so that filenames w/ spaces are handled correctly in loops below
IFS=

for f in $DLF_HOME/bennett-cli-*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done

# add libs to CLASSPATH
for f in $DLF_HOME/lib/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done

# restore ordinary behaviour
unset IFS

# figure out which class to run
if [ $# = 0 ] || [ $COMMAND = "help" ] || [ $COMMAND = "-help" ] || [ $COMMAND = "-h" ] ;then
  printHelp
  exit 0
fi
CLASS='com.aliyun.bennett.cli.DlfCli'

$JAVA -Dfile.encoding=UTF-8 $JAVA_HEAP_MAX $DLF_OPT -classpath "$CLASSPATH" $CLASS "$@" &

#set up signal
pid=$!
trap "killJava $pid" 1 2 3 15
#1) SIGHUP 2) SIGINT 3) SIGQUIT 15) SIGTERM
wait $pid
