#!/usr/local/bin/bash

td="/tmp/sd$$";

trap "rm -rf $td" EXIT
mkdir $td

grep '=====' $1 |
sed -e 's/.*(//' -e 's/).*//' -e 's/,/ /' >$td/all

n=$( head -100 $1 | sed -e '/^#L:[ 	]*N=/!d' -e 's/.*N=//' )
echo N=$n

{
cat <<ENDEND

data "$td/all"
read {x 1 y 2}
terminal x11
ctype default
limits x y
erase
box
points x y
points y x

macro min 2	{ (($1)<($2) ? ($1) : ($2)) }

macro diff {
	set s=0.5*(x+y)
	set s=abs(x-y)<abs(x-y-$n)?0.5*(x+y):(x+y<$n?0.5*(x+y-$n):0.5*(x+y+$n))
	set d=abs(x-y)<abs(x-y-$n)?abs(x-y):abs(x-y-$n)
	limits s d
	erase
	box
	ctype green
	points s d
	ctype default
	}

ENDEND

cat
} | sm
