#!/bin/bash
# SPDX-License-Identifier: EPL-1.0
##############################################################################
# Copyright (c) 2018 The Linux Foundation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
##############################################################################


#echo "Diff LDAP against INFO.yaml"

tmpdir(){

  DIR="/tmp/infofile/$purpose.$repo.$ldapgroup"
  mkdir -p "$DIR"

}

parseoptions() {

  if [[ -z $purpose ]]; then
    purpose=correct
  fi

  echo "    gerritclonebase $gerritclonebase"
  echo "    ldapgroup $ldapgroup"
  echo "    repo $repo"
  echo "    purpose $purpose"

  if [[ $purpose =~ "READY_FOR_INFO" ]]; then

    tmpdir "$@"

    if ! [[ -d "$DIR"/"$repo" ]]; then
      echo "    git clone -q $gerritclonebase$repo $DIR/$repo || exit 1"
      git clone -q "$gerritclonebase""$repo" "$DIR"/"$repo" || exit 1
    fi
    if [ ! -f "$DIR"/"$repo"/INFO.yaml ]; then
      cp INFO.template.yaml "$DIR"/"$repo"/INFO.yaml || exit 1
    else
      echo "INFO file already exists, refusing to overwrite"
      exit 1
    fi

  fi

  if [[ $purpose =~ "LINT" ]]; then

    tmpdir "$@"

    if ! [[ -d "$DIR"/"$repo" ]]; then
      echo "    git clone -q $gerritclonebase$repo $DIR/$repo || exit 1"
      git clone -q "$gerritclonebase""$repo" "$DIR"/"$repo" || exit 1
    fi
    if ! yamllint "$DIR"/"$repo"/INFO.yaml; then
      echo "ERROR LINT FAILED"
      exit 1
    fi

  fi

  #I should only clone the review if their is a discrepancy in committers
  if [[ $purpose =~ "IN-REVIEW" ]]; then

    tmpdir "$@"

    if ! [[ -d "$DIR"/"$repo" ]]; then
      echo "    git clone -q $gerritclonebase$repo $DIR/$repo || exit 1"
      SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
      git clone -q "$gerritclonebase""$repo" "$DIR"/"$repo" || exit 1
      cd "$DIR"/"$repo" || exit
      git fetch origin "$review" && git checkout --quiet FETCH_HEAD
      cd "$SCRIPTDIR" || exit
      yamllint "$DIR"/"$repo"/INFO.yaml
      if ! yamllint "$DIR"/"$repo"/INFO.yaml; then
        echo "   ERROR LINT FAILED CANNOT AUTO CORRECT FILE IN REVIEW"
        exit 1
      fi

    fi
  fi

  main "$@"
}

main() {

  if [[ -z $DIR ]]; then
    tmpdir "$@"
  fi
  echo "    tmpdir = $DIR"

  local ldap_committers
  if ! ldap_committers=$(LEGACY_CLI=1 lftools-uv ldap yaml4info "$ldapgroup"); then
    echo "    LDAP lookup failed" >&2
    exit 1
  fi
  echo "    LDAP lookup successful"
  # get-committers and sync-committers read an INFO file's committers key,
  # and yaml4info prints only the entries under it. A write that fails would
  # leave an earlier run's LDAP.yaml in place, so it must stop the script.
  if ! printf 'committers:\n%s\n' "${ldap_committers:-    []}" > "$DIR"/LDAP.yaml; then
    echo "    ERROR: Could not write $DIR/LDAP.yaml" >&2
    exit 1
  fi

  if ! [[ -d "$DIR"/"$repo" ]]; then
    echo "    git clone -q $gerritclonebase/$repo $DIR/$repo || exit 1"
    git clone -q "$gerritclonebase"/"$repo" "$DIR"/"$repo" || exit 1
  fi

  # Read both lists before comparing them: a read that fails must fail the
  # script, not count as a match or a difference.
  local ldap_list info_list
  if ! ldap_list=$(LEGACY_CLI=1 lftools-uv infofile get-committers "$DIR"/LDAP.yaml); then
    echo "    ERROR: Could not read the committers in $DIR/LDAP.yaml" >&2
    exit 1
  fi
  if ! info_list=$(LEGACY_CLI=1 lftools-uv infofile get-committers "$DIR"/"$repo"/INFO.yaml); then
    echo "    ERROR: Could not read the committers in $DIR/$repo/INFO.yaml" >&2
    exit 1
  fi
  # printf keeps an empty list empty, where a here-string would add a blank
  # line for diff to report.
  diff="$(diff <(printf '%s' "$ldap_list" | sort) <(printf '%s' "$info_list" | sort))"
  status="$?"

  diff_array=()
  onlyinINFO=()
  onlyinLDAP=()

  while IFS= read -r line; do
    diff_array+=( "$line" )
    if echo "$line" | grep -q ">";
    then
      onlyinINFO+=( "$(echo "$line" | awk -F"id: " '{ print $2 }')" )
    fi
    if echo "$line" | grep -q "<";
    then
      onlyinLDAP+=( "$(echo "$line" | awk -F"id: " '{ print $2 }')" )
    fi
  done < <(echo "${diff[@]}" )


  if ! [ "${#onlyinINFO[@]}" -eq 0 ]; then
    for missing in "${onlyinINFO[@]}"; do
      if [ -n "$missing" ]; then
        echo "    DUMMY: sending invite to $missing"
        LEGACY_CLI=1 lftools-uv infofile get-committers --id "$missing" "$DIR"/"$repo"/INFO.yaml 2>&-
      fi
    done
  fi

  if ! [ "${#onlyinLDAP[@]}" -eq 0 ]; then
    echo "    These users are listed as committers in LDAP and not in the INFO.yaml"
    for missing in "${onlyinLDAP[@]}"; do
      [ -n "$missing" ] || continue
      echo "    LEGACY_CLI=1 lftools-uv infofile sync-committers $DIR/$repo/INFO.yaml $DIR/LDAP.yaml $missing --repo $repo"
      if ! LEGACY_CLI=1 lftools-uv infofile sync-committers "$DIR"/"$repo"/INFO.yaml "$DIR"/LDAP.yaml "$missing" --repo "$repo"; then
        echo "    ERROR: Could not add $missing to $DIR/$repo/INFO.yaml" >&2
        status=1
      fi
    done

  fi

  echo "    Exit status = $status"
  exit "$status"

}

usage() {
cat << EOF
Must be called from lftools-uv
eg: LEGACY_CLI=1 lftools-uv ldap autocorrectinfofile
EOF
exit 1
}

if [[ -z "$*" ]]; then usage
fi

gerritclonebase="$1"
ldapgroup="$2"
repo="$3"
purpose="$4"
review="$5"

parseoptions "$@"
