noxz-sites

A collection of a builder and various scripts creating the noxz.tech sites
git clone https://noxz.tech/git/noxz-sites.git
noxz-sites

commit: 958931c5177713f8d58f2b50b91e28eb809df997
parent: f3849c601ca122913df872259b0b8a140edcd3e0
author: Chris Noxz <chris@noxz.tech>
date:   Mon, 27 Dec 2021 17:33:09 +0100
implement shellcheck corrections
Mbuild221++++++++++----------
1 file changed, 113 insertions(+), 108 deletions(-)
diff --git a/build b/build
@@ -28,6 +28,7 @@ CFG_SITEMAP_XML="${CFG_DIR}/sitemap.xml"
 CFG_SITEMAP_XML_ITEM="${CFG_DIR}/sitemap_item.xml"
 
 # helper function to html encode stdin
+# shellcheck disable=SC2120
 html_encode() {
 	if [ ! -t 0 ]; then
 		cat -
@@ -41,6 +42,8 @@ html_encode() {
 		-e 's/>/\&gt;/g'
 }
 
+# helper function to produce awk safe strings
+# shellcheck disable=SC2120
 awk_safe() {
 	if [ ! -t 0 ]; then
 		cat -
@@ -50,14 +53,13 @@ awk_safe() {
 	|  sed                                                                  \
 		-e 's/\\/\\\\/g'                                                    \
 		-e 's/\&/\\\\\&/g'
-
 }
 
 print_loading() {
 	>&2 printf '[%s*%s] %-.100s '                                           \
 		"$(tput setaf 12)"                                                  \
 		"$(tput sgr 0)"                                                     \
-		"${1} $(for i in `seq 1 100`; do printf '.'; done)"
+		"${1} $(for _ in $(seq 1 100); do printf '.'; done)"
 }
 
 print_done() {
@@ -69,94 +71,94 @@ print_done() {
 # arg 1: site root eg. ../noxz-sites/noxz.tech
 # arg 2: current site eg. ../noxz-sites/noxz.tech/guides/groff
 render_main_menu() {
-	# variables used
-	local _root="${1}"
-	local _current="${2}"
-	local _active=""
-	local _output=""
-	local _buffer=""
-	local _home="$(printf '<li><a href="//%s/">home</a></li>' "${SITE_DOMAIN}")"
+	# variables used in main menu
+	menu_root="${1}"
+	menu_current="${2}"
+	menu_active=""
+	menu_output=""
+	menu_buffer=""
+	menu_home="$(printf '<li><a href="//%s/">home</a></li>' "${SITE_DOMAIN}")"
 
 	# check if both directories exist
-	[ ! -d "${_root}" ] && return
-	[ ! -d "${_current}" ] && return
+	[ ! -d "${menu_root}" ] && return
+	[ ! -d "${menu_current}" ] && return
 
 	# remove trailing slashes if existing
-	_root=$(dirname "${_root}/*")
-	_current=$(dirname "${_current}/*")
+	menu_root=$(dirname "${menu_root}/*")
+	menu_current=$(dirname "${menu_current}/*")
 
 	# make sure current directory is a subdirectory of root
-	echo "${_current}" | grep -qv "^${_root}" && return
+	echo "${menu_current}" | grep -qv "^${menu_root}" && return
 
 	# root menu
-	if [ "${_root}" = "${_current}" ]; then
-		_home="$(echo "$_home" | sed 's/<li/<li class="active"/')"
-		for f in ${_current}/*; do
-			[ ! -d "${f}" ] && continue                 # only process directories
-			[ -f "${f}/.buildignore" ] && continue      # skip buildignore
+	if [ "${menu_root}" = "${menu_current}" ]; then
+		menu_home="$(echo "${menu_home}" | sed 's/<li/<li class="active"/')"
+		for f in "${menu_current}"/*; do
+			[ ! -d "${f}" ] && continue                                     # only process directories
+			[ -f "${f}/.buildignore" ] && continue                          # skip buildignore
 
-			ff="$(echo "${f}" | sed "s?^${_root}?//${SITE_DOMAIN}?")"
+			ff="$(echo "${f}" | sed "s?^${menu_root}?//${SITE_DOMAIN}?")"
 			fn="$(echo "${f##*/}" | sed 's/_/ /g')"
-			_output=$(printf '%s<li><a href="%s">%s</a></li>'               \
-				"${_output}"                                                \
+			menu_output=$(printf '%s<li><a href="%s">%s</a></li>'           \
+				"${menu_output}"                                            \
 				"${ff}"                                                     \
 				"${fn}")
 		done
 	fi
 
 	# run until root directory is the current one
-	while [ "${_root}" != "${_current}" ]; do
+	while [ "${menu_root}" != "${menu_current}" ]; do
 
 		# handle children of first current
-		if [ "${_current}" = "${2}" ]; then
-			for f in ${_current}/*; do
-				[ ! -d "${f}" ] && continue             # only process directories
-				[ -f "${f}/.buildignore" ] && continue  # skip buildignore
-				_output="$(printf '%s<li' "${_output}")"
-				ff="$(echo "${f}" | sed "s?^${_root}?//${SITE_DOMAIN}?")"
+		if [ "${menu_current}" = "${2}" ]; then
+			for f in "${menu_current}"/*; do
+				[ ! -d "${f}" ] && continue                                 # only process directories
+				[ -f "${f}/.buildignore" ] && continue                      # skip buildignore
+				menu_output="$(printf '%s<li' "${menu_output}")"
+				ff="$(echo "${f}" | sed "s?^${menu_root}?//${SITE_DOMAIN}?")"
 				fn="$(echo "${f##*/}" | sed 's/_/ /g')"
-				_output=$(printf '%s><a href="%s">%s</a></li>'              \
-					"${_output}"                                            \
+				menu_output=$(printf '%s><a href="%s">%s</a></li>'          \
+					"${menu_output}"                                        \
 					"${ff}"                                                 \
 					"${fn}")
 			done
 		fi
 
-		_active="${_current}"                           # mark current as active
-		_current="${_current%/*}"                       # mark parent as current
+		menu_active="${menu_current}"                                       # mark current as active
+		menu_current="${menu_current%/*}"                                   # mark parent as current
 
 		# handle all siblings
-		for f in ${_current}/*; do
-			[ ! -d "${f}" ] && continue                 # only process directories
-			[ -f "${f}/.buildignore" ] && continue      # skip buildignore
+		for f in "${menu_current}"/*; do
+			[ ! -d "${f}" ] && continue                                     # only process directories
+			[ -f "${f}/.buildignore" ] && continue                          # skip buildignore
 
-			_buffer=$(printf '%s<li' "${_buffer}")
-			ff="$(echo "${f}" | sed "s?^${_root}?//${SITE_DOMAIN}?")"
+			menu_buffer=$(printf '%s<li' "${menu_buffer}")
+			ff="$(echo "${f}" | sed "s?^${menu_root}?//${SITE_DOMAIN}?")"
 			fn="$(echo "${f##*/}" | sed 's/_/ /g')"
-			if [ "${f}" = "${_active}" ]; then          # treat active different
-				_buffer=$(printf '%s class="active"><a href="%s">%s</a>'    \
-					"${_buffer}"                                            \
+			if [ "${f}" = "${menu_active}" ]; then                          # treat active different
+				menu_buffer=$(printf '%s class="active"><a href="%s">%s</a>'\
+					"${menu_buffer}"                                        \
 					"${ff}"                                                 \
 					"${fn}")
 				# dump output to buffer if existing (sub menu)
-				[ "$_output" != "" ]                                        \
-				&& _buffer=$(printf '%s<ul>%s</ul>'                         \
-					"${_buffer}"                                            \
-					"${_output}")
-				_buffer=$(printf '%s</li>' "${_buffer}")
+				[ "${menu_output}" != "" ]                                    \
+				&& menu_buffer=$(printf '%s<ul>%s</ul>'                     \
+					"${menu_buffer}"                                        \
+					"${menu_output}")
+				menu_buffer=$(printf '%s</li>' "${menu_buffer}")
 			else
-				_buffer=$(printf '%s><a href="%s">%s</a></li>'              \
-					"${_buffer}"                                            \
+				menu_buffer=$(printf '%s><a href="%s">%s</a></li>'          \
+					"${menu_buffer}"                                        \
 					"${ff}"                                                 \
 					"${fn}")
 			fi
 		done
-		_output="${_buffer}"
-		_buffer=""
+		menu_output="${menu_buffer}"
+		menu_buffer=""
 	done
 	printf '<ul id="main-nav">%s%s</ul>'                                    \
-		"${_home}"                                                          \
-		"${_output}"                                                        \
+		"${menu_home}"                                                      \
+		"${menu_output}"                                                    \
 	| sed                                                                   \
 		-e 's,\(<\(ul\|li\)[^>]*>\|</ul>\),\n\1,2g'                         \
 		-e 's,\(</ul>\)\(</li>\),\1\n\2,g'                                  \
@@ -354,16 +356,16 @@ render_main_content() {
 }
 
 render_main() {
-	local _current="${2}"
+	main_current="${2}"
 
-	[ ! -d "${_current}" ] && return
-	_current=$(dirname "${_current}/*")
+	[ ! -d "${main_current}" ] && return
+	main_current=$(dirname "${main_current}/*")
 
-	[ -f "${_current}/index.www" ] && SITE_WWW="${_current}/index.www" || return
-	[ -f "${_current}/.title" ] && SITE_TITLE="$(<"${_current}/.title")"
+	[ -f "${main_current}/index.www" ] && SITE_WWW="${main_current}/index.www" || return
+	[ -f "${main_current}/.title" ] && SITE_TITLE="$(cat "${main_current}/.title")"
 	SITE_HTML="${SITE_WWW%.*}.html"
 
-	print_loading "Rendering ${_current}"
+	print_loading "Rendering ${main_current}"
 
 	awk                                                                     \
 		-v var_author="$(awk_safe "${SITE_AUTHOR}")"                        \
@@ -416,15 +418,15 @@ render_main() {
 }
 
 render_git_repo() {
-	local _path="${SITE_GIT_REPO_ROOT}/$2"
-	local _path_head="$1/${2}/head.html.tmp"
-	local _path_log="$1/${2}/log.html.tmp"
-	local _path_readme="$1/${2}/readme.html.tmp"
-	local _path_license="$1/${2}/license.html.tmp"
-	local _name="${2%.*}"
-	local _description="$(cat "${1}/${2}"/description | html_encode)"
-
-	rsync -a --delete-before "${_path}/" "$1/${2##*/}"
+	repo_path="${SITE_GIT_REPO_ROOT}/$2"
+	repo_path_head="$1/${2}/head.html.tmp"
+	repo_path_log="$1/${2}/log.html.tmp"
+	repo_path_readme="$1/${2}/readme.html.tmp"
+	repo_path_license="$1/${2}/license.html.tmp"
+	repo_name="${2%.*}"
+	repo_description="$(< "${1}/${2}"/description html_encode)"
+
+	rsync -a --delete-before "${repo_path}/" "$1/${2##*/}"
 	git -C "$1/${2##*/}" --bare update-server-info
 	ln -s "refs" "$1/${2##*/}/info/refs?service=git-upload-pack"
 
@@ -433,8 +435,8 @@ render_git_repo() {
 		-v var_site="$(awk_safe "${1}")"                                    \
 		-v var_repo="$(awk_safe "${2}")"                                    \
 		-v var_logo="$(awk_safe "${SITE_ICON_SVG}")"                        \
-		-v var_name="$(awk_safe "${_name}")"                                \
-		-v var_description="$(awk_safe "${_description}")"                  \
+		-v var_name="$(awk_safe "${repo_name}")"                                \
+		-v var_description="$(awk_safe "${repo_description}")"                  \
 		-v var_menu="$(
 			git -C "$1/${2}" show HEAD:README >/dev/null 2>&1 ||            \
 			git -C "$1/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&           \
@@ -454,7 +456,7 @@ render_git_repo() {
 			gsub(/\{:menu:\}/, var_menu)
 			print
 		}
-	' "${CFG_GIT_HEAD_HTML}" > "${_path_head}"
+	' "${CFG_GIT_HEAD_HTML}" > "${repo_path_head}"
 
 	# render log
 	git -C "$1/${2}" log                                                    \
@@ -488,7 +490,7 @@ render_git_repo() {
 			printf "\t\t</tbody>\n"
 			printf "\t</table>\n"
 		}
-	' > "${_path_log}"
+	' > "${repo_path_log}"
 
 	# render readme if existing
 	git -C "$1/${2}" show HEAD:README >/dev/null 2>&1 &&                    \
@@ -506,7 +508,7 @@ render_git_repo() {
 		END {
 			print "</pre>"
 		}
-	' > "${_path_readme}" &&                                                \
+	' > "${repo_path_readme}" &&                                                \
 	ln -s "readme.html" "$1/${2}/index.html"
 
 	# render license if existing
@@ -525,37 +527,37 @@ render_git_repo() {
 		END {
 			print "</pre>"
 		}
-	' > "${_path_license}"
+	' > "${repo_path_license}"
 
 	[ ! -h "$1/${2}/index.html" ] && ln -s "log.html" "$1/${2}/index.html"
 }
 
 render_git() {
-	local _repos=
-	local _forks=
-	local _buffer=
-	local _pp=
-	local _owner=
-	local _description=
-	local _index="${1}/index.html"
-
-	for repo in ${SITE_GIT_REPO_ROOT}/*; do
-		_pp="${repo##*/}"
-		_pp="${_pp%.*}"
-		print_loading "Rendering repo ${_pp}"
-		_owner="$(cat "${repo}"/owner | html_encode)"
-		_description="$(cat "${repo}"/description | html_encode)"
-		_buffer=$(
+	git_repos=
+	git_forks=
+	git_buffer=
+	git_pp=
+	git_owner=
+	git_description=
+	git_index="${1}/index.html"
+
+	for repo in "${SITE_GIT_REPO_ROOT}"/*; do
+		git_pp="${repo##*/}"
+		git_pp="${git_pp%.*}"
+		print_loading "Rendering repo ${git_pp}"
+		git_owner="$(< "${repo}"/owner html_encode)"
+		git_description="$(< "${repo}"/description html_encode)"
+		git_buffer=$(
 			printf '<tr><td><a href="%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td></tr>' \
 				"//${1}/${repo##*/}"                                        \
-				"${_pp}"                                                    \
-				"${_description}"                                           \
-				"${_owner}"                                                 \
+				"${git_pp}"                                                    \
+				"${git_description}"                                           \
+				"${git_owner}"                                                 \
 				"$(git -C "${repo}" log -1 --date=format:"%Y-%m-%d %T" --format=%ad)")
 
 		[ -f "$repo/fork" ]                                                 \
-			&& _forks="$(printf '%s%s' "${_forks}" "${_buffer}")"           \
-			|| _repos="$(printf '%s%s' "${_repos}" "${_buffer}")"
+			&& git_forks="$(printf '%s%s' "${git_forks}" "${git_buffer}")"           \
+			|| git_repos="$(printf '%s%s' "${git_repos}" "${git_buffer}")"
 
 		render_git_repo "${1}" "${repo##*/}"
 		print_done
@@ -567,8 +569,8 @@ render_git() {
 		-v var_favicon="$(awk_safe "${SITE_ICON_PNG}")"                     \
 		-v var_stylesheet="$(awk_safe "${SITE_CSS}")"                       \
 		-v var_logo="$(awk_safe "${SITE_ICON_SVG}")"                        \
-		-v var_repos="$(awk_safe "${_repos}" | sed 's/<tr>/\n\0/2g')"       \
-		-v var_forks="$(awk_safe "${_forks}" | sed 's/<tr>/\n\0/2g')"       \
+		-v var_repos="$(awk_safe "${git_repos}" | sed 's/<tr>/\n\0/2g')"       \
+		-v var_forks="$(awk_safe "${git_forks}" | sed 's/<tr>/\n\0/2g')"       \
 	'
 		/\{:repos:\}/{
 			padding=$0
@@ -592,15 +594,17 @@ render_git() {
 			gsub(/\{:forks:\}/, var_forks)
 			print
 		}
-	' "${CFG_GIT_REPOS_HTML}" > "${_index}"
+	' "${CFG_GIT_REPOS_HTML}" > "${git_index}"
 
 	# complete html-files from html.tmp files
-	find "${1}" -mindepth 2 -type f -name "*.html.tmp" | while read file; do
-		[ ${file##*/} = "head.html.tmp" ] && continue
-		awk '1;/<body>/{exit}' "${_index}" > "${file%.*}"
-		cat "${file%/*}/head.html.tmp" >> "${file%.*}"
-		cat "${file}" >> "${file%.*}"
-		awk '/<\/body>/,0' "${_index}" >> "${file%.*}"
+	find "${1}" -mindepth 2 -type f -name "*.html.tmp" | while read -r file; do
+		[ "${file##*/}" = "head.html.tmp" ] && continue
+		{
+			awk '1;/<body>/{exit}' "${git_index}"
+			cat "${file%/*}/head.html.tmp"
+			cat "${file}"
+			awk '/<\/body>/,0'
+		} > "${file%.*}"
 	done
 
 	# remove all html.tmp files
@@ -608,9 +612,9 @@ render_git() {
 }
 
 render_git_commits() {
-	local _tmp_file="$(mktemp "${TMPDIR:-/tmp/}$(basename $0).repolog.XXXXXX")"
+	_tmp_file="$(mktemp "${TMPDIR:-/tmp/}$(basename "$0").repolog.XXXXXX")"
 
-	for repo in $SITE_GIT_REPO_ROOT/*; do
+	for repo in "${SITE_GIT_REPO_ROOT}"/*; do
 		a=${repo##*/}
 		a="<a href=\"//git.${1}/${a}\">${a%.*}</a>"
 		git -C "${repo}" log -n "${SITE_GIT_COMMIT_COUNT}"                  \
@@ -654,7 +658,7 @@ render_git_commits() {
 }
 
 generate_sitemap() {
-	local _sitemap="${1}/pub/sitemap.xml"
+	_sitemap="${1}/pub/sitemap.xml"
 
 	awk                                                                     \
 		-v var_items="$(
@@ -684,10 +688,11 @@ main() {
 	fi
 
 	# make sure both arguments, if existing, are directories
-	[ ! -z "$1" ] && [ ! -d "$1" ] && return
-	[ ! -z "$2" ] && [ ! -d "$2" ] && return
+	[ -n "$1" ] && [ ! -d "$1" ] && return
+	[ -n "$2" ] && [ ! -d "$2" ] && return
 
 	# main render
+	# TODO : FIX
 	[ -z "$2" ]                                                             \
 	&& find "$1" -type d                                                    \
 		-exec test -e '{}/index.www' \;                                     \
@@ -707,7 +712,7 @@ main() {
 		# convert absolute paths to relative, so that they work with IPFS
 		print_loading "Converting absolute paths to relative"
 		for site in "$1" "git.${1}"; do
-			find "${site}" -type f -name "*.html" | while read line; do
+			find "${site}" -type f -name "*.html" | while read -r line; do
 				count="$(echo "$line" | tr -cd '/' | wc -c)"
 				sed -i "s,\"//\(${site}\|\[site\]\)/,\"$(
 						seq -f "../%g" -s '' 2 "$count" | sed 's/[0-9]//g'