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

VERSION=${1:-3.0.0-SNAPSHOT}
TARGETDIR=${2:-/tmp/target}
TOOLSDIR=${3:-/tmp/tools}

function getfilename
{
  declare module=$1
  declare modtype=$2

  if [[ ${modtype} = builtin ]]; then
    echo "${TARGETDIR}/hadoop-${VERSION}/libexec/tools/${module}.sh"
  else
    echo "${TARGETDIR}/hadoop-${VERSION}/libexec/shellprofile.d/${module}.sh"
  fi
}

function header
{
  declare fn=$1

  cat >>"${fn}" <<-'TOKEN'
#!/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.
#
#
#
# IMPORTANT: This file is automatically generated by hadoop-dist at
#            -Pdist time.
#
#
TOKEN

}

function optional_prologue
{
  declare fn=$1
  declare module=$2

  if [[ -z "${OPTMODS}" ]]; then
    OPTMODS=${module}
  else
    OPTMODS="${OPTMODS},${module}"
  fi

  {
    echo "if hadoop_verify_entry HADOOP_TOOLS_OPTIONS \"${module}\"; then"
    echo "  hadoop_add_profile \"${module}\""
    echo "fi"
    echo ""
    echo "function _${module}_hadoop_classpath"
    echo "{"
  } >> "${fn}"
}

function builtin_prologue
{
  declare fn=$1
  declare module=$2

  {
    echo ""
    echo "function hadoop_classpath_tools_${module}"
    echo "{"
  } >> "${fn}"
}

function dependencywork
{
  declare fn=$1
  declare module=$2
  declare depfn=$3

  declare depline
  declare jarname

  while read -r depline; do
    jarname=$(echo "${depline}" | awk -F: '{print $2"-"$4".jar"}')

    if [[ -f "${TARGETDIR}/hadoop-${VERSION}/share/hadoop/tools/lib/${jarname}" ]]; then
      {
        echo "  if [[ -f \"\${HADOOP_TOOLS_HOME}/\${HADOOP_TOOLS_LIB_JARS_DIR}/${jarname}\" ]]; then"
        echo "    hadoop_add_classpath \"\${HADOOP_TOOLS_HOME}/\${HADOOP_TOOLS_LIB_JARS_DIR}/${jarname}\""
        echo "  fi"
      } >> "${fn}"

    elif [[ -f "${TARGETDIR}/hadoop-${VERSION}/share/hadoop/common/${jarname}"
         || -f "${TARGETDIR}/hadoop-${VERSION}/share/hadoop/common/lib/${jarname}" ]]; then
      true
    else
      echo "ERROR: ${module} has missing dependencies: ${jarname}"
    fi
  done < <(grep compile "${depfn}")

  {
    echo "  hadoop_add_classpath \"\${HADOOP_TOOLS_HOME}/\${HADOOP_TOOLS_LIB_JARS_DIR}/${module}-${VERSION}.jar\""
    echo "}"
    echo ""
  } >> "${fn}"
}

function document_optionals
{
  echo "Rewriting ${TARGETDIR}/hadoop-${VERSION}/etc/hadoop/hadoop-env.sh"
  sed -e "s^@@@HADOOP_OPTIONAL_TOOLS@@@^${OPTMODS}^" \
    "${TARGETDIR}/hadoop-${VERSION}/etc/hadoop/hadoop-env.sh" \
    > "${TARGETDIR}/hadoop-${VERSION}/etc/hadoop/hadoop-env.sh.new"
  mv "${TARGETDIR}/hadoop-${VERSION}/etc/hadoop/hadoop-env.sh.new" \
    "${TARGETDIR}/hadoop-${VERSION}/etc/hadoop/hadoop-env.sh"
}

function process
{
  declare fn
  declare basefn
  declare modtype
  declare module
  declare newfile
  declare newdir

  while read -r fn; do
    basefn=${fn##*/}
    module=$(echo "${basefn}" | cut -f1 -d.)
    modtype=$(echo "${basefn}" | cut -f2 -d.)
    modtype=${modtype##tools-}

    newfile=$(getfilename "${module}" "${modtype}")
    newdir=$(dirname "${newfile}")
    mkdir -p "${newdir}"

    if [[ -f "${newfile}" ]]; then
      rm "${newfile}"
    fi

    touch "${newfile}"

    header "${newfile}" "${module}"

    "${modtype}_prologue" "${newfile}" "${module}"

    dependencywork "${newfile}" "${module}" "${fn}"

    chmod a+rx "${newfile}"

  done < <(find "${TOOLSDIR}" -name '*.tools-builtin.txt' -o -name '*.tools-optional.txt')

  document_optionals
}

process
