REVEEEEERT!
[gofetch.git] / gopher.sh
1 #!/bin/sh
2
3 # $0 [prog ($1 will be the query)] ([port])
4
5 [ "$ADDR" = "" ] && ADDR=127.0.0.1
6 [ "$LOG" = "" ] && LOG=/tmp/log.err
7 [ "$PIDF" = "" ] && PIDF="`mktemp`"
8 [ "$TIMEOUT" = "" ] && TIMEOUT=10
9 [ "$MAXCON" = "" ] && MAXCON=1024
10 prog="$1"
11 port="$2"
12 [ "$port" = "" ] && port=70
13
14 if [ "$prog" = "" ]; then
15 rm "$PIDF"
16 echo "Syntax: $0 [prog (\$1 will be the query)] ([port])" >&2
17 exit 1
18 fi
19
20 tmpd=`mktemp -t -d .gopher-service.XXXXXX`
21 if [ ! -d "$tmpd" ]; then
22 rm "$PIDF"
23 echo "Cannot create temporary directory, aborting..." >&2
24 exit 2
25 fi
26
27 touch "$PIDF"
28 echo "$PIDF"
29
30 i=0
31 while [ -e "$PIDF" ]; do
32 i=`expr $i + 1`
33 [ $i -gt "$MAXCON" ] && i=1
34 fifo="$tmpd/fifo.$i"
35 rm -f "$fifo"
36 mkfifo "$fifo"
37 < "$fifo" nc -l -q0 -w"$TIMEOUT" "$ADDR" -p "$port" | (
38 read -r query
39 query="`echo "$query" | cut -f2 -d' '`"
40 (
41 "$prog" "$query" 2>> "$LOG"
42 rm "$fifo"
43 )&
44 ) > "$fifo"
45 done
46 rm -rf "$tmpd"
47