#!/usr/bin/env bash

# Concepts for resolving Git conflicts
# https://gist.github.com/karenyyng/f19ff75c60f18b4b8149

BIN_DIR=/usr/local/bin
#BIN_DIR=~/bin

sudo apt install -y meld
GUI_DIFF_TOOL=meld # graphical diff/merge tool
GUI_MERGE_TOOL=meld
git config --global diff.guitool ${GUI_DIFF_TOOL}
git config --global merge.guitool ${GUI_MERGE_TOOL}
git config --global mergetool.meld.hasOutput true

# Available tools:
git difftool --tool-help

TOOL_PATH=nvim
DIFF_TOOL=nvimdiff # terminal diff/merge tool
MERGE_TOOL=nvimdiff
# DIFF_STYLE="LOCAL,MERGED,REMOTE"
DIFF_STYLE="LOCAL,BASE,REMOTE / MERGED"

git config --global merge.tool ${DIFF_TOOL}
git config --global mergetool.${DIFF_TOOL}.layout "${DIFF_STYLE}"

#    $LOCAL is the file in the current branch (e.g. master).
#    $REMOTE is the file in the branch being merged (e.g. branch_name).
#    $MERGED is the partially merged file with the merge conflict information in it.
#    $BASE is the shared commit ancestor of $LOCAL and $REMOTE, this is to say the file as it was when the branch containing $REMOTE was originally created.

# if unset the diff tool follows the mergetool settings.
# default path is vim.
#
# 2019
# git config --global diff.tool vimdiff3
# git config --global difftool.vimdiff3.path nvim
# git config --global merge.tool vimdiff3
# git config --global mergetool.vimdiff3.path

git config diff.tool ${DIFF_TOOL}
git config --global diff.tool ${DIFF_TOOL}
git config difftool.prompt false
git config --global difftool.prompt false
git config --global difftool.${DIFF_TOOL}.path "${TOOL_PATH}"

git config merge.tool ${MERGE_TOOL}
git config --global merge.tool ${MERGE_TOOL}
git config merge.layout "${DIFF_STYLE}"
git config --global mergetool.${MERGE_TOOL}.layout "${DIFF_STYLE}"
git config --global mergetool.${MERGE_TOOL}.path ${TOOL_PATH}

#git config --global mergetool.${MERGE_TOOL}.cmd ${MERGE_TOOL} '$LOCAL $BASE $MERGED'

git config alias.d difftool
git config --global alias.d difftool
#git config --global difftool.${DIFF_TOOL}.cmd ${DIFF_TOOL} '$LOCAL $REMOTE'

#git config mergetool.${MERGE_TOOL}.cmd ${MERGE_TOOL} '$LOCAL $BASE $MERGED'

#git config --global difftool.${DIFF_TOOL}.cmd ${DIFF_TOOL} '$LOCAL $REMOTE'

echo "Global: Active mergetool: $(git config --global merge.tool)"
echo "Global: Merge style: $(git config --global merge.conflictstyle)"

echo "Global: Active difftool: $(git config --global diff.tool)"
echo "Global: Prompt for difftool: $(git config --global difftool.prompt)"

echo "Active mergetool: $(git config merge.tool)"
echo "Merge style: $(git config merge.conflictstyle)"

echo "Active difftool: $(git config diff.tool)"
echo "Prompt for difftool: $(git config difftool.prompt)"

# to use an unsupported tool see below.
#git config --global diff.external ${BIN_DIR}/git-diff-vimdiff.sh

# the executable specified by GIT_EXTERNAL_DIFF will be called with a fixed set of 7 arguments:

# path old-file old-hex old-mode new-file new-hex new-mode

#cat > ${BIN_DIR}/git-diff-meld.sh <<'EOF'
##!/bin/bash
#echo $*
#meld $2 $5
#EOF
#
#chmod +x ${BIN_DIR}/git-diff-meld.sh
#
#cat > ${BIN_DIR}/git-diff-vimdiff.sh <<'EOF'
##!/bin/bash
#echo $*
#vimdiff $2 $5
#EOF
#
#chmod +x ${BIN_DIR}/git-diff-vimdiff.sh
