aboutsummaryrefslogtreecommitdiffstats
path: root/teaqueue-server
blob: 02c8b6a90a0bfedfeda8929ee24881fd51b90c3c (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
#!/bin/bash
# use bash for trap

set -eu

# default options
queuefn="queue.txt"
donefn="done.txt"
port=1340

usage()
{
    cat <<EOF
Usage: $0 [OPTIONS...]

Feeds lines from a text file to teaqueue-clients.

Options:
  -h, --help   show usage
  -p, --port   what port to use (default: 1340)
  -q, --queue  what filename to use as input queue
  -d, --done   what filename to use for storing done items
EOF
}

# arg parsing
TEMP=$(getopt -o hH:p: --long help,host,port -n teaqueue-client -- "$@")
if [ $? != 0 ]; then echo "Terminating..." >&2; exit 1; fi
eval set -- "$TEMP"
while true; do
    case "$1" in
        -h|--help) usage; exit 0;;
        -p|--port) port="$2"; shift 2;;
        -q|--queue) queuefn="$2"; shift 2;;
        -d|--done) donefn="$2"; shift 2;;
        --) shift; break;;
        *) echo "Arguments parsing error" >&2; exit 1;;
    esac
done

my_exit()
{
    mv "${queuefn}" "${queuefn}.old"
    comm --nocheck-order -23 "${queuefn}.old" "${donefn}" > "${queuefn}"
    exit 0
}

trap my_exit SIGINT SIGTERM

cat "${queuefn}" | while read -r line; do
    printf "%s\n" "$line" | nc -q0 -l -p $port
    printf "%s\n" "$line"
done > "${donefn}"