summaryrefslogtreecommitdiff
path: root/maintainer/mutt-hooks/b4-shazam.sh
blob: 49206583bdc7361bc359ae9515fa37a6bc2b9c81 (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
#!/bin/bash

REPO_DIR=/home/cem/Source/MAINTAINER/xfs-linux
FORMAIL=$(which formail)
B4=$(which b4)

_die() {
	echo $1
	exit 1
}

check_env() {

	if [ ! -d $REPO_DIR ]; then
		_die "Linux repo does not exist"
	fi

	if [ -z $FORMAIL ]; then
		_die "Formail program missing"
	fi

	if [ -z $B4 ]; then
		_die "b4 program missing"
	fi
}

shazam_message() {
	local MSG_ID=$1
	local CUR_DIR=$PWD

	cd $REPO_DIR

	$B4 shazam -s $MSG_ID

	if [ $? -ne 0 ]; then
		cd $CUR_DIR
		_die "Failed to apply $MSG_ID"
	fi

	cd $CUR_DIR
	echo "Message/Series $MSG_ID applied"
}

check_env

MSG=""

if [ ! -t 0 ]; then
	MSG=$(cat)
else
	_die "No email has been piped"
fi

# Quote $MSG to trick the shell's IFS
MESSAGE_ID=$(echo "$MSG" | formail -x message-id | sed 's/[<>]//g')

if [ -z $MESSAGE_ID ]; then
	_die "Message is empty"
fi

shazam_message $MESSAGE_ID