scripts/cbz2ebook.sh
Simon Bruder f94b0ab07e
cbz2ebook: Remove dependency on my zshrc
This script could now easily be ported to bash (or even sh), but we are
using nix-shell anyway, so why not use zsh.
2020-09-02 13:05:41 +02:00

46 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env nix-shell
#!nix-shell -i zsh -p imagemagick unzip zip zsh
set -e
size="1440x1920" # Kobo Forma
#size="768x1024" # Amazon Kindle Paperwhite
vertical_size="$(cut -dx -f2 <<< $size)x$(cut -dx -f1 <<< $size)"
infile="$(realpath $1)"
outfile="$(realpath $2)"
tmpdir=$(mktemp -d)
function cleanup {
rm -rf "$tmpdir"
}
trap cleanup EXIT INT SIGTERM
cd "$tmpdir"
unzip "$infile"
# rename files to have a plain 4 digit filename
i=1
for file in **/*.???; do
mv -n "$file" "$(dirname $file)/$(printf %04d $i).${file##*.}"
i=$((i+1))
done
length=$(ls -1 **/*.??? | wc -l)
position=0
for image in **/*.???; do
width=$(identify -format "%W" "$image")
height=$(identify -format "%H" "$image")
if (($width > $height)); then
mogrify -resize "$vertical_size" -rotate 270 "$image"
else
mogrify -resize "$size" "$image"
fi
position=$(($position + 1))
echo -ne "$(printf '%3s' $((100 * $position / $length))) % "$(printf "%0*d" "$((72 * $position / $length))" 0 | tr '0' '#')'\r'
done
echo
zip "$outfile" **/*.???
cd -