1#!/bin/bash
2
3if [ $# -eq 2 ]
4  then
5	OLD=$1
6	NEW=$2
7
8	# We need a tab character as a field separator
9	TAB=`echo -e "\t"`
10
11	#Temporary storage
12	TEMPFILE=`mktemp /tmp/catmerge.XXXXX`
13
14	# Extract the list of keys to remove
15	# Compare (diff) the keys only (cut) ; keep only 'removed' lines (grep -),
16	# Ignore diff header and headlines from both files (tail), remove diff's
17	# prepended stuff (cut)
18	# Put the result in our tempfile.
19	diff -u <(cut -f 1,2 $OLD) <(cut -f 1,2 $NEW) |grep ^-|\
20		tail -n +3|cut -b2- > $TEMPFILE
21
22	# Reuse the headline from the new file (including fingerprint). This gets
23	# the language wrong, but it isn't actually used anywhere
24	head -1 $NEW
25	# Sort-merge old and new, inserting lines from NEW into OLD (sort);
26	#	Exclude the headline from that (tail -n +2)
27	# Then, filter out the removed strings (fgrep)
28	sort -u -t"$TAB" -k 1,2 <(tail -n +2 $OLD) <(tail -n +2 $NEW)|\
29		fgrep -v -f $TEMPFILE
30
31	rm $TEMPFILE
32
33  else
34	echo "$0 OLD NEW"
35	echo "merges OLD and NEW catalogs, such that all the keys in NEW that are"
36	echo "not yet in OLD are added to it, and the one in OLD but not in NEW are"
37	echo "removed. The fingerprint is also updated."
38fi
39