#!/bin/sh

# @author Dominic Jäger <e1634025@student.tuwien.ac.at>
# @brief Perform a three-way merge with files using Elektra
# @date 2019-08-20
# @tags creator

ourpath="user/elektra/merge/our"
theirpath="user/elektra-cmerge/their"
basepath="user/elektra-cmerge/base"
resultpath="user/elektra-cmerge/result"
resultfile="elektra-cmerge_result"

if [ "$#" -ne 4 ]; then
	echo "Usage is $0 our their base format"
	exit 1
fi
format=$4
kdb rm -rf $ourpath
kdb rm -rf $theirpath
kdb rm -rf $basepath
kdb rm -rf $resultpath
cat $1 | kdb import $ourpath $format
cat $2 | kdb import $theirpath $format
cat $3 | kdb import $basepath $format
# uncomment lines to debug
# valgrind --leak-check=full --show-leak-kinds=all kdb cmerge $ourpath $theirpath $basepath $resultpath
# valgrind --leak-check=full kdb cmerge $ourpath $theirpath $basepath $resultpath
kdb cmerge $ourpath $theirpath $basepath $resultpath
kdb export $resultpath $format > $resultfile
kdb rm -rf $ourpath
kdb rm -rf $theirpath
kdb rm -rf $basepath
kdb rm -rf $resultpath
