all: hypervideo doc pypi-files
clean: clean-test clean-dist clean-cache
completions: completion-bash completion-fish completion-zsh
doc: README.md CONTRIBUTING.md
ot: offlinetest
tar: hypervideo.tar.gz

# Keep this list in sync with MANIFEST.in
# intended use: when building a source distribution,
# make pypi-files && python setup.py sdist
pypi-files: AUTHORS Changelog.md LICENSE README.md completions devscripts/* test/*

.PHONY: all clean install test tar pypi-files completions ot offlinetest codetest

clean-test:
	rm -rf test/testdata/sigs/player-*.js tmp/ *.annotations.xml *.aria2 *.description *.dump *.frag *.frag.urls \
	*.frag.aria2 *.frag.urls *.info.json *.live_chat.json *.meta *.part* *.tmp *.temp *.unknown_video *.ytdl \
	*.3gp *.ape *.ass *.avi *.desktop *.f4v *.flac *.flv *.gif *.jpeg *.jpg *.m4a *.m4v *.mhtml *.mkv *.mov *.mp3 \
	*.mp4 *.mpga *.oga *.ogg *.opus *.png *.sbv *.srt *.swf *.swp *.tt *.ttml *.url *.vtt *.wav *.webloc *.webm *.webp  *.ytdl
clean-dist:
	rm -rf MANIFEST build/ dist/ .coverage cover/ hypervideo.tar.gz completions/ hypervideo_dl/extractor/lazy_extractors.py *.spec CONTRIBUTING.md.tmp hypervideo hypervideo.exe hypervideo_dl.egg-info/ AUTHORS .mailmap
clean-cache:
	find . -name "*.pyc" -o -name "*.class" -delete

completion-bash: completions/bash/hypervideo
completion-fish: completions/fish/hypervideo.fish
completion-zsh: completions/zsh/_hypervideo
lazy-extractors: hypervideo_dl/extractor/lazy_extractors.py

PREFIX ?= /usr/local
DESTDIR ?= .
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/man
SHAREDIR ?= $(PREFIX)/share
PYTHON ?= /usr/bin/env python3

# set SYSCONFDIR to /etc if PREFIX=/usr or PREFIX=/usr/local
SYSCONFDIR = $(shell if [ $(PREFIX) = /usr -o $(PREFIX) = /usr/local ]; then echo /etc; else echo $(PREFIX)/etc; fi)

install: hypervideo completions
	install -Dm755 hypervideo $(DESTDIR)$(BINDIR)
	install -Dm644 completions/bash/hypervideo $(DESTDIR)$(SHAREDIR)/bash-completion/completions/hypervideo
	install -Dm644 completions/zsh/_hypervideo $(DESTDIR)$(SHAREDIR)/zsh/site-functions/_hypervideo
	install -Dm644 completions/fish/hypervideo.fish $(DESTDIR)$(SHAREDIR)/fish/vendor_completions.d/hypervideo.fish

codetest:
	flake8 .

test:
	$(PYTHON) -m pytest
	$(MAKE) codetest

offlinetest: codetest
	$(PYTHON) -m pytest -k "not download"

hypervideo: hypervideo_dl/*.py hypervideo_dl/*/*.py
	mkdir -p zip
	for d in hypervideo_dl hypervideo_dl/downloader hypervideo_dl/extractor hypervideo_dl/postprocessor ; do \
	  mkdir -p zip/$$d ;\
	  cp -pPR $$d/*.py zip/$$d/ ;\
	done
	touch -t 200001010101 zip/hypervideo_dl/*.py zip/hypervideo_dl/*/*.py
	mv zip/hypervideo_dl/__main__.py zip/
	cd zip ; zip -q ../hypervideo hypervideo_dl/*.py hypervideo_dl/*/*.py __main__.py
	rm -rf zip
	echo '#!$(PYTHON)' > hypervideo
	cat hypervideo.zip >> hypervideo
	rm hypervideo.zip
	chmod a+x hypervideo

README.md: hypervideo_dl/*.py hypervideo_dl/*/*.py
	COLUMNS=80 $(PYTHON) hypervideo_dl/__main__.py --help | $(PYTHON) devscripts/make_readme.py

CONTRIBUTING.md: README.md
	$(PYTHON) devscripts/make_contributing.py README.md CONTRIBUTING.md

completions/bash/hypervideo: hypervideo_dl/*.py hypervideo_dl/*/*.py devscripts/bash-completion.in
	mkdir -p completions/bash
	$(PYTHON) devscripts/bash-completion.py

completions/zsh/_hypervideo: hypervideo_dl/*.py hypervideo_dl/*/*.py devscripts/zsh-completion.in
	mkdir -p completions/zsh
	$(PYTHON) devscripts/zsh-completion.py

completions/fish/hypervideo.fish: hypervideo_dl/*.py hypervideo_dl/*/*.py devscripts/fish-completion.in
	mkdir -p completions/fish
	$(PYTHON) devscripts/fish-completion.py

_EXTRACTOR_FILES = $(shell find hypervideo_dl/extractor -iname '*.py' -and -not -iname 'lazy_extractors.py')
hypervideo_dl/extractor/lazy_extractors.py: devscripts/make_lazy_extractors.py devscripts/lazy_load_template.py $(_EXTRACTOR_FILES)
	$(PYTHON) devscripts/make_lazy_extractors.py $@

hypervideo.tar.gz: all
	@tar -czf $(DESTDIR)/hypervideo.tar.gz --transform "s|^|hypervideo/|" --owner 0 --group 0 \
		--exclude '*.DS_Store' \
		--exclude '*.kate-swp' \
		--exclude '*.pyc' \
		--exclude '*.pyo' \
		--exclude '*~' \
		--exclude '__pycache__' \
		--exclude '.pytest_cache' \
		--exclude '.git' \
		-- \
		bin README.md Changelog.md LICENSE \
		CONTRIBUTING.md CONTRIBUTORS AUTHORS \
		Makefile MANIFEST.in README.md completions \
		setup.py setup.cfg hypervideo hypervideo_dl requirements.txt \
		devscripts test

AUTHORS: .mailmap
	git shortlog -s -n | cut -f2 | sort > AUTHORS

.mailmap:
	git shortlog -s -e -n | awk '!(out[$$NF]++) { $$1="";sub(/^[ \t]+/,""); print}' > .mailmap
