Skip to content

Instantly share code, notes, and snippets.

@linktohack
Created November 10, 2021 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linktohack/3ed49bf7e7dba8078b3110c9cd4f2fa5 to your computer and use it in GitHub Desktop.
Save linktohack/3ed49bf7e7dba8078b3110c9cd4f2fa5 to your computer and use it in GitHub Desktop.
KOBO Modifies CBR and CBZ files to be displayed correctly on Kobo devices
#!/bin/bash
# kobocomic: Modifies CBR and CBZ files to be displayed correctly on Kobo devices
# Requires perl, rar, unrar, zip, unzip
if [ "$#" -lt 1 ]; then
echo Supply a CBZ, CBR, ZIP, or RAR file to modify.
exit
fi
if [ -d "tmp_kobocomic" ]; then
echo Directory tmp_kobocomic exists
exit
fi
for z in "$@"
do
mkdir tmp_kobocomic
if [ ${z: -4} == ".cbz" ] || [ ${z: -4} == ".zip" ]; then
filetype="zip"
elif [ ${z: -4} == ".cbr" ] || [ ${z: -4} == ".rar" ]; then
filetype="rar"
else
continue
fi
if [ $filetype == "rar" ]; then
echo Unraring: $z
unrar x "$z" tmp_kobocomic >/dev/null
else
echo Unzipping: $z
unzip -d tmp_kobocomic "$z" >/dev/null
fi
cd tmp_kobocomic
# Add leading zeros to any single or double digits in files, recursively
# perl is needed for lookahead/behind
echo \ \ Padding numbers with leading zeros
find . -depth | while read i; do
mv -n "$i" "$(dirname "$i")/$(basename "$i" | perl -ne 's/(?<!\d)(\d)(?!\d)/0$1/g; s/(?<!\d)(\d\d)(?!\d)/0$1/g; print;')" 2>/dev/null
done
# Append directory name to all files in subdirectories
echo \ \ Appending directory name to all files
find . -mindepth 2 -type f | while read i; do
dir=$(dirname "$i")
mv "$i" "$dir/$(basename "$dir")_$(basename "$i")"
done
if [ $filetype == "rar" ]; then
echo \ \ Compressing: ${z:0:${#z}-4}_new.cbr
rar a "${z:0:${#z}-4}_new.cbr" * >/dev/null
mv "${z:0:${#z}-4}_new.cbr" ..
else
echo \ \ Compressing: ${z:0:${#z}-4}_new.cbz
zip -r "${z:0:${#z}-4}_new.cbz" * >/dev/null
mv "${z:0:${#z}-4}_new.cbz" ..
fi
cd ..
rm -r tmp_kobocomic
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment