#!/bin/bash

# cat with buffers

# there was still some problem here:
#   cat does not open some of the pipes at all, until it's too late.
# I wrote my own 'catf', which does.

dir=$PWD
work_dir="$dir/.net.$$"
trap "kill -TERM -$$ 2>/dev/null; trap - EXIT; find '$work_dir' -type p | xa rm ; rmdir '$work_dir'; exit" HUP INT QUIT TERM PIPE EXIT
mkdir	"$work_dir"
#	catb - cat with file buffers
first=1
for pipe; do
	p="$work_dir/$pipe"
	if [ -n "$first" ]; then
		ln "$dir/$pipe" "$p"
		first=
	else
		mkfifo "$p"
		buf <"$pipe" >"$p" &
	fi
done
cd "$work_dir"
catf "$@" >&1 &
wait
