aboutsummaryrefslogblamecommitdiff
path: root/v2/src/bin/makemake
blob: e95a040b9d73df39d606842e6efbcd5dd886598e (plain) (tree)



































































                                                                            
                                                                            




                   






                                                  


           
     







            
     
         







                                          
                                         






                                                                     
                                         






                                           
 
                 
                                                



                
                                 



















                                                                   
#!/bin/sh
set -eu


usage() {
	cat <<-'EOF'
		Usage:
		  makemake
		  makemake -h
	EOF
}

help() {
	cat <<-'EOF'

		Options:
		  -h, --help    show this message


		Generate make(1) code for later evaluation by make(1).  What
		this scripts does is fill the gap where make(1) can't handle
		globs and dynamic dependencies, and uses some ad-hoc scripts
		to generate those.
	EOF
}

for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))



varlist() {
	printf '%s = \\\n' "$1"
	sed \
		-e 's/^/	/' \
		-e 's/$/ \\/'
	printf '\n'
}

html_deps() {
	"$@" | sed 's/^\(.*\)\.md$/\1.conf \1.content: \1.md/'
	"$@" | sed 's/^\(.*\)\.md$/\1.snippets \1.html: \1.conf \1.content/'
	printf '\n'
}


content_mds() {
	{
		if [ "$r" = 'pages' ]; then
			echo src/content/*.md
		else
			echo src/content/"$r"/*.md
		fi
	} | tr ' ' '\n'
}

RESOURCES='
pages
pastebins
tils
'

EXTENSIONS='
.md
.conf
.content
.html
.snippets
'

for r in $RESOURCES; do
	content_mds "$r" | varlist "$r".md
	for e in $EXTENSIONS; do
		if [ "$e" = '.md' ]; then
			continue
		fi
		# shellcheck disable=2016
		printf '%s%s = $(%s.md:.md=%s)\n' "$r" "$e" "$r" "$e"
	done
	printf '%s =' "$r"
	for e in $EXTENSIONS; do
		if [ "$e" = '.md' ]; then
			continue
		fi
		# shellcheck disable=2016
		printf ' $(%s%s)' "$r" "$e"
	done
	printf '\n'
	html_deps content_mds "$r"
	printf '\n'
done


all_resources() {
	echo "$RESOURCES" | tr ' ' '\n' | grep .
}

all_vars() {
	EXT="$1"
	# shellcheck disable=2016
	all_resources |
		sed 's|^|$(|' |
		sed "s|$|$EXT)|" |
		varlist all-generated"$EXT"
}

for e in $EXTENSIONS; do
	all_vars "$e"
done
all_vars ''


git ls-files |
	grep -v '^src/content/' |
	varlist 'non-content'

git ls-files src/content/ |
	grep -v '\.md$' |
	grep -Ev "src/content/($(all_resources | paste -sd'|'))/" |
	varlist 'static-content'