-
Notifications
You must be signed in to change notification settings - Fork 1
/
badger.sh
executable file
·120 lines (91 loc) · 2.4 KB
/
badger.sh
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
# Badger
#
# Michal Tynior (c) 2018
# License: MIT
#
# https://github.com/mtynior/badger
function checkTools() {
PATH_TO_IMAGE_MAGIC=$(which convert)
if [ -z $PATH_TO_IMAGE_MAGIC ]; then
logError "You have to install ImageMagick to use 'badger'."
echo "Install it using: 'brew install imagemagick'"
exit 1
fi
}
function addBadge() {
ICON_PATH=$1
BADGE_PATH=$2
WIDTH=$(identify -format %w ${ICON_PATH})
HEIGHT=$(identify -format %h ${ICON_PATH})
SIZE="${WIDTH}x${HEIGHT}"
composite -resize ${SIZE} ${BADGE_PATH} ${ICON_PATH} ${ICON_PATH}
}
function reportUsage() {
echo "Usage: ${0} -b <path to badge file> -i <path to .appiconset file>"
}
function logError() {
echo -e "\033[1;31m${1}\033[0m"
}
function logInfo() {
echo -e "\033[1;36m${1}\033[0m"
}
function logDebug() {
echo $1
}
function logSuccess() {
echo -e "\033[1;32m${1}\033[0m"
}
#------------------------------------------------------------------------------
# Main script
BADGER_VERSION="1.0.0"
logInfo "Badger v${BADGER_VERSION}"
# Check if we have all available tools
checkTools
# Set an initial values for the flags
BADGE_IMAGE_PATH=""
ICONSET_PATH=""
# Read options
OPTS=`getopt b:i: $*`
eval set -- "$OPTS"
if [ $? != 0 ] ; then
reportUsage
exit 1
fi
# Extract options and their arguments into variables.
while true ; do
case "$1" in
-b) BADGE_IMAGE_PATH=$2 ; shift 2 ;;
-i) ICONSET_PATH=$2 ; shift 2 ;;
--) shift ; break ;;
*) reportUsage ; exit 1 ;;
esac
done
# Check if we got all reqired arguments
if [ -z $BADGE_IMAGE_PATH ]; then
logError "Path to badge image is missing."
reportUsage
exit 1
fi
if ! [ -f "$BADGE_IMAGE_PATH" ]; then
logError "Could not find file ${BADGE_IMAGE_PATH}"
exit 1
fi
if [ -z $ICONSET_PATH ]; then
logError "Path to .appiconset file is missing."
reportUsage
exit 1
fi
if ! [ -d "$ICONSET_PATH" ]; then
logError "Could not find file ${ICONSET_PATH}"
exit 1
fi
# Get all icons from .appiconset
ICONS=$(find -E "$ICONSET_PATH" -regex '.*\.(png|PNG)')
NUMBER_OF_ICONS=$(wc -l <<< "$ICONS" | xargs)
logInfo "Found ${NUMBER_OF_ICONS} icons."
while read -r icon; do
logDebug "Adding badge to ${icon}."
addBadge $icon "$BADGE_IMAGE_PATH"
done <<< "$ICONS"
logSuccess "Successfully finished adding badge to ${NUMBER_OF_ICONS} icons."