aboutsummaryrefslogtreecommitdiff
path: root/scripts/lesson-make
blob: d5639b7a87e7038382abc4983f2d2ffff93ef12a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/bash
MAKEFILE='Makefile'
while [[ ! -f "${MAKEFILE}" ]]; do
	MAKEFILE=../"${MAKEFILE}"
done

FIND="find . -maxdepth 1 '(' -name '*.md' -o -name '*.tex' ')' -a -not -name 'Aufgabe[0-9].tex' -a -not -name '_*'"

if [[ ! -f "${MAKEFILE}" ]]; then
	echo "No root Makefile found" >&2
	exit 1
fi

targets=( "$@" )
if [[ -z "${targets[*]}" ]]; then
	# Autodetect targets
	mapfile -t sources < <(eval "${FIND}")
	for source in "${sources[@]}"; do
		declare -a new_targets=()
		source_name="$(basename "${source}")"
		case "$source_name" in
		slides*.md)
			new_targets+=("${source_name/.md/.pdf}" "${source_name/.md/.pptx}")
			;;
		*)
			mapfile -t new_targets < <(echo "${source}" | sed -e 's/\.tex$/.pdf/g' -e 's/\.md$/.pdf/g')
			;;
		esac

		if [[ "${source}" != *solution*.tex ]] \
		&& [[ "${source}" != *"test_lehrer.tex" ]] \
		&& [[ "${source}" != *"test_loesung.tex" ]] \
		&& grep -qe '\\iflehrer\|lehrerversion\|\\[p]\?gap\|\\[p]\?solution' "${source}"; then
			declare -a sus_targets=( "${new_targets[@]}" )
			for sus_target in "${sus_targets[@]}"; do
				dir=$(dirname "${sus_target}")
				target_file=$(basename "${sus_target}")
				new_targets+=("${dir}/lehrer_${target_file}")
			done
		fi
		targets+=( "${new_targets[@]}" )
	done
fi

exec make -f "${MAKEFILE}" "${targets[@]}"