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:
- 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?
- 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