#!/bin/bash
# bash, not POSIX sh, because of "readarray".

echo "Please use the program in https://github.com/plume-lib/merging ."
echo "You are using an obsolete $0 from https://github.com/plume-lib/plume-scripts ."

# This script edits files in place to remove conflict markers related to Java
# imports.  For a given conflict that involves only `import` statements and
# blank lines, the output includes every `import` statement that is in either
# of the parents.  This script leaves other conflicts untouched.

# Usage:
#   resolve-import-conflicts [file ...]
#
# The script works on all files given on the command line.
# If none are given, the script works on all files in or under the current directory.
#
# The exit status code is 0 (success) if all conflicts are resolved in all the files.
# The exit status code is 1 (failure) if any conflict remains.

# This script is not a git mergetool.  A git mergetool is given the base, parent 1, and
# parent 2 files, all without conflict markers.
# However, this can be run after a git mergetool that leaves conflict markers
# in files, as the default git mergetool does.

if [ "$#" -eq 0 ]; then
  readarray -t files < <(grep -l -r '^<<<<<<< HEAD' .)
else
  files=("$@")
fi

SCRIPTDIR="$(cd "$(dirname "$0")" && pwd -P)"

status=0

for file in "${files[@]}"; do
  if ! "${SCRIPTDIR}"/resolve-conflicts.py --java_imports "$file"; then
    status=1
  fi
done

exit $status
