Skip to content

Instantly share code, notes, and snippets.

@abathur
Created February 5, 2022 23:14
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 abathur/8cc26608febfba6159deda1562823dde to your computer and use it in GitHub Desktop.
Save abathur/8cc26608febfba6159deda1562823dde to your computer and use it in GitHub Desktop.
#!/nix/store/z6rd8wq02azalrlm2m5k08iy53klg624-bash-5.1-p12/bin/sh
# (c) Christoph Sieghart <sigi@0x2a.at> 2007 - 2011
VERSION=0.9.2
# Flags for commandline options
S_FLAG=0
C_FLAG=0
trap 'if [ $S_FLAG -eq 1 ]; then clean_temp_dir; clean_cursor; fi' 0 1 2
#*****************************************************************************
# - Functions
#*****************************************************************************
check_print_error() {
if [ $? -ne 0 ]
then
echo "Error: $0: $1"
exit 1
fi
}
print_version() {
echo "djvu2pdf $VERSION - Christoph Sieghart <sigi@0x2a.at>"
}
print_help() {
print_version
echo -e "\n Usage: ./djvu2pdf filename.djvu [filename.djvu] ...\n"
echo " -h Prints this help"
echo " -v Prints the version number"
echo " -s Show status messages (A little bit slower - every page"
echo " gets dumped on its own and later recombined"
echo -e " -c Don't use terminal escape sequences to move cursor \n"
exit 0
}
print_quiet() {
if [ $S_FLAG -eq 1 ]; then
echo -en $1
fi
}
move_cursor() {
if [ $S_FLAG -eq 1 ]; then
if [ $C_FLAG -eq 1 ]; then
echo -ne "\n"
return
else
# cursor magic
echo -en "\033[35D"
fi
fi
}
clean_cursor() {
# if the cursor 'magic' messes anything up
if [ $S_FLAG -eq 1 ]; then
if [ $C_FLAG -eq 0 ]; then
/nix/store/5dmd4cq2mwhd0d7kmppjk7ql00ygfkh3-ncurses-6.3/bin/tput sgr0
fi
fi
}
clean_temp_files() {
/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -rf "$TEMP"/* >/dev/null 2>&1
}
clean_temp_dir() {
/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm -rf "$TEMP" >/dev/null 2>&1
}
#*****************************************************************************
# - Programm checks
#*****************************************************************************
# MacOS and BSD compatibility
/nix/store/mhnx1fhz1iaznn1b0snxyr2qp9y2wcsa-which-2.21/bin/which seq >/dev/null 2>&1
if [ $? -eq 0 ]; then
SEQ=seq
else
SEQ=jot
fi
for i in "ddjvu djvulibre" "gs ghostscript"; do
BINARY=$(echo $i | /nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{print $1'})
PACKAGE=$(echo $i | /nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{print $2'})
/nix/store/mhnx1fhz1iaznn1b0snxyr2qp9y2wcsa-which-2.21/bin/which $BINARY >/dev/null 2>&1
check_print_error "$BINARY not found. Install $PACKAGE."
done
#*****************************************************************************
# - Commandline options
#*****************************************************************************
while getopts hvcs opt
do
case "$opt" in
v) print_version
exit 0;;
h) print_help;;
c) C_FLAG=1;;
s) S_FLAG=1;;
\?) exit 1;;
esac
done
shift `/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/expr $OPTIND - 1`
if [ $C_FLAG -eq 1 ]; then
if [ $S_FLAG -eq 0 ]; then
echo -e "Error: $0: The option -c only makes sense with -s"
exit 1
fi
fi
if [ -z "$1" ]; then
echo -e "Error: $0: No file specified for conversion"
exit 1
fi
#*****************************************************************************
# - Main loop
#*****************************************************************************
while [ $# -gt 0 ]; do
if [ ! -f "$1" ]; then
echo -e "Error: $0: File '$1' not found"
exit 1
fi
FILENAME=$1
FILEBASE=`/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/basename "$FILENAME" .djvu`
OUTPUTFILE="${FILEBASE}.pdf"
if [ $S_FLAG -eq 0 ]; then
/nix/store/495blbmi282jp20pijgls7iszi0jrlqp-djvulibre-3.5.28-bin/bin/ddjvu -format=pdf "${FILENAME}" "${OUTPUTFILE}" 2> /dev/null
else
#*****************************************************************************
# - Temporary Files
#*****************************************************************************
TEMP="${TMPDIR:=/tmp}/djvu2pdf.$$"
/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir $TEMP
check_print_error "Creating temporary directory failed!"
# Child processes should use our temporary directory
TMPDIR=$TEMP
PAGES=`/nix/store/495blbmi282jp20pijgls7iszi0jrlqp-djvulibre-3.5.28-bin/bin/djvudump "$FILENAME" | /nix/store/pkw9ckmar4dw7sgsszg8zgydfq4xc11k-gnugrep-3.7/bin/grep pages | /nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk '{print $8;}'`
ZEROCOUNT=$(/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/expr `echo $PAGES | /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/wc -m` - 1)
COUNT=1
TENS=1
print_quiet "Converting $FILENAME to $OUTPUTFILE\n"
#
# We dump every page and print the status message
#
for COUNT in `/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/seq 1 $PAGES`;
do
if [ `/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/expr $COUNT / $TENS` -gt 0 ]
then
TENS=`/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/expr $TENS \* 10`
ZEROCOUNT=`/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/expr $ZEROCOUNT - 1`
ZEROS=$(for i in `/nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/seq 1 $ZEROCOUNT`; do echo -n 0; done)
fi
/nix/store/495blbmi282jp20pijgls7iszi0jrlqp-djvulibre-3.5.28-bin/bin/ddjvu -format=pdf -page $COUNT "$FILENAME" "$TEMP/$FILEBASE.${ZEROS}$COUNT.pdf" 2> /dev/null
print_quiet "Page $COUNT/$PAGES dumped"
move_cursor
done
clean_cursor
#
# The pages get combined into one big happy .pdf
#
print_quiet "\nDumping finished - writing $OUTPUTFILE\n"
/nix/store/w0k1ip6cizl1a8lf6sxvynf504xzdjw3-ghostscript-9.55.0/bin/gs -dSAFER -dQUIET -dBATCH -sDEVICE=pdfwrite -sOutputFile="$OUTPUTFILE" -DNOPAUSE "$TEMP/$FILEBASE".*.pdf
check_print_error "Error in creating pdf file"
clean_temp_files
fi
shift
done
exit 0
### resholve directives (auto-generated) ## format_version: 2
# resholve: fix $SEQ:seq
# resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/basename
# resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/expr
# resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/mkdir
# resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/rm
# resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/seq
# resholve: keep /nix/store/2hfc8dk1g3bsms4l4xf620x7ynslvq3k-coreutils-9.0/bin/wc
# resholve: keep /nix/store/495blbmi282jp20pijgls7iszi0jrlqp-djvulibre-3.5.28-bin/bin/ddjvu
# resholve: keep /nix/store/495blbmi282jp20pijgls7iszi0jrlqp-djvulibre-3.5.28-bin/bin/djvudump
# resholve: keep /nix/store/5dmd4cq2mwhd0d7kmppjk7ql00ygfkh3-ncurses-6.3/bin/tput
# resholve: keep /nix/store/mhnx1fhz1iaznn1b0snxyr2qp9y2wcsa-which-2.21/bin/which
# resholve: keep /nix/store/pkw9ckmar4dw7sgsszg8zgydfq4xc11k-gnugrep-3.7/bin/grep
# resholve: keep /nix/store/vr8p2is1j4n68gwr1hj4r061pf6462ng-gawk-5.1.1/bin/awk
# resholve: keep /nix/store/w0k1ip6cizl1a8lf6sxvynf504xzdjw3-ghostscript-9.55.0/bin/gs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment