-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcover-extract
executable file
·146 lines (122 loc) · 3.45 KB
/
cover-extract
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# cover-extract
# Copyright (C) 2013 iuk (http://www.iukonline.com/)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
UNRAR=/usr/bin/unrar-nonfree
UNZIP=/usr/bin/unzip
TMPDIR=/tmp
IMGEXT="bmp BMP gif GIF jpg JPG jpeg JPEG png PNG"
NOCOLOR=0
# Usage: color shade
function color {
# Are we in Cron?
if [ ! -t 0 -o $NOCOLOR = 1 ]; then return 1; fi
case $1 in
off|OFF) echo -n '[0m';;
red|RED) echo -n '[1;31m';;
yellow|YELLOW) echo -n '[1;33m';;
green|GREEN) echo -n '[1;32m';;
blue|BLUE) echo -n '[1;34m';;
bell|BELL) echo -n '';;
*) ;;
esac
}
# Usage: error "String to display"
error() {
color BELL;color RED
printf "%s: %s\n" $"ERROR" "$1" >&2
color OFF
}
# Usage: warning "String to display"
warning() {
color YELLOW
printf "%s: %s\n" $"WARNING" "$1"
color OFF
}
info() {
color BLUE
printf "%s: %s\n" $"INFO" "$1"
color OFF
}
infile="$1"
filename="${infile##*/}"
filepath="${infile%/*}"
fileext="${infile##*.}"
filebase="${filename%.*}"
if [ "$filepath" == "$infile" ]; then
filepath="."
fi
echo -n "Checking file "; color YELLOW; echo "$infile"; color OFF
for tryext in $IMGEXT; do
if [ -e "$filepath/$filebase.$tryext" ]; then
echo -n "An external cover file already exists ("; color GREEN; echo -n "$filepath/$filebase.$tryext"; color OFF; echo "). Skipping cover generation."
exit 0
fi
done
filelist=""
if [ "$fileext" == "cbr" ]; then
filelist=$($UNRAR vb "$infile" | LC_ALL=C sort)
else
if [ "$fileext" == "cbz" ]; then
filelist=$($UNZIP -l "$infile" | cut -c 31- | head -n -2 | tail -n +4 | LC_ALL=C sort)
else
error "Unknown extension $fileext"
exit 1
fi
fi
oldIFS=$IFS
IFS=$'\n'
cover_found=0
for file in $filelist; do
IFS=$oldIFS
echo -n "Checking file "; color YELLOW; echo -n "$file"; color OFF; echo -n "..."
filetryext="${file##*.}"
is_img=0
for tryext in $IMGEXT; do
if [ "$tryext" == "$filetryext" ]; then
is_img=1
fi
done
if [ "$is_img" -gt 0 ]; then
color GREEN; echo " Cover found!"; color OFF
cover_found=1
if [ "$fileext" == "cbr" ]; then
$UNRAR e -y "$infile" "$file" "$TMPDIR"
else
unzipoutfile=$(echo "$file" | sed -e s/\\\[/\\\\\\\[/g | sed -e s/\\\]/\\\\\\\]/g)
$UNZIP -o -j -d "$TMPDIR" "$infile" "$unzipoutfile"
fi
outfile=$TMPDIR/${file##*/}
if [ ! -e "$outfile" ]; then
error "Cover file $outfile not found!"
exit 1
fi
echo -n "Moving file to destination folder ($outfile -> $filepath/$filebase.${file##*.})..."
if mv -f "$outfile" "$filepath/$filebase.${file##*.}" > /dev/null 2>&1; then
color GREEN; echo " Done!"; color OFF
exit 0
fi
color RED; echo " Failed!"; color OFF
exit 1
else
echo " Skipping"
fi
done
IFS=$oldIFS
if [ "$cover_found" -eq 0 ]; then
error "Can't extract cover from file $infile"
exit 1
fi