REVEEEEERT!
[gofetch.git] / gopher.sh
CommitLineData
c8b05198
NR
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
10prog="$1"
11port="$2"
12[ "$port" = "" ] && port=70
13
14if [ "$prog" = "" ]; then
15 rm "$PIDF"
16 echo "Syntax: $0 [prog (\$1 will be the query)] ([port])" >&2
17 exit 1
18fi
19
20tmpd=`mktemp -t -d .gopher-service.XXXXXX`
21if [ ! -d "$tmpd" ]; then
22 rm "$PIDF"
23 echo "Cannot create temporary directory, aborting..." >&2
24 exit 2
25fi
26
27touch "$PIDF"
28echo "$PIDF"
29
30i=0
31while [ -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"
45done
46rm -rf "$tmpd"
47