# calibre Bash Shell Completion
_calibre()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--version -h --help --redirect-console-output --with-library --start-in-tray -v --verbose --no-update-check --ignore-plugins -s --shutdown-running-calibre"
    pics="@(jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP)"

    case "${prev}" in
      --cover )
           _filedir "${pics}"
           return 0
           ;;
    esac

    case "${cur}" in
      --cover )
         _filedir "${pics}"
         return 0
         ;;
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         return 0
         ;;
      *  )
        _filedir '@(lrf|rar|zip|rtf|lit|txt|txtz|text|htm|xhtm|html|htmlz|xhtml|pdf|pdb|updb|pdr|prc|mobi|azw|doc|epub|fb2|djv|djvu|lrx|cbr|cbz|cbc|oebzip|rb|imp|odt|chm|tpz|azw1|pml|pmlz|mbp|tan|snb|xps|oxps|azw4|book|zbf|pobi|docx|md|textile|markdown|ibook|iba|azw3|LRF|RAR|ZIP|RTF|LIT|TXT|TXTZ|TEXT|HTM|XHTM|HTML|HTMLZ|XHTML|PDF|PDB|UPDB|PDR|PRC|MOBI|AZW|DOC|EPUB|FB2|DJV|DJVU|LRX|CBR|CBZ|CBC|OEBZIP|RB|IMP|ODT|CHM|TPZ|AZW1|PML|PMLZ|MBP|TAN|SNB|XPS|OXPS|AZW4|BOOK|ZBF|POBI|DOCX|MD|TEXTILE|MARKDOWN|IBOOK|IBA|AZW3)'
        return 0
        ;;
    esac

}
complete -o filenames -F _calibre calibre

_lrf2lrs()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--version -h --help -o --output --dont-output-resources --verbose"
    pics="@(jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP)"

    case "${prev}" in
      --cover )
           _filedir "${pics}"
           return 0
           ;;
    esac

    case "${cur}" in
      --cover )
         _filedir "${pics}"
         return 0
         ;;
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         return 0
         ;;
      *  )
        _filedir '@(lrf|LRF)'
        return 0
        ;;
    esac

}
complete -o filenames -F _lrf2lrs lrf2lrs

_ebook_meta()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--version -h --help -t --title -a --authors --title-sort --author-sort --cover -c --comments -p --publisher --category -s --series -i --index -r --rating --isbn --tags -k --book-producer -l --language -d --date --get-cover --to-opf --from-opf --lrf-bookid"
    pics="@(jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP)"

    case "${prev}" in
      --cover )
           _filedir "${pics}"
           return 0
           ;;
    esac

    case "${cur}" in
      --cover )
         _filedir "${pics}"
         return 0
         ;;
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         return 0
         ;;
      *  )
        _filedir '@(rtf|prc|fb2|odt|cbr|pml|rar|cbz|snb|htmlz|txt|updb|zip|oebzip|chm|lit|imp|html|txtz|azw1|azw3|azw4|rb|lrf|tpz|opf|lrx|epub|mobi|pmlz|pobi|pdf|pdb|azw|RTF|PRC|FB2|ODT|CBR|PML|RAR|CBZ|SNB|HTMLZ|TXT|UPDB|ZIP|OEBZIP|CHM|LIT|IMP|HTML|TXTZ|AZW1|AZW3|AZW4|RB|LRF|TPZ|OPF|LRX|EPUB|MOBI|PMLZ|POBI|PDF|PDB|AZW)'
        return 0
        ;;
    esac

}
complete -o filenames -F _ebook_meta ebook-meta

_lrfviewer()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--version -h --help --redirect-console-output --verbose --visual-debug --disable-hyphenation --white-background --profile"
    pics="@(jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP)"

    case "${prev}" in
      --cover )
           _filedir "${pics}"
           return 0
           ;;
    esac

    case "${cur}" in
      --cover )
         _filedir "${pics}"
         return 0
         ;;
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         return 0
         ;;
      *  )
        _filedir '@(lrf|LRF)'
        return 0
        ;;
    esac

}
complete -o filenames -F _lrfviewer lrfviewer

_ebook_viewer()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--version -h --help --raise-window -f --full-screen --fullscreen --debug-javascript --open-at"
    pics="@(jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP)"

    case "${prev}" in
      --cover )
           _filedir "${pics}"
           return 0
           ;;
    esac

    case "${cur}" in
      --cover )
         _filedir "${pics}"
         return 0
         ;;
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         return 0
         ;;
      *  )
        _filedir '@(azw|azw3|azw4|cbc|cbr|cbz|chm|djv|djvu|downloaded_recipe|epub|fb2|htm|html|htmlz|lit|lrf|markdown|md|mobi|odt|opf|pdb|pdf|pml|pmlz|prc|rb|recipe|rtf|shtm|shtml|snb|tcr|text|textile|txt|txtz|updb|xhtm|xhtml|AZW|AZW3|AZW4|CBC|CBR|CBZ|CHM|DJV|DJVU|DOWNLOADED_RECIPE|EPUB|FB2|HTM|HTML|HTMLZ|LIT|LRF|MARKDOWN|MD|MOBI|ODT|OPF|PDB|PDF|PML|PMLZ|PRC|RB|RECIPE|RTF|SHTM|SHTML|SNB|TCR|TEXT|TEXTILE|TXT|TXTZ|UPDB|XHTM|XHTML)'
        return 0
        ;;
    esac

}
complete -o filenames -F _ebook_viewer ebook-viewer

