#!/bin/bash
# Formatting/Migration tool for Spacemacs
#
# Arguments:
# $1: action name can be `all`, `doc`, `config`
# $2: if equal to `test` then use test files as input
# TODO a function to add headers to config.el, packages.el etc...
if ! [ -d "./.git" ]
then
echo "Should be executed from the repo root."
exit 1
fi
#Use "sed" or "gsed" if avaliable.
seder="sed"
if hash gsed 2>/dev/null; then
seder="gsed"
#Use md5 or md5sum
mdfive="md5sum"
if hash md5 2>/dev/null; then
mdfive="md5"
if [[ $2 = "test" ]]
places=("./core/tools/doc-fmt/tests")
else
places=("./doc" "./layers")
for place in "${places[@]}"
do :
before_md5="foo"
after_md5="bar"
while ! [ "$before_md5" = "$after_md5" ]
do
# Calculate md5 of the files in $place before formating
before_md5=$(find $place -type f -exec $mdfive {} \; | sort -k 2 | $mdfive)
if [ $1 = "all" ] || [ $1 == "doc" ]
# Remove trailing delimiters in headlines
find $place -name "*.org" -type f -exec $seder -i 's/^\(*\+\s\+.*\)[;,.]$/\1/g' {} \;
# Remove trailing spaces
find $place -name "*.org" -type f -exec $seder -i 's/[ \t]*$//' {} \;
# Remove #+HTML_HEAD_EXTRA: ... readtheorg.css" />
find $place -name "*.org" -type f -exec $seder -i '/#+HTML_HEAD_EXTRA.*readtheorg.css.*/d' {} \;
# Replace multiply empty lines with a single empty line
find $place -name "*.org" -type f -exec $seder -i '/^$/N;/^\n$/D' {} \;
# Replace :TOC_4_org: with :TOC_4_gh:
find $place -name "*.org" -type f -exec $seder -i 's/:TOC_4_org:/:TOC_4_gh:/' {} \;
# apply toc-org
find $place -name "*.org" -type f -exec emacs -batch -l ./core/tools/doc-fmt/fmt.el '{}' -f apply-all \;
if [ $1 = "all" ] || [ $1 == "config" ]
# migrate packages lists to config.el
find $place -name "packages.el" -type f -exec emacs -batch -l ./core/tools/doc-fmt/fmt.el '{}' -f move-packages-to-config \;
# Calculate md5 of the files in $place after formating
after_md5=$(find $place -type f -exec $mdfive {} \; | sort -k 2 | $mdfive)
done