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


## @description  check a file
## @audience     private
## @stability    evolving
## @replaceable  no
## @param        filename
## @param        jarfile
## @return       0 = destroy verify dir
## @return       1 = keep verify dir
function process_file
{
  declare check=$1
  declare fqfn=$2
  declare fn
  declare keepdir
  declare tf
  declare count
  declare valid

  fn=$(basename "${fqfn}")
  keepdir=false
  valid=0
  count=0

  unzip -o -d "${WORK_DIR}/${fn}" "${fqfn}" '*'"${check}"'*' >/dev/null 2>&1

  while read -r tf; do
    ((count = count + 1))
    if diff -q "${DIST_DIR}/${check}.txt" "${tf}" >/dev/null 2>&1; then
      ((valid = valid + 1))
    fi
  done < <(find "${WORK_DIR}/${fn}" -name "${check}"'*')

  if [[ "${count}" -eq 0 ]]; then
    hadoop_error "ERROR: ${fn}: Missing a ${check} file"
  elif [[ "${count}" -gt 1 ]]; then
    hadoop_error "WARNING: ${fn}: Found ${count} ${check} files (${valid} were valid)"
    keepdir=true
  fi

  if [[ "${valid}" -eq 0 ]] && [[ "${count}" -gt 0 ]]; then
      hadoop_error "ERROR: ${fn}: No valid ${check} found"
      keepdir=true
  fi

  if [[ "${keepdir}" = "false" ]]; then
    return 0
  else
    return 1
  fi
}


## @description  check a jar
## @audience     private
## @stability    evolving
## @replaceable  no
## @param        jarfile
## @return       0 - success
## @return       1 - errors
function process_jar
{
  declare fqfn=$1
  declare fn
  declare keepwork

  fn=$(basename "${fqfn}")
  keepwork=false

  if [[ ! ${fn} =~ hadoop-.*-${PROJ_VERSION} ]]; then
    return
  fi

  mkdir -p "${WORK_DIR}/${fn}"

  if ! process_file LICENSE "${fqfn}"; then
    keepwork=true
  fi

  if ! process_file NOTICE "${fqfn}"; then
    keepwork=true
  fi

  if [[ "${keepwork}" = "false" ]]; then
    rm -rf "${WORK_DIR:?}/${fn}"
    return 0
  else
    hadoop_error ""
    return 1
  fi
}


MYNAME="${BASH_SOURCE-$0}"
#shellcheck disable=SC2034
HADOOP_SHELL_EXECNAME="${MYNAME##*/}"
BINDIR=$(cd -P -- "$(dirname -- "${MYNAME}")" >/dev/null && pwd -P)
#shellcheck disable=SC2034
HADOOP_LIBEXEC_DIR="${BINDIR}/../../hadoop-common-project/hadoop-common/src/main/bin"

#shellcheck disable=SC1090
. "${HADOOP_LIBEXEC_DIR}/hadoop-functions.sh"

HADOOP_LIBEXEC_DIR=$(hadoop_abs "${HADOOP_LIBEXEC_DIR}")
BINDIR=$(hadoop_abs "${BINDIR}")
BASEDIR=$(hadoop_abs "${BINDIR}/../..")

pushd "${BASEDIR}" >/dev/null
#shellcheck disable=SC2016
PROJ_VERSION=$(mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec)
popd >/dev/null

DIST_DIR="${BASEDIR}/hadoop-dist/target/hadoop-${PROJ_VERSION}"
WORK_DIR="${BASEDIR}/patchprocess/verify"

rm -rf "${WORK_DIR:?}"
mkdir -p "${WORK_DIR}"

while read -r filename; do
  process_jar "${filename}"
  ((ret = ret + $? ))
done < <(find "${DIST_DIR}" \
  -name "hadoop-*${PROJ_VERSION}*.jar" -or \
  -name "hadoop-*${PROJ_VERSION}*.war")
if [[ ${ret} -gt 0 ]]; then
  exit 1
fi
exit 0
