aboutsummaryrefslogtreecommitdiff
path: root/completion/make.sh
blob: b8f0d47eb3fd30a34a42a295f2854cc12b4e791e (plain) (blame)
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
#!/bin/bash

pacman_bash_completion='/usr/share/bash-completion/completions/pacman'
pacman_zsh_completion='/usr/share/zsh/site-functions/_pacman'

function error() {
  local code="$?"
  rm "$1"
  exit "$code"
}

function apply-patch() {
  for f in "$@"; do
    patch -sNp1 --dry-run -i "$f" > /dev/null && {
      patch -sNp1 -r - --no-backup-if-mismatch -i "$f"
      return "$?"
    }
  done

  # show error applying first patch
  patch -sNp1 -r - --no-backup-if-mismatch -i "$1"
}

function delete-shell-fn() {
  perl -0777 -pe 's/\n'"$1"'\(\) *\{\n([^}].*\n)*\}\n*/\n\n/g;s/\n{3,}/\n\n/g'
}

function delete-shell-array() {
  perl -0777 -pe 's/\n'"$1"'=\(\n([^)].*\n)*\)\n?//g'
}

[ "$1" = 'bash' ] && {
  cat "$pacman_bash_completion" |
  delete-shell-fn '_pacman_keyids' |
  delete-shell-fn '_pacman_key' |
  delete-shell-fn '_makepkg' |
  sed 's/^_pacman() {$/_pakku() {/' \
  > 'bash' ||
  error 'bash'

  apply-patch 'bash.patch' 'bash-git.patch' ||
  error 'bash'

  exit 0
}

[ "$1" = 'zsh' ] && {
  cat "$pacman_zsh_completion" |
  delete-shell-array '_key_shortopts' |
  delete-shell-array '_key_longopts' |
  delete-shell-array '_pacman_key_options' |
  delete-shell-fn '_pacman_key' |
  delete-shell-fn '_keys' |
  delete-shell-array '_makepkg_shortopts' |
  delete-shell-array '_makepkg_longopts' |
  delete-shell-fn '_makepkg_action_none' |
  delete-shell-fn '_makepkg' |
  sed 's/_pacman/_pakku/g' \
  > 'zsh' ||
  error 'zsh'

  apply-patch 'zsh.patch' 'zsh-git.patch' ||
  error 'zsh'

  exit 0
}

exit 1