]> zdv2.bktei.com Git - BK-2020-03.git/blob - user/cbz_dedup.sh
fix(user/bkdatev):Make tzdata-legacy 'right/UTC' TZ optional
[BK-2020-03.git] / user / cbz_dedup.sh
1 #!/bin/bash
2 # Desc: Deduplicates files within a CBZ file
3 # Usage: cbz_dedup.sh [CBZ file]
4 # Example: cbz_dedup.sh input.cbz
5 # Version: 0.0.5
6 # Depends: jdupes 1.27.3, unzip 6.00 by Debian
7
8
9 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
10 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
11 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
12 showUsage() {
13 # Desc: Display script usage information
14 # Usage: showUsage
15 # Version 0.0.2
16 # Input: none
17 # Output: stdout
18 # Depends: GNU-coreutils 8.30 (cat)
19 cat <<'EOF'
20 USAGE:
21 cbz_dedup.sh [FILE]
22
23 EXAMPLE:
24 cbz_dedup.sh input.cbz
25 EOF
26 }; # Display information on how to use this script.
27 main() {
28 fout="output.cbz";
29
30 # Check args
31 if [[ $# -ne 1 ]]; then showUsage; die "FATAL:Invalid arg count (should be 1):$#"; fi;
32 re1='\.cbz$';
33 if [[ ! "$1" =~ $re1 ]]; then showUsage; die "FATAL:Not a .cbz file:$1"; fi;
34 if [[ ! -f "$1" ]]; then die "FATAL:Not a file:$1"; fi;
35 fin="$1";
36 dir_tmp="$(mktemp -d)";
37 if [[ ! -d "$dir_tmp" ]]; then die "FATAL:Could not make temporary directory:${dir_tmp}"; fi;
38 trap 'rm -rf "${dir_tmp}"' EXIT;
39 pout="${dir_tmp}/${fout}";
40
41 # Check depends
42 for _cmd in unzip zip jdupes; do
43 if ! command -v "$_cmd" 1>/dev/random 2>&1; then
44 die "FATAL:Missing app:${_cmd}";
45 fi;
46 done;
47
48 # Extract CBZ to temp dir
49 must unzip "$fin" -x / -d "$dir_tmp";
50
51 # Dedupe with jdupes
52 ## Check size before
53 tmp_size1="$(du -bd0 "$dir_tmp" | cut -f1; )";
54 must jdupes -dN "${dir_tmp}";
55 ## Check size after
56 tmp_size2="$(du -bd0 "$dir_tmp" | cut -f1; )";
57
58 # Replace CBZ if size changed
59 if [[ "$tmp_size1" -eq "$tmp_size2" ]]; then yell "STATUS:No deduplication detected."; return 0; fi;
60
61 ## Recreate CBZ
62 must zip -j "$pout" "${dir_tmp}"/*;
63
64 ## Preserve original CBZ
65 must mv -n "$fin" "${fin%.*}_original.cbz";
66
67 ## Move deduped CBZ
68 must mv -n "$pout" "$fin";
69
70 return 0;
71 };
72
73 main "$@";