|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
| |
|
|
| QUALITY=75 |
| OUTPUT=output |
| |
|
|
| function title () { echo -e "\033[0;1m$*\033[0;0m"; } |
| function pass () { echo -e "\033[1;32m$*\033[0;0m"; } |
| function warn () { echo -e "\033[1;33m$*\033[0;0m"; } |
| function fail () { echo -e "\033[1;31m$*\033[0;0m"; } |
| function debug () { [ "$DEBUG" = yes ] && echo -e "\033[1;34m$*\033[0;0m"; } |
| |
|
|
| echo $OUTPUT | grep -q '^[~/]' || OUTPUT="$(pwd)/$OUTPUT" |
| debug "dirout: $OUTPUT" |
| |
| for file; do |
| title "Processing: $file" |
| |
| |
| debug "input: $file" |
| echo $file | grep -q '^[~/]' || file="$(pwd)/$file" |
| [ -f "$file" ] || { warn "usage: $(basename $0) <file>"; exit 1; } |
| debug "file: $file" |
| |
| |
| case $(file "$file") in |
| *"7-zip archive data"*) type="7Z";; |
| *"PDF document"*) type="PDF";; |
| *"RAR archive data"*) type="RAR";; |
| *"POSIX tar archive"*) type="TAR";; |
| *"Zip archive data"*) type="ZIP";; |
| *) fail "unkown archive type"; exit 1;; |
| esac |
| debug "type: $type" |
| |
| |
| tmp=$(mktemp -d) |
| [ -d "$tmp" ] || { fail "unable to create temporary directory"; exit 1; } |
| debug "tmp: $tmp" |
| |
| |
| case $type in |
| 7Z) (cd $tmp; 7z x "$file");; |
| PDF) (cd $tmp; pdfimages -j "$file" page >/dev/null);; |
| RAR) (cd $tmp; unrar -inul x "$file");; |
| TAR) (cd $tmp; tar xf "$file");; |
| ZIP) (cd $tmp; unzip -q "$file");; |
| esac |
| [ -d "$tmp/$(ls $tmp)" ] && [ ! x"$(ls $tmp)" = xbook ] && mv "$tmp/$(ls $tmp)" $tmp/book |
| chmod u+rw -R $tmp |
| list=$(cd $tmp; find -type f | sort -n | awk '{sub(/\.\//, ""); m=m" "$0} END{print m}') |
| debug "archive:$list" |
| |
| |
| before=$(find $tmp -type f -print0 | xargs -0 wc -c | awk '$2=="total" {print $1}') |
| |
| [ -z "$(find $tmp -iname '*.jpg' -o -iname '*.jpeg')" ] || \ |
| { find $tmp -iname '*.jpg' -o -iname '*.jpeg' -print0 | xargs -0 jpegoptim -m$QUALITY -p -q -s; } |
| [ -z "$(find $tmp -iname '*.png')" ] || \ |
| { find $tmp -iname '*.png' -print0 | xargs -0 optipng -o2 -quiet -preserve; } |
| after=$(find $tmp -type f -print0 | xargs -0 wc -c | awk '$2=="total" {print $1}') |
| debug "compression: $after/$before $(echo $after*100/$before | bc)%" |
| |
| |
| [ $after -gt $before ] && warn "useless compressing $(echo $after*100/$before | bc)%" |
| |
| |
| archive=$(basename "$file"| sed 's/\.[^.]*$/.cbz/') |
| [ -d "$OUTPUT" ] || mkdir "$OUTPUT" |
| debug "output: $OUTPUT/$archive" |
| [ -f "$OUTPUT/$archive" ] && rm "$OUTPUT/$archive" |
| (cd $tmp; find . -type f -print | zip -9 --quiet "$OUTPUT/$archive" -@) |
| size=$(ls -sh "$OUTPUT/$archive" | awk '{print $1}') |
| debug "result: $size" |
| |
| |
| rm -rf $tmp |
| |
| pass "Produced: $archive $size" |
| done |