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

# project.build.directory
BASEDIR=$1

#hdds.version
HDDS_VERSION=$2

## @audience     private
## @stability    evolving
function run()
{
  declare res

  echo "\$ ${*}"
  "${@}"
  res=$?
  if [[ ${res} != 0 ]]; then
    echo
    echo "Failed!"
    echo
    exit "${res}"
  fi
}

## @audience     private
## @stability    evolving
function findfileindir()
{
  declare file="$1"
  declare dir="${2:-./share}"
  declare count

  count=$(find "${dir}" -iname "${file}" | wc -l)

  #shellcheck disable=SC2086
  echo ${count}
}


# shellcheck disable=SC2164
ROOT=$(cd "${BASEDIR}"/../../..;pwd)
echo
echo "Current directory $(pwd)"
echo

run rm -rf "ozone-${HDDS_VERSION}"
run mkdir "ozone-${HDDS_VERSION}"
run cd "ozone-${HDDS_VERSION}"

run cp -p "${ROOT}/hadoop-ozone/dist/src/main/license/bin/NOTICE.txt" "NOTICE.txt"
run cp -p "${ROOT}/hadoop-ozone/dist/src/main/license/bin/LICENSE.txt" "LICENSE.txt"
run cp -pr "${ROOT}/hadoop-ozone/dist/src/main/license/bin/licenses" "licenses"
run cp -p "${ROOT}/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/LICENSE" "licenses/LICENSE-ozone-recon.txt"
run cp -p "${ROOT}/README.md" .
run cp -p "${ROOT}/HISTORY.md" .
run cp -p "${ROOT}/SECURITY.md" .
run cp -p "${ROOT}/CONTRIBUTING.md" .

run mkdir -p ./share/ozone/classpath
run mkdir -p ./share/ozone/lib
run mkdir -p ./share/ozone/web
run mkdir -p ./bin
run mkdir -p ./sbin
run mkdir -p ./etc
run mkdir -p ./libexec
run mkdir -p ./log
run mkdir -p ./temp
run mkdir -p ./tests

run cp -r "${ROOT}/hadoop-hdds/common/src/main/conf/" "etc/hadoop"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/conf/om-audit-log4j2.properties" "etc/hadoop"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/conf/dn-audit-log4j2.properties" "etc/hadoop"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/conf/dn-container-log4j2.properties" "etc/hadoop"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/conf/scm-audit-log4j2.properties" "etc/hadoop"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/conf/s3g-audit-log4j2.properties" "etc/hadoop"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/conf/ozone-site.xml" "etc/hadoop"
run cp -f "${ROOT}/hadoop-ozone/dist/src/shell/conf/log4j.properties" "etc/hadoop"
run cp "${ROOT}/hadoop-hdds/common/src/main/resources/network-topology-default.xml" "etc/hadoop"
run cp "${ROOT}/hadoop-hdds/common/src/main/resources/network-topology-nodegroup.xml" "etc/hadoop"

run cp -r "${ROOT}/hadoop-ozone/dist/src/main/dockerlibexec/." "libexec/"

run cp "${ROOT}/hadoop-ozone/dist/src/shell/ozone/ozone" "bin/"


run cp "${ROOT}/hadoop-ozone/dist/src/shell/ozone/ozone-config.sh" "libexec/"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/ozone/ozone-functions.sh" "libexec/"
run cp -r "${ROOT}/hadoop-ozone/dist/src/shell/shellprofile.d" "libexec/"
run cp -r "${ROOT}/hadoop-ozone/dist/src/shell/upgrade" "libexec/"


run cp "${ROOT}/hadoop-ozone/dist/src/shell/hdds/hadoop-daemons.sh" "sbin/"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/hdds/workers.sh" "sbin/"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/ozone/start-ozone.sh" "sbin/"
run cp "${ROOT}/hadoop-ozone/dist/src/shell/ozone/stop-ozone.sh" "sbin/"

# fault injection tests
run cp  -r "${ROOT}/hadoop-ozone/fault-injection-test/network-tests/src/test/blockade" tests

# Optional documentation, could be missing
cp -r "${ROOT}/hadoop-hdds/docs/target/classes/docs" ./

# BOM is created only when using 'dist' profile
cp -p "${ROOT}"/target/bom.* ./

#copy byteman helpers
run cp -r "${ROOT}/dev-support/byteman" "share/ozone/"

#Copy docker compose files
#compose files are preprocessed: properties (eg. project.version) are replaced first by maven.
run cp -p -R "${ROOT}/hadoop-ozone/dist/target/compose" .
run cp -p -r "${ROOT}/hadoop-ozone/dist/target/smoketest" .
run cp -p -r "${ROOT}/hadoop-ozone/dist/target/k8s" kubernetes
run cp -p -r "${ROOT}/hadoop-ozone/dist/target/Dockerfile" .

run mkdir compose/_keytabs

for file in $(find "${ROOT}" -path '*/target/classes/*.classpath' | sort); do
  # We need to add the artifact manually as it's not part the generated classpath desciptor
  module=$(basename "${file%.classpath}")
  sed -i -e "s;$;:\$HDDS_LIB_JARS_DIR/${module}-${HDDS_VERSION}.jar;" "$file"

  cp -n -p -v "$file" share/ozone/classpath/
done

for file in $(find "${ROOT}" -path '*/share/ozone/lib/*jar' | sort); do
  cp -n -p -v "$file" share/ozone/lib/
done

#workaround for https://issues.apache.org/jira/browse/MRESOURCES-236
find ./compose -name "*.sh" -exec chmod 755 {} \;
find ./kubernetes -name "*.sh" -exec chmod 755 {} \;
