handouts/build.sh

157 lines
3.5 KiB
Bash
Raw Normal View History

2023-10-10 22:00:32 -07:00
#!/usr/bin/env bash
2023-10-11 09:44:32 -07:00
#
# This script builds every document in this repo,
# (handout and solutions, if they exist), and creates
# a zip of all resulting files.
# Where we're running this script
2023-10-10 22:00:32 -07:00
run_dir=$(pwd)
2023-10-11 09:44:32 -07:00
# Output files
target_dir="${run_dir}/output"
target_zip="${run_dir}/output.zip"
2023-10-10 22:00:32 -07:00
2023-10-11 09:44:32 -07:00
# Clean up previous build
rm -drf "${target_dir}"
rm -f "${target_zip}"
# Build one document.
# Args: <target_dir> <job_name> <doc_dir> <main_file>
# target_dir: move output pdf to this directory
# job_name: name of this document. Output is saved as job_name.pdf
# doc_dir: cd here before building.
# main_file: build this tex file. Usually main.tex
2023-10-10 22:00:32 -07:00
function build() {
2023-12-11 15:03:48 -08:00
local b_target_dir="${1}"
2023-10-11 09:44:32 -07:00
local job_name="${2}"
local doc_dir="${3}"
local main_file="${4}"
2023-10-10 22:00:32 -07:00
echo "|> Building ${job_name}..."
cd "${doc_dir}"
2023-10-23 10:58:48 -07:00
tectonic_args=(
--chatter minimal
2023-11-09 21:59:59 -08:00
--web-bundle "https://static.betalupi.com/tectonic/texlive2023.tar"
2023-10-23 10:58:48 -07:00
)
2023-10-10 22:21:42 -07:00
# Build handout
2023-10-11 09:44:32 -07:00
echo "\\def\\argNoSolutions{1}\\input{${main_file}}" | \
2023-10-10 22:21:42 -07:00
tectonic \
2023-10-23 10:58:48 -07:00
"${tectonic_args[@]}" \
2023-10-10 22:21:42 -07:00
--outfmt pdf \
-
2023-12-11 15:03:48 -08:00
handout_stat=$?
2023-10-10 22:21:42 -07:00
2023-12-11 15:03:48 -08:00
if [[ $handout_stat == 0 ]]; then
mkdir -p "${b_target_dir}"
mv texput.pdf "${b_target_dir}/${job_name}.pdf"
2023-10-10 22:21:42 -07:00
else
2023-12-11 15:03:48 -08:00
rmdir --ignore-fail-on-non-empty "${b_target_dir}"
2023-10-10 22:21:42 -07:00
rm -f texput.pdf
echo "|> Handout build failed"
echo ""
fi
# Build solutions
2023-10-11 09:44:32 -07:00
echo "\\def\\argYesSolutions{1}\\input{${main_file}}" | \
2023-10-10 22:21:42 -07:00
tectonic \
2023-10-23 10:58:48 -07:00
"${tectonic_args[@]}" \
2023-10-10 22:21:42 -07:00
--outfmt pdf \
-
2023-10-10 22:00:32 -07:00
2023-12-11 15:03:48 -08:00
solution_stat=$?
2023-10-10 22:00:32 -07:00
2023-12-11 15:03:48 -08:00
if [[ $solution_stat == 0 ]]; then
mkdir -p "${b_target_dir}"
mv texput.pdf "${b_target_dir}/${job_name}.sols.pdf"
2023-10-10 22:00:32 -07:00
else
2023-12-11 15:03:48 -08:00
rmdir --ignore-fail-on-non-empty "${b_target_dir}"
2023-10-10 22:21:42 -07:00
rm -f texput.pdf
echo "|> Solution build failed"
2023-10-10 22:00:32 -07:00
fi
2023-10-11 09:44:32 -07:00
# Clean up if files contents are identical
2023-12-11 15:03:48 -08:00
#if [[ $(cmp -bl "${b_target_dir}/${job_name}.sols.pdf" "${b_target_dir}/${job_name}.pdf" | wc -l) < 200 ]]; then
# echo "|> Versions identical, removing ${job_name}.sols.pdf"
# rm "${b_target_dir}/${job_name}.sols.pdf"
# solution_stat=1;
#fi
2023-10-11 09:44:32 -07:00
echo ""
2023-12-11 15:03:48 -08:00
if [[ $handout_stat == 0 ]]; then
(
echo -n "{"
echo -n "\"title\": \"${job_name}\"",
echo -n "\"type\": \"$(realpath --relative-to="${target_dir}" "${b_target_dir}")\"",
echo -n "\"description\": \"${job_name}\"",
echo -n "\"handout\": \"https://static.betalupi.com/ormc/$(realpath --relative-to="${target_dir}" "${b_target_dir}")/${job_name}.pdf\","
if [[ $solution_stat == 0 ]]; then
2024-05-24 23:26:01 -07:00
echo -n "\"solutions\": \"https://static.betalupi.com/ormc/$(realpath --relative-to="${target_dir}" "${b_target_dir}")/${job_name}.sols.pdf\","
fi
if [[ $solution_stat == 0 ]]; then
echo -n "\"starred\": true"
else
echo -n "\"starred\": false"
2023-12-11 15:03:48 -08:00
fi
echo -n "},"
) >> "$target_dir/index.json"
fi
2023-10-10 22:00:32 -07:00
cd "${run_dir}"
}
2023-12-11 15:03:48 -08:00
mkdir -p "${target_dir}"
echo -n "[" > "$target_dir/index.json"
2023-10-10 22:21:42 -07:00
2023-10-10 22:00:32 -07:00
2023-10-11 09:44:32 -07:00
for d in "${run_dir}/Misc/Warm-Ups"/*.tex ; do
file="$(basename "${d}")"
build \
"${target_dir}/Warm-Ups" \
"${file%.*}" \
"${run_dir}/Misc/Warm-Ups" \
"${file}"
done
2023-10-10 22:21:42 -07:00
2023-10-11 09:44:32 -07:00
for d in "${run_dir}/Advanced"/*/ ; do
2023-10-10 22:00:32 -07:00
doc_dir=$(realpath "${d}")
2023-10-11 09:44:32 -07:00
job_name="$(basename "${doc_dir}")"
2023-10-10 22:00:32 -07:00
2023-10-11 09:44:32 -07:00
build \
"${target_dir}/Advanced" \
"${job_name}" \
"${doc_dir}" \
"main.tex"
2023-10-10 22:00:32 -07:00
done
2023-10-11 09:44:32 -07:00
for d in "${run_dir}/Intermediate"/*/ ; do
doc_dir=$(realpath "${d}")
job_name="$(basename "${doc_dir}")"
2023-10-10 22:21:42 -07:00
2023-10-11 09:44:32 -07:00
build \
"${target_dir}/Intermediate" \
"${job_name}" \
"${doc_dir}" \
"main.tex"
done
2023-10-10 22:21:42 -07:00
2023-12-11 15:03:48 -08:00
echo -n "{}]" >> "$target_dir/index.json"
2023-10-10 22:21:42 -07:00
2023-10-11 09:44:32 -07:00
# cd so paths in zip are relative
cd "${target_dir}"
2023-12-11 15:03:48 -08:00
zip -FSr "${target_zip}" .
zip -d "${target_zip}" "index.json" # We don't need this in the zip