#!/usr/bin/env rc

# Dmerge - Merge notes or regions
# Usage: Dmerge <source-id> <dest-id>          (file merge)
#        Dmerge <dest-id> [format]             (region merge, in acme)

annotation='MERGED FILE:'
regionannotation='MERGED REGION:'
mnt=$DENOTE_9MOUNT
if(~ $#mnt 0) mnt=$HOME/mnt/denote

hasselection=0
if(! ~ $winid '') {
	selection=`{9p read acme/$winid/rdsel}
	if(! ~ $#selection 0 && ! ~ $selection '') {
		hasselection=1
	}
}

if(~ $#* 2) {
	if(~ $hasselection 1) {
		dst=$1
		format=$2
		mode=region
		tag=`{9p read acme/$winid/tag}
		filename=`{echo $tag | awk '{for(i=1;i<=NF;i++) if($i ~ /[0-9]{8}T[0-9]{6}/) {print $i; exit}}'}
		src=`{echo $filename | sed 's/--.*//'}
	}
	if not {
		src=$1
		dst=$2
		mode=file
	}
}
if not if(~ $#* 1) {
	if(~ $winid '') {
		echo 'region merge requires running from acme window' >[1=2]
		exit 1
	}
	if(~ $hasselection 0) {
		echo 'no text selected' >[1=2]
		exit 1
	}
	dst=$1
	format=plain
	mode=region
	tag=`{9p read acme/$winid/tag}
	filename=`{echo $tag | awk '{for(i=1;i<=NF;i++) if($i ~ /[0-9]{8}T[0-9]{6}/) {print $i; exit}}'}
	src=`{echo $filename | sed 's/--.*//'}
}
if not {
	echo 'usage: Dmerge source-id dest-id OR Dmerge dest-id [format] (in acme)' >[1=2]
	exit 1
}

if(~ $mode file) {
	srcpath=`{cat $mnt/n/$src/path}
	dstpath=`{cat $mnt/n/$dst/path}
	srctitle=`{cat $mnt/n/$src/title}

	if(~ $#srcpath 0 || ~ $#dstpath 0) {
		echo 'error: note not found' >[1=2]
		exit 1
	}

	if(! ~ $srcpath *.md *.org *.txt) {
		echo 'error: source must be .md, .org, or .txt file' >[1=2]
		exit 1
	}
	if(! ~ $dstpath *.md *.org *.txt) {
		echo 'error: destination must be .md, .org, or .txt file' >[1=2]
		exit 1
	}

	awk '
	BEGIN { found_blank = 0 }
	found_blank { print }
	!found_blank && /^$/ { found_blank = 1 }
	' $srcpath > /tmp/content.$pid

	{
		cat $dstpath
		if(! ~ $annotation '') {
			echo ''
			echo '* * *'
			echo ''
			echo $annotation $"srctitle
			echo ''
		}
		cat /tmp/content.$pid
	} > /tmp/merge.$pid

	rm /tmp/content.$pid

	for(linkid in `{cat $mnt/n/$src/backlinks | awk -F'|' '{print $1}'}) {
		linkid=`{echo $linkid | awk '{gsub(/^ +| +$/, ""); print}'}
		if(! ~ $#linkid 0) {
			linkpath=`{cat $mnt/n/$linkid/path}
			if(! ~ $#linkpath 0) {
				sed 's/denote:'$src'/denote:'$dst'/g' $linkpath > /tmp/relink.$pid
				cp /tmp/relink.$pid $linkpath
				rm /tmp/relink.$pid
			}
		}
	}

	cp /tmp/merge.$pid $dstpath
	rm /tmp/merge.$pid

	echo 'd' > $mnt/n/$src/ctl

	echo 'Merged' $src 'into' $dst
}

if not if(~ $mode region) {
	dstpath=`{cat $mnt/n/$dst/path}

	if(~ $#dstpath 0) {
		echo 'error: destination note not found' >[1=2]
		exit 1
	}

	if(! ~ $dstpath *.md *.org *.txt) {
		echo 'error: destination must be .md, .org, or .txt file' >[1=2]
		exit 1
	}

	switch($format) {
	case plain
		formatted=$selection
	case plain-indented
		formatted=`{echo $selection | sed 's/^/    /'}
	case org-src
		formatted=`{echo '#+begin_src'; echo $selection; echo '#+end_src'}
	case org-quote
		formatted=`{echo '#+begin_quote'; echo $selection; echo '#+end_quote'}
	case org-example
		formatted=`{echo '#+begin_example'; echo $selection; echo '#+end_example'}
	case md-quote
		formatted=`{echo $selection | sed 's/^/> /'}
	case md-fence
		formatted=`{echo '```'; echo $selection; echo '```'}
	case *
		echo 'unknown format:' $format >[1=2]
		exit 1
	}

	{
		cat $dstpath
		if(! ~ $regionannotation '') {
			echo ''
			echo '* * *'
			echo ''
			echo $regionannotation
			echo ''
		}
		echo $"formatted
	} > /tmp/merge.$pid

	cp /tmp/merge.$pid $dstpath
	rm /tmp/merge.$pid

	echo -n 'denote:'$dst | 9p write acme/$winid/wrsel

	echo 'Region merged from' $src 'into' $dst '(format:' $format')'
}
