#!/bin/sh
#################################################################
#
#	@(#) tbm -- tmux buffer manager  (c) Apr 2021 hoz HZNET
#
#	* load files into named tmux buffer
#	* -s	show buffer content
#	* -c	clear buffer
#	* -r	rename buffer
#	* -l	list all tmux buffers
#
#	2023-07
#
#	* Option -p to write the whole content of the current pane
#	  to stdout.
#	* Option -d to write a "screen dump" to stdout
#	* Both options allows the tmux "capture-pane" options
#	  (-e -E -N -C) additionally
#
#	2024-05
#
#	* Commandsname (show, print, rename, etc) added
#	* Command "load" fills also the system paste buffer
#	  This is different to "no command"
#
#	Bugs
#		The namespaces for files and buffers are different.
#		The filename should be "tainted" before we use
#		it as a buffer name.
#
#################################################################
PATH=/bin:/usr/bin:/usr/local/bin

prog=`basename $0`

usage()
{
	test "$1" && echo "$prog: $1"

	echo "$prog: tmux buffer manager" 1>&2
	echo "usage: $prog [load | -s|show | -c|clear | -l|list | -r|rename] ..." 1>&2
	echo "usage: $prog -h 			this help" 1>&2
	echo "" 1>&2
	echo "usage: $prog [<file>]		load file or stdin into the default buffer" 1>&2
	echo "usage: $prog load [<file>]	load also into the system paste buffer" 1>&2
	echo "usage: $prog <buffer> <file>	load file into named buffer (use '-' for stdin)" 1>&2
	echo "" 1>&2
	echo "usage: $prog -l			list all buffers (with content)" 1>&2
	echo "usage: $prog -s [<buffer>]	show (default) buffer content" 1>&2
	echo "" 1>&2
	echo "usage: $prog -c [<buffer>]	clear (default) buffer" 1>&2
	echo "usage: $prog -r [<buffer>] <name>	rename (default) buffer" 1>&2
	echo "" 1>&2
	echo "usage: $prog -p [{-e|-N|-J}]	print the content of the current pane to stdout" 1>&2
	echo "usage: $prog -d [{-e|-N}] [-t <pane>]	print a \"screen dump\" of the current pane to stdout" 1>&2

	exit 1
}

act=load
load_pastebuffer=no
while test $# -gt 0
do
	case $1 in
	-h)	usage ;;
	-l|list)	act=list ;;
	-s|show)	act=show ;;
	-c|clear)	act=clear ;;
	-r|rename)	act=rename ;;
	load)		act=load ; load_pastebuffer=also ;;
	-p|-d|print|dump)
		case "$1" in
		-p|print)	act=print;;	# print current pane
		-d|dump)	act=dump;;	# screen dump current pane
		esac
		shift
		opts="$@"
		shift $#
		break
		;;
	-*)	usage "Illegal option $1" ;;
	*)	break ;;
	esac
	shift
done

if test $# -ge 1
then
	# "load" with 2 args or all other actions
	buffer="$1"
	if test $act != load -o $# -eq 2
	then
		shift
	fi
	bufferopt="-b $buffer"
fi

case $act in
list)	tmux list-buffers
	;;
show)	tmux show-buffer $bufferopt
	;;
clear)	tmux delete-buffer $bufferopt
	;;
load)	tmux load-buffer $bufferopt ${1:-"-"}
	if test $load_pastebuffer = also
	then
		if type xclip > /dev/null 2>&1
		then
			tmux show-buffer $bufferopt | xclip -in -selection clipboard
		elif type pbcopy > /dev/null 2>&1
		then
			tmux show-buffer $bufferopt | pbcopy
		fi
	fi
	;;
rename)	if test $# -lt 1
	then
		newname="$buffer"
		bufferopt=""
	else
		newname="$1"
	fi
	tmux set-buffer $bufferopt  -n $newname
	;;
print)	tmux capture-pane -pS - $opts
	;;
dump)	tmux capture-pane -J -p $opts
	;;
esac
