summaryrefslogtreecommitdiff
path: root/bin/ii-connect
blob: 9ec0702518f841a055d6e1ca447cc482923976a1 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh

# run and handle the irc client

# shellcheck source=conf/default.conf
. "${0%/*}/../conf/default.conf"

# start ii if it is not running
if ! pgrep -x ii > /dev/null; then
  rm -rf --one-file-system "${irc_dir}"
  screen -d -m ii -f buildmaster -n buildmaster
  sleep 10
fi

# register if not yet done
if tail -n1 "${irc_dir}/nickserv/out" 2> /dev/null | \
  grep -qF 'This nickname is registered. Please choose a different nickname'; then
  printf 'identify %s\n' "${irc_password}" > \
    "${irc_dir}/nickserv/in"
fi

# join #archlinux-ports if not yet done
if ! grep ' buildmaster\.archlinux32\.org .* buildmaster$' "${irc_dir}/out" | \
  tail -n1 | \
  grep -q ' #archlinux-ports '; then
  {
    echo '/j #archlinux-ports'
    echo '/WHO buildmaster'
  } > \
    "${irc_dir}/in"
fi

# start watch daemon if not running yet
if ! pgrep -xf 'SCREEN -d -m ii-connect watch' > /dev/null; then
  screen -d -m ii-connect watch
fi

# watch if asked to
if [ "$1" = 'watch' ]; then
  while pgrep -x 'ii' > /dev/null; do
    find "${irc_dir}" \
      -regextype sed \
      -type f \
      -name 'out' \
      -printf '%p\n' | \
      while read -r out_file; do
        channel="${out_file%/out}"
        channel="${channel##*/}"
        if [ -z "${channel%%#*}" ]; then
          prefix='buildmaster: '
        else
          prefix=''
        fi
        regex='^\S\+ \S\+ <\S\+> '"${prefix}"'why[- ]don'"'"'\?t[- ]you \(build\|stabilize\|unstage\|keep\|stubbornly_keep\) '
        if grep -q "${regex}" "${out_file}"; then
          sed -n '
            s/'"${regex}"'/\1 /
            T
            p
          ' "${out_file}" | \
            while read -r line; do
              reason=$(
                echo "${line}" | \
                  xargs why-dont-you 2>&1
              )
              if [ "$(echo "${reason}" | wc -l)" -le 5 ]; then
                echo "${reason}"
              else
                echo "${reason}" | \
                  head -n5
                printf '... (%s lines total)\n' "$(echo "${reason}" | wc -l)"
              fi > \
                "${out_file%/out}/in"
            done
          sed -i "/${regex}/d" "${out_file}"
        fi
      done
    sleep 1
  done
  exit
fi