-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheb
executable file
·87 lines (77 loc) · 3.53 KB
/
eb
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
#!/usr/bin/env bash
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
see() ( { set -x; } 2>/dev/null ; "$@" )
dieifnotinpath() { command -v "$1" >/dev/null || die "$1 not in PATH"; }
dieifnotinpath 7z
optq=0
mkdir_or_die() { # if already exists, do nothing (existing contents not disturbed)
[[ -d "$1" ]] || mkdir "$1" || die "could not mkdir $1"
[[ -d "$1" ]] || die "mkdir $1 failed"
}
zfdin="./_consumed" ; mkdir_or_die "$zfdin" # when making a bulk run, it's handy to be able to verify outcome by comparing the # of
excl_nonebook_files() (
echo "eb before" ; find . -type f -print | sed 's/^/ /'
xdnm="interesting_files"
mkdir_or_die "$xdnm"
rm -rf "$xdnm:?"/* # in case dir already existed, nuke its content
# rmv typical "advertising" files embedded in www downloads; extensions that never contain content we want to keep
find . -type f \( \
-iname "SHA1SUMS" -or \
-iname "*.docx" -or \
-iname "*.fb2" -or \
-iname "*.html" -or \
-iname "*.htmlz" -or \
-iname "*.jpeg" -or \
-iname "*.jpg" -or \
-iname "*.lit" -or \
-iname "*.lrf" -or \
-iname "*.opf" -or \
-iname "*.pdb" -or \
-iname "*.png" -or \
-iname "*.rtf" -or \
-iname "*.txt" -or \
-iname "*.url" -or \
-iname "*.DS_Store" \) -delete
find . -type f ! \( \
-iname "*.epub" -or \
-iname "*.azw" -or \
-iname "*.azw[34]" -or \
-iname "*.mobi" -or \
-iname "*.pdf" -or \
-iname "*.zip" \) -exec mv {} "$xdnm/" \; # *.zip: "accompanying code" files
rmdir "$xdnm" || die "$xdnm is not empty!" # if nothing left, rmdir will succeed
)
do1file() (
((optq!=0)) || echo "arg=$1'"
[[ -f "$1" ]] || { ((optq!=0)) || die "$1 is not a file" ; die ; }
sanspath="${1##*/}" # ; echo "sanspath=$sanspath'" # https://stackoverflow.com/a/965069
sansext="${sanspath%.*}" # ; echo "sansext=$sansext'"
[[ "${sanspath,,}" == *[_.]code.zip ]] && die "skipping code-zip file $sanspath"
[[ -d "$sansext" ]] && die "dir $sansext already exists"
7z e -o"$sansext" "$1" || die "7z extraction failed"
(cd "$sansext" && find . -type d -empty -delete) # `7z e ...` can create empty dirs in output dir: delete them
# above is common to various scripts: unzips $1 into a dir of its own name (minus extension) ($sansext)
# purpose: given a typical ebook archive file (zip, rar) extracted into $sansext, delete non-ebook files and rename ebook files $sansext.ext
( cd "$sansext" && excl_nonebook_files ) || die "excl_nonebook_files failed"
( cd "$sansext" && mv_calibre_junk ; rm -rf _calibre_junk )
# some downloaded archives' files have timestamps newer than "now": touch them
( cd "$sansext" && find . -type f -newer "../$1" -printf 'from the future: %p\n' )
( cd "$sansext" && find . -type f -newer "../$1" -exec touch {} + )
# move the files that're left back to cwd
( cd "$sansext" && find . -type f | while read -r file; do mv "$file" "../$sansext.${file##*.}" ; done ) # https://stackoverflow.com/a/8489394
mv "$1" "$zfdin/" || die "mv $1 $zfdin/ FAILED" # move input file (which might be a zip file) into "done" dir
rmdir "$sansext" || die "mv did not empty $sansext/"
echo "------------------------------- eb successful -------------------------------"
)
argerr() { die "usage: $0 [-q] FILE..." ; }
while getopts "hq" opt; do
case $opt in
q) optq=1 ;;
h) argerr ;;
\?) argerr ;;
esac
done
shift "$((OPTIND-1))" # shift so that $@, $1, etc. refer to the non-option arguments
for fnm in "$@"; do
do1file "$fnm"
done