forked from cfgnunes/nautilus-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-functions
More file actions
285 lines (252 loc) · 7.7 KB
/
common-functions
File metadata and controls
285 lines (252 loc) · 7.7 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
IFS=$'\n'
ERROR_LOG_FILE="errors.log"
_msg_error() {
MESSAGE="$1"
# Check if the script is running in a terminal
if env | grep -q '^TERM'; then
echo >&2 "$MESSAGE"
else # It not use gui notifications
if hash notify-send 2>/dev/null; then
notify-send -i error "$MESSAGE"
elif hash zenity 2>/dev/null; then
zenity --error --width=300 --text "$MESSAGE"
elif hash kdialog 2>/dev/null; then
kdialog --title "$MESSAGE" --passivepopup "This popup will disappear in 5 seconds" 5 &
elif hash xmessage 2>/dev/null; then
xmessage "$MESSAGE" -timeout 5
fi
fi
}
_msg_info() {
MESSAGE="$1"
# Check if the script is running in a terminal
if env | grep -q '^TERM'; then
echo "$MESSAGE"
else # It not use gui notifications
if hash notify-send 2>/dev/null; then
notify-send "$MESSAGE"
elif hash zenity 2>/dev/null; then
zenity --info --width=300 --text "$MESSAGE"
elif hash kdialog 2>/dev/null; then
kdialog --title "$MESSAGE" --passivepopup "This popup will disappear in 5 seconds" 5 &
elif hash xmessage 2>/dev/null; then
xmessage "$MESSAGE" -timeout 5
fi
fi
}
_check_dependency() {
LIST_COMMANDS="$1"
for COMMAND in $LIST_COMMANDS; do
if hash "$COMMAND" 2>/dev/null; then
continue
fi
PACKAGE_NAME=""
case "$COMMAND" in
"make")
PACKAGE_NAME="make"
;;
"convert")
PACKAGE_NAME="imagemagick"
;;
"gzip")
PACKAGE_NAME="gzip"
;;
"tar")
PACKAGE_NAME="tar"
;;
"file-roller")
PACKAGE_NAME="file-roller"
;;
"file")
PACKAGE_NAME="file"
;;
"gs")
PACKAGE_NAME="ghostscript"
;;
"inkscape")
PACKAGE_NAME="inkscape"
;;
"jpegoptim")
PACKAGE_NAME="jpegoptim"
;;
"latexmk")
PACKAGE_NAME="latexmk"
;;
"mediainfo")
PACKAGE_NAME="mediainfo"
;;
"meld")
PACKAGE_NAME="meld"
;;
"7z")
PACKAGE_NAME="p7zip-full"
;;
"qpdf")
PACKAGE_NAME="qpdf"
;;
"zenity")
PACKAGE_NAME="zenity"
;;
"zip")
PACKAGE_NAME="zip"
;;
"iconv")
PACKAGE_NAME="libc-bin"
;;
"pandoc")
PACKAGE_NAME="pandoc"
;;
"pdfimages" | "pdfseparate" | "pdftoppm" | "pdftotext" | "pdfunite")
PACKAGE_NAME="poppler-utils"
;;
*)
PACKAGE_NAME=""
;;
esac
if [ -n "$PACKAGE_NAME" ]; then
_msg_error "Error: You need to install the '$PACKAGE_NAME' package."
else
_msg_error "Error: The command '$COMMAND' was not found. Please, install it."
fi
exit 1
done
}
_get_filename_without_extension() {
FILENAME="$1"
# shellcheck disable=SC2001
echo "$FILENAME" | sed "s|\.[^.]*$||"
}
_get_filename_extension() {
FILENAME="$1"
# shellcheck disable=SC2001
echo "$FILENAME" | sed "s|[^.]*\.|.|" | grep "\."
}
_log_error() {
FILENAME="$1"
MESSAGE="$2"
OUTPUT_DIR="$3"
{
echo "$(date "+%Y-%m-%d %H:%M:%S:") Error processing '$FILENAME'."
echo "$MESSAGE"
echo ""
} >>"$OUTPUT_DIR/$ERROR_LOG_FILE"
}
_display_result() {
OUTPUT_DIR="$1"
OUTPUT_DIR_PRINT="$OUTPUT_DIR"
echo "$OUTPUT_DIR_PRINT" | grep -q "$PWD" && OUTPUT_DIR_PRINT="./${OUTPUT_DIR#$PWD/}"
# shellcheck disable=SC2088
echo "$OUTPUT_DIR_PRINT" | grep -q "$HOME" && OUTPUT_DIR_PRINT="~/${OUTPUT_DIR#$HOME/}"
# Check if there was some error
if [ -f "$OUTPUT_DIR/$ERROR_LOG_FILE" ]; then
_msg_error "Task finished with errors! See the '$OUTPUT_DIR_PRINT/$ERROR_LOG_FILE' for details."
exit 1
fi
# If OUTPUT_DIR parameter is defined
if [ -n "$OUTPUT_DIR" ]; then
# Try remove output dir if it is empty
rmdir "$OUTPUT_DIR" &>/dev/null
# Check if output directory still exists
if [ ! -d "$OUTPUT_DIR" ]; then
_msg_info "Task finished, but there is no output files!"
exit 0
fi
_msg_info "Task finished! The output files is in '$OUTPUT_DIR_PRINT' directory."
else
_msg_info "Task finished!"
fi
}
_get_output_dir() {
DIR_NAME="output"
DIR_BASE="$PWD"
DIR_SUFFIX=1
[[ ! -w "$DIR_BASE" ]] && DIR_BASE="$HOME"
[[ ! -w "$DIR_BASE" ]] && DIR_BASE="/tmp"
OUTPUT_DIR="$DIR_BASE/$DIR_NAME"
if [[ ! -w "$DIR_BASE" ]]; then
_msg_error "Error: It was not possible to find directory with write permission."
exit 1
fi
while [ -d "$OUTPUT_DIR" ]; do
DIR_SUFFIX=$((DIR_SUFFIX + 1))
OUTPUT_DIR="$DIR_BASE/$DIR_NAME$DIR_SUFFIX"
done
mkdir "$OUTPUT_DIR"
echo "$OUTPUT_DIR"
}
_get_files() {
INPUT_FILES="$1" # Input list
MIME_LIST="$2" # Filter bt MIME type
MIN_FILES="$3" # To check the minimum number of valid files
MAX_FILES="$4" # To check the maximum number of valid files
DIRECTORY_FLAG="$5"
# DIRECTORY_FLAG values:
# 'd': Include directories.
# 'r': Expand directories recursively (include only files).
# 'R': Expand directories recursively (include files + subdirectories).
# other_value: Ignore directories.
# Expand files in directories recursively
if [ "$DIRECTORY_FLAG" = "r" ] || [ "$DIRECTORY_FLAG" = "R" ]; then
INPUT_FILES_NEW=""
for FILE in $INPUT_FILES; do
if [ "$DIRECTORY_FLAG" = "r" ]; then
INPUT_FILES_NEW+="$(find -L "$FILE" -type f)"
else
INPUT_FILES_NEW+="$(find -L "$FILE")"
fi
INPUT_FILES_NEW+=$'\n'
done
INPUT_FILES="$INPUT_FILES_NEW"
fi
OUTPUT_FILES=""
VALID_FILES_COUNT=0
for FILE in $INPUT_FILES; do
VALID_FILE=0
# Filter files by mime type
if [ -n "$MIME_LIST" ]; then
MIME_FILE=$(file --mime-type -b "$FILE")
IFS_OLD=$IFS
IFS=";"
for MIME in $MIME_LIST; do
[[ "$MIME_FILE" == *"$MIME"* ]] && VALID_FILE=1
done
IFS=$IFS_OLD
else
VALID_FILE=1
fi
# Check the DIRECTORY_FLAG
if [ -d "$FILE" ]; then
[ -n "$DIRECTORY_FLAG" ] && VALID_FILE=1
fi
# Add the file in the list if it is valid
if ((VALID_FILE == 1)); then
VALID_FILES_COUNT=$((VALID_FILES_COUNT + 1))
OUTPUT_FILES+="$FILE"
OUTPUT_FILES+=$'\n'
fi
done
# Check if there is at last one file with valid mime type
if ((VALID_FILES_COUNT == 0)); then
if [ -n "$MIME_LIST" ]; then
_msg_error "Error: There is no '$MIME_LIST' in the selected files!"
else
_msg_error "Error: There is no valid itens in the in the selected files!"
fi
return 1
fi
if [ -n "$MIN_FILES" ]; then
if ((VALID_FILES_COUNT < MIN_FILES)); then
_msg_error "Error: You selected $VALID_FILES_COUNT files, but the minium is $MIN_FILES!"
return 1
fi
fi
if [ -n "$MAX_FILES" ]; then
if ((VALID_FILES_COUNT > MAX_FILES)); then
_msg_error "Error: You selected $VALID_FILES_COUNT files, but the maximum is $MAX_FILES!"
return 1
fi
fi
echo "$OUTPUT_FILES" | sed '/^$/d' | sort
return 0
}