#!/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 commiters
  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"

  if lftools ldap yaml4info "$ldapgroup" 2>&- > "$DIR"/LDAP.yaml
    then
      echo "    LDAP lookup sucssesfull"
    else
      echo "    LDAP lookup failed"
      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

  diff="$(diff <(lftools infofile get-committers "$DIR"/LDAP.yaml 2>&-| sort) <(lftools infofile get-committers "$DIR"/"$repo"/INFO.yaml 2>&- | sort))"
  status="$?"

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

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


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

  if ! [ "${#onlyinLDAP[@]}" -eq 0 ]; then
    echo "    These users are listed as commiters in LDAP and not in the INFO.yaml"
    for missing in "${onlyinLDAP[@]}"; do
      echo "    lftools infofile sync-committers $DIR/$repo/INFO.yaml $DIR/LDAP.yaml $missing --repo $repo 2>&-"
      lftools infofile sync-committers "$DIR"/"$repo"/INFO.yaml "$DIR"/LDAP.yaml "$missing" --repo "$repo" 2>&-
    done

  fi

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

}

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

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

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

parseoptions "$@"