_fetch_ebook_metadata()
{
    local cur opts
    local IFS=$'|\t'
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    opts="--version|-h|--help|-t|--title|-a|--authors|-i|--isbn|-v|--verbose|-o|--opf|-c|--cover|-d|--timeout"
    words=""

    case "${cur}" in
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
         return 0
         ;;
      *  )
         COMPREPLY=( $(compgen -W "${words}" -- ${cur}) )
         COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
         return 0
         ;;
    esac

}
complete -F _fetch_ebook_metadata fetch-ebook-metadata

_calibre_smtp()
{
    local cur opts
    local IFS=$'|\t'
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    opts="--version|-h|--help|-l|--localhost|-o|--outbox|-f|--fork|-t|--timeout|-v|--verbose|-a|--attachment|-s|--subject|-r|--relay|--port|-u|--username|-p|--password|-e|--encryption-method"
    words=""

    case "${cur}" in
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
         return 0
         ;;
      *  )
         COMPREPLY=( $(compgen -W "${words}" -- ${cur}) )
         COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
         return 0
         ;;
    esac

}
complete -F _calibre_smtp calibre-smtp

_calibre_server()
{
    local cur opts
    local IFS=$'|\t'
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    opts="--version|-h|--help|-p|--port|-t|--timeout|--thread-pool|--password|--username|--develop|--max-cover|--max-opds-items|--max-opds-ungrouped-items|--url-prefix|--with-library|--pidfile|--daemonize|--restriction|--auto-reload"
    words=""

    case "${cur}" in
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
         return 0
         ;;
      *  )
         COMPREPLY=( $(compgen -W "${words}" -- ${cur}) )
         COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
         return 0
         ;;
    esac

}
complete -F _calibre_server calibre-server

_epub_fix()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="--version -h --help --unmanifested --delete-unmanifested --epubcheck"
    pics="@(jpg|jpeg|png|gif|bmp|JPG|JPEG|PNG|GIF|BMP)"

    case "${prev}" in
      --cover )
           _filedir "${pics}"
           return 0
           ;;
    esac

    case "${cur}" in
      --cover )
         _filedir "${pics}"
         return 0
         ;;
      -* )
         COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
         return 0
         ;;
      *  )
        _filedir '@(epub|EPUB)'
        return 0
        ;;
    esac

}
complete -o filenames -F _epub_fix epub-fix


_ebook_device_ls()
{
local pattern search listing prefix
pattern="$1"
search="$1"
if [[ -n "{$pattern}" ]]; then
    if [[ "${pattern:(-1)}" == "/" ]]; then
    pattern=""
    else
    pattern="$(basename ${pattern} 2> /dev/null)"
    search="$(dirname ${search} 2> /dev/null)"
    fi
fi

if [[  "x${search}" == "x" || "x${search}" == "x." ]]; then
    search="/"
fi

listing="$(ebook-device ls ${search} 2>/dev/null)"

prefix="${search}"
if [[ "x${prefix:(-1)}" != "x/" ]]; then
    prefix="${prefix}/"
fi

echo $(compgen -P "${prefix}" -W "${listing}" "${pattern}")
}

_ebook_device()
{
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=()
case "${prev}" in
    ls|rm|mkdir|touch|cat )
        COMPREPLY=( $(_ebook_device_ls "${cur}") )
        return 0
        ;;
    cp )
        if [[ ${cur} == prs500:* ]]; then
        COMPREPLY=( $(_ebook_device_ls "${cur:7}") )
        return 0
        else
        _filedir
        return 0
        fi
        ;;
    prs500 )
        COMPREPLY=( $(compgen -W "cp ls rm mkdir touch cat info books df" "${cur}") )
        return 0
        ;;
    * )
        if [[ ${cur} == prs500:* ]]; then
        COMPREPLY=( $(_ebook_device_ls "${cur:7}") )
        return 0
        else
        if [[ ${prev} == prs500:* ]]; then
            _filedir
            return 0
        else
            COMPREPLY=( $(compgen -W "prs500:" "${cur}") )
            return 0
        fi
        return 0
        fi
    ;;
esac
}
complete -o nospace  -F _ebook_device ebook-device

complete -o nospace -C calibre-complete ebook-convert
