summaryrefslogtreecommitdiffstats
path: root/make.sh
blob: 2c6fe5eca30747ee91e053e4abb47efc3c57041b (plain) (blame)
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
#!/bin/bash

if [ -z "$1" ]; then
	echo "Pass abolute path to where slx-tools will be located"
	exit 1
fi

declare -rg BASE="$1"

shopt -s globstar
set -e


DEFS=
FUNCTIONS=

for file in modules/**/*.inc; do
	name="${file#modules/}"
	name="${name//\//_}"
	name="${name%.inc}"
	[[ "$name" =~ ^[a-z0-9_]+$ ]] || continue
	echo " * Handling $file"
	if ! bash -n "$file"; then
		echo "FILE CONTAINS ERRORS; ABORTING"
		rm -f -- slx-tools
		exit 1
	fi
	FUNCTIONS="${FUNCTIONS%, }"'\n  '
	for var in $( grep -oP '^[a-z0-9_]+\s*\(\)' "$file" | grep -oE '^[a-z0-9_]+' ); do
		if ! [[ "$var" == "$name"* ]]; then
			echo "- IGNORING $var"
			continue
		fi
		echo "+ Including $var"
		DEFS+="$var() { . \$BASE/$file; $var "'"$@"; }\n'
		FUNCTIONS+="$var, "
	done
done

sed "s#%DEFS%#${DEFS}#;s#%FUNCTIONS%#${FUNCTIONS%, }#;s#^BASE=.*\$#BASE='$BASE'#" slx-tools.template > slx-tools
chmod +x slx-tools