From 604f3d64764270c052cfb43081ec522237bbdb75 Mon Sep 17 00:00:00 2001
From: Ludovic Pouzenc <ludovic@pouzenc.fr>
Date: Fri, 5 May 2017 11:28:51 +0200
Subject: Massive add for all draft stuff to keep it in sync

---
 draft/uftp/sinkcat.sh | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100755 draft/uftp/sinkcat.sh

(limited to 'draft/uftp/sinkcat.sh')

diff --git a/draft/uftp/sinkcat.sh b/draft/uftp/sinkcat.sh
new file mode 100755
index 0000000..20ac948
--- /dev/null
+++ b/draft/uftp/sinkcat.sh
@@ -0,0 +1,39 @@
+#!/bin/busybox sh
+
+if [ $# -ne 1 ]
+then	cat <<EOT
+Usage: $(basename $0) <sink-dir>
+  Concatenate then delete files as soon they appear in sink-dir to stdout.
+  If multiple files are found in sink, the first in alphabetical order is choosen.
+  <sink-dir> must not exists, this program must create it (avoiding mistakes).
+  Dropping an empty file in dir-sink will clean exit this program.
+EOT
+	exit 1
+fi
+
+SINKDIR=$1
+mkdir "$SINKDIR" && cd "$SINKDIR"
+if [ $? -ne 0 ]
+then	echo "Cannot mkdir/chdir to '$SINKDIR'" >&2
+	exit 2
+fi
+
+while true
+do
+	f=$(ls | head -n1)
+	if [ -n "$f" ]
+	then	if [ -f "$f" -a -r "$f" ]
+		then	size=$(stat -c'%s' -- "$f")
+			# Do the actual work on the following line
+			cat -- "$f" && rm -v -- "$f" >&2
+			# Normal exit condition
+			if [ $size -eq 0 ]
+			then	cd / && rmdir -v -- "$SINKDIR" >&2
+				exit 0
+			fi
+		else	echo "'$SINKDIR/$f' is not a readable file" >&2
+			exit 3
+		fi
+	fi
+	sleep 1
+done
-- 
cgit v1.2.3