]> git.rmz.io Git - dotfiles.git/blob - isync/post-sync.d/10-youtube
0b0363c25f0e514fcf2b72635f146d52e4212816
[dotfiles.git] / isync / post-sync.d / 10-youtube
1 #!/usr/bin/env bash
2
3 if [[ $mbsync_errno -ne 0 ]]; then
4 echo "mbsync failed, ignoring post-sync commands." >&2
5 exit
6 fi
7
8 mbsync_args=$*
9
10 if [[ "$mbsync_args" != "gmail" ]]; then
11 # only run for gmail account
12 exit
13 fi
14
15 yt_src_mb=youtube-orig
16 yt_dst_mb=youtube
17 yt_parser=$dir/parse-mail.py
18 maildirs=( $(sed -nr 's/^Path\s*(.*)$/\1/p' $config) )
19
20 if [[ -z $maildirs ]]; then
21 echo "Could not extract 'Path' from 'MaildirStore' in '$config'" >&2
22 exit 1
23 fi
24
25 shopt -s extglob
26 shopt -s nullglob
27 for maildir in "${maildirs[@]}"; do
28 # expand tilde
29 maildir=${maildir/#~/$HOME}
30
31 if [[ ! -d "$maildir/$yt_src_mb" ]]; then
32 echo "No mailbox at '$maildir/$yt_src_mb'"
33 continue
34 fi
35
36 for mail in "$maildir"/$yt_src_mb/new/* ; do
37 mangled_mail=${mail/$yt_src_mb/$yt_dst_mb}
38 # remove UID for mbsync to regenerate it
39 mangled_mail="${mangled_mail/,U=+([0-9])}"
40
41 echo -n "Parsing new message '$(basename "$mail")'..."
42 $yt_parser <"$mail" >"$mangled_mail"
43
44 if [[ $? -eq 0 ]]; then
45 echo " Success."
46 else
47 echo " Failure! Copying message as is."
48 cp "$mail" "$mangled_mail"
49 fi
50
51 # sync {a,m}time
52 touch --reference "$mail" "$mangled_mail"
53 mv "$mail" "${mail/new/cur}S"
54 done
55 # resync new yt_dst_mb
56 mbsync -c "$config" gmail-$yt_src_mb gmail-$yt_dst_mb
57 done