#!/bin/sh
# (c) Christoph Sieghart <sigi@0x2a.at> 2007 - 2011
# Source: https://0x2a.at/site/projects/djvu2pdf/

# djvu2pdf

# Copyright:  2007-2009 Christoph Sieghart <sigi@0x2a.at>

# The home page of djvu2pdf is at:
# http://www.0x2a.at/s/projects/djvu2pdf

# The entire code base may be distributed under the terms of the GNU General
# Public License (GPL), which appears immediately below.  Alternatively, all
# of the source code as any code derived from that code may instead be
# distributed under the GNU Lesser General Public License (LGPL), at the
# choice of the distributor. The complete text of the LGPL appears at the
# bottom of this file.

# See /usr/share/common-licenses/(GPL|LGPL)

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
            tput sgr0
        fi
    fi
}

clean_temp_files() {
    rm -rf "$TEMP"/* >/dev/null 2>&1
}

clean_temp_dir() {
    rm -rf "$TEMP" >/dev/null 2>&1
}

#*****************************************************************************
# - Programm checks
#*****************************************************************************

# MacOS and BSD compatibility
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 | awk '{print $1'})
    PACKAGE=$(echo $i | awk '{print $2'})
    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 `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=`basename "$FILENAME" .djvu`
    OUTPUTFILE="${FILEBASE}.pdf"

    if [ $S_FLAG -eq 0 ]; then
        ddjvu -format=pdf "${FILENAME}" "${OUTPUTFILE}" 2> /dev/null
    else

        #*****************************************************************************
        # - Temporary Files
        #*****************************************************************************

        TEMP="${TMPDIR:=/tmp}/djvu2pdf.$$"
        mkdir $TEMP
        check_print_error "Creating temporary directory failed!"

        # Child processes should use our temporary directory
        TMPDIR=$TEMP

        PAGES=`djvudump "$FILENAME" | grep pages | awk '{print $8;}'`
        ZEROCOUNT=$(expr `echo $PAGES | 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 `$SEQ 1 $PAGES`;
        do
            if [ `expr $COUNT / $TENS` -gt 0 ]
            then
                TENS=`expr $TENS \* 10`
                ZEROCOUNT=`expr $ZEROCOUNT - 1`
                ZEROS=$(for i in `$SEQ 1 $ZEROCOUNT`; do echo -n 0; done)
            fi

            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"

        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
