summaryrefslogtreecommitdiff
path: root/carddav-to-alpine
blob: 66d885f11064bbd621583a1a58cf4431a395c06b (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
#!/bin/bash

. "${0%/*}/common"

error=0

for file in 'addressbook' 'uids'; do
  if [ -n "$(git -C "${git_dir}" diff -- "${file}")" ]; then
    >&2 printf 'File "%s" was changed relative to git.\n' "${file##*/}"
    exit 1
  fi
  rm -f "${git_dir}"'/'"${file}"
  touch "${git_dir}"'/'"${file}"
done

find_vcfs \
| while read -r address_file; do
  uid="${address_file##*/}"
  uid="${uid%.vcf}"

  for key in "${keys[@]}"; do
    eval 'unset _'"${key}"
  done

  while read -r key value; do
    eval '_'"${key}"'="${value}"'
  done < <(
    sed -n '
      s@\t@ @g
      s@\r$@@
      /^BEGIN:VCARD$/,/END:VCARD$/ {
        /^\('"$(
          printf '%s\\|' "${keys[@]}" \
          | sed 's@\\|$@@'
        )"'\):/ {
          s@:@ @
          p
        }
      }
    ' "${address_file}"
  )

  if [ "${_UID}" != "${uid}" ]; then
    >&2 printf 'uids differ in %s: "%s" != "%s"\n' \
      "${address_file##*/}" "${uid}" "${_UID}"
    error=1
    continue
  fi

  printf '%s\t' \
    "${_UID}" \
    "${_NICKNAME}" \
    "${_FN}" \
    "${_EMAIL}" \
    "${_FCC}" \
    "${_COMMENT}" \
  | sed '
    s@\t\+$@\n@
  ' \
  >>"${git_dir}"'/uids'
done

sed '
  s@^\S\+\s@@
' "${git_dir}"'/uids' \
>"${git_dir}"'/addressbook'

exit ${error}