SRC_URL=http://www.wagner.pp.ru/~vitus/fiction
UPLOAD_URL=https://dav.wagner.pp.ru/vitus/fiction
ID=90860948-65dc-11e5-bd77-bc5ff453db06
VERSION=0.$(shell date +%s)

all: jasmina.html jasmina.epub jasmina.fb2.zip

fb2: jasmina.fb2.zip
pdf: jasmina.pdf

publish: jasmina.html jasmina.epub jasmina.fb2.zip jasmina.pdf
	for i in $^; do curl --digest -n -T $$i $(UPLOAD_URL)/$$i || exit 1;done

all.mdwn: order.lst $(wildcard *.txt)
	for i in `cat order.lst`; do sed 's/<TBD>/*(Фрагмент недописан)*/' "$$i"; echo ""; done > $@
jasmina.html: all.mdwn
	pandoc -t html --toc -s all.mdwn -o jasmina.html
jasmina.epub: all.mdwn jasmina-title.jpg
	pandoc -t epub -s  all.mdwn -o jasmina.epub --epub-cover-image=jasmina-title.jpg
jasmina.fb2.zip: jasmina.fb2
	zip $@ $+

annotation.html: annotation.txt
	pandoc -o annotation.html annotation.txt
jasmina.fb2:	all.mdwn annotation.html jasmina-title.jpg Makefile
	pandoc -t fb2 -V language=ru all.mdwn -o jasmina.fb2
	sed -i 's!<annotation>.*</annotation>!!' $@
	sed -i 's!\(<book-title>.*</book-title>\)\(<author>.*</author>\)!\2\1!' $@
	sed -i 's!</title-info>!<lang>ru</lang></title-info>!' $@
	sed -i 's!</lang>!</lang><coverpage><image l:href="#cover" /></coverpage>!' $@
	sed -i 's!<title-info>!<title-info><genre>sf_history</genre>!' $@
	sed -i "s!</book-title>!</book-title><annotation>`cat annotation.html|tr -d '\n'`</annotation>!" $@
	sed -i "s!</annotation>!</annotation><date value='`date "+%Y-%m-%d"`'>`date +%Y`</date>!" $@
	sed -i "s!</program-used>!</program-used><date value='`date "+%Y-%m-%d"`'>`date +%Y`</date>!" $@
	sed -i 's!<document-info>!<document-info><author><first-name>Виктор</first-name><last-name>Вагнер</last-name></author>!'  $@
	sed -i 's!</document-info>!<src-url>${SRC_URL}</src-url><id>${ID}</id><version>${VERSION}</version></document-info>!' $@
	sed -i "s!</FictionBook>!<binary id='cover' content-type='image/jpeg'>`base64 -w 0 jasmina-title.jpg`</binary></FictionBook>!" $@
jasmina.pdf: jasmina.tex all.tex jasmina-title.jpg
	xelatex jasmina.tex
	xelatex jasmina.tex
all.tex: all.mdwn
	pandoc -o all.tex all.mdwn
	sed -i -e 's! ---!~---!g' -e 's!\([а-я]\)-\([А-Яа-я]\)!\1"=\2!g' all.tex
	sed -i -e 's!\\section{!\\part{!' -e 's!\\subsection{!\\chapter{!' all.tex

notes: all.mdwn
	# List all the footnotes
	grep '^\[\^' all.mdwn|sort -n -k 1.3

	
