summaryrefslogtreecommitdiff
path: root/draft/uftp/sinkdo.sh
blob: 1b46437b95b9f550e6f6e308a697f4950a15aec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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