diff options
Diffstat (limited to 'draft/uftp/sinkdo.sh')
-rwxr-xr-x | draft/uftp/sinkdo.sh | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/draft/uftp/sinkdo.sh b/draft/uftp/sinkdo.sh new file mode 100755 index 0000000..1b46437 --- /dev/null +++ b/draft/uftp/sinkdo.sh @@ -0,0 +1,57 @@ +#!/bin/busybox sh + +if [ $# -ne 2 ] +then cat <<EOT +Usage: $(basename $0) <land-dir> <sink-dir> +EOT + exit 1 +fi + +LANDDIR=$1 +SINKDIR=$2 +cd "$LANDDIR" || exit 2 + +curtask="(start)" +while true +do + f=$(ls | head -n1) + if [ -z "$f" ] + then sleep 1 + continue + fi + if [ ! -f "$f" -o ! -r "$f" ] + then echo "'$LANDDIR/$f' is not a readable file" >&2 + exit 2 + fi + size=$(stat -c'%s' -- "$f") + task=${f:0:2} + if [ "$curtask" == "$task" ] + then # Next file of an already started task + mv "$f" "$SINKDIR/" + else # Switch to the next task + if [ -d "$SINKDIR" ] + then # Inform sinkcat about end-of-data + touch -- "$SINKDIR/zz" + wait + # sinkcat always rmdir "$SINKDIR" on normal exit + if [ -d "$SINKDIR" ] + then echo "Task $task has ran into troubles" >&2 + exit 3 + fi + fi + echo "Switching from task $curtask to $task" >&2 + curtask=$task + if [ "$task" == "99" ] + then # Normal exit condition + rm -v "$f" >&2 + reset + echo "All task completed sucessfully" >&2 + exit 0 + else # Start a new task + chmod +x -- "$f" # XXX Checks on $f (is a script ?) + sinkcat.sh $SINKDIR | "./$f" & + while [ ! -d "$SINKDIR" ]; do sleep 1; done + rm -v "$f" >&2 + fi + fi +done |