0

I try to create a makefile which converts ebook formats with the help of calibre-convert (ebook-convert). My preferred order of formats is epub > mobi > pdf. The makefile should be executed in a folder with ebooks which have different formats (epub, mobi, pdf). One eBook can lay there in several formats. Make should convert all existing ebooks in all not existing formats of a book (e.g. a ebook has the formats epub and pdf - make should create the missing mobi variant). Which source format is used for conversation is set by the preferred order of formats. I manage to create a makefile, which converts all missing formats. See below.

SOURCES:=$(shell find . -type f -iname "*.epub" -or -iname "*.mobi" -or -iname "*.pdf" 2>/dev/null | sed 's/ /\*/g')

TARGETS_EPUB:=$(SOURCES:%.mobi=%.epub) $(SOURCES:%.pdf=%.epub)
TARGETS_MOBI:=$(SOURCES:%.epub=%.mobi) $(SOURCES:%.pdf=%.mobi)
TARGETS_PDF:=$(SOURCES:%.epub=%.pdf) $(SOURCES:%.mobi=%.pdf)

%.epub: %.mobi
        ebook-convert '$(subst *, ,$^)' '$(subst *, ,$@)'

%.epub: %.pdf
        ebook-convert '$(subst *, ,$^)' '$(subst *, ,$@)'

%.mobi: %.epub
        ebook-convert '$(subst *, ,$^)' '$(subst *, ,$@)'

%.mobi: %.pdf
        ebook-convert '$(subst *, ,$^)' '$(subst *, ,$@)'

%.pdf: %.epub
        ebook-convert '$(subst *, ,$^)' '$(subst *, ,$@)'

%.pdf: %.mobi
        ebook-convert '$(subst *, ,$^)' '$(subst *, ,$@)'

all: epub mobi pdf

epub: ${TARGETS_EPUB}
mobi: ${TARGETS_MOBI}
pdf: ${TARGETS_PDF}

This makefile reads all epub, mobi and pdf files of the current directory. Then it replaces spaces in the filenames with * placeholder (because make arrays are space separated). The targets set how make should convert the formats and in which order.

The commands how to convert one format in the other is set below the target variables. ebook-convert detects the source and target formats by parsing the file extensions. Because of that the command line for every format conversation is the same.

With this makefile I have some problems:

  1. it e.g. converts the missing epub out of mobi and then it uses the generated epub to generate the missing pdf. But I only want to convert files from the still existing formats and not from newly generated. Maybe there is a possibility to advise make to only use existing files as source?
  2. I want to convert ebooks in subfolders of the current directory. But I have problems if these folders have spaces in their name. I tested this by extent the find command by searching recursively. Replacing the spaces by a placeholder didn't work in folder names.

I hope someone has good gnu make experience and can help me with these problems.

Thanks in advance

Best Regards

Axel

mawey
  • 93
  • 5
  • make doesn't like files with spaces **at all** you can get certain things to work a bit until you look at it funny and then it falls down. Just don't do that. Your life will be **much** easier. – Etan Reisner Apr 03 '15 at 22:27
  • The problem with spaces is not my main problem. At the end I can live with these problems. My main problem is the conversation problem. Do you have any ideas to problem #1 ? – mawey Apr 04 '15 at 10:04
  • [Please rename all files with spaces in their names. This will make you unhappy. _make will not_ do what you want.] You seem to be asking _make_ to fill in any gaps. So `f.epub` needs to produce an `f.mobi` and `f.pdf`. Similarly you want `g.pdf` to become `g.mobi` and g.epub`. Is that correct? – bobbogo Apr 17 '15 at 11:01
  • With `make` I want to generate every format which is not there. At the end I want the formats `epub`, `mobi` and `pdf`. As source for the conversation `make` should use the formats which are already there. If there is more than one source format, it should use them by this priority: `epub > mobi > pdf`. BUT: generated files shouldn't be used for the conversation: e.g. if there is a `mobi` and `make` generates `epub`, this `epub` shouldn't be used to generate the `pdf`. Instead, the original (not converted) `mobi` should be used to generate the `pdf`, too. – mawey May 15 '15 at 20:56
  • Has anyone ideas to solve this? – mawey May 15 '15 at 20:57

0 Answers0