# shellcheck shell=bash

# QA checks for OpenRC init.d files.

openrc_check() {
	# Sanity check syntax errors in init.d scripts
	local d i
	for d in /etc/conf.d /etc/init.d ; do
		[[ -d ${ED}/${d} ]] || continue
		for i in "${ED}"/${d}/* ; do
			[[ -L ${i} ]] && continue
			# if empty conf.d/init.d dir exists (baselayout), then i will be "/etc/conf.d/*" and not exist
			[[ ! -e ${i} ]] && continue
			if [[ ${d} == /etc/init.d && ${i} != *.sh ]] ; then
				if ! read -r < "${i}" ; then
					continue
				elif [[ ${REPLY} != '#!'*[[:blank:]/]@(openrc-run|runscript|sh) ]] ; then
					# skip non-shell-script for bug #451386
					continue
				elif [[ ${REPLY} == '#!/sbin/runscript' ]] ; then
					eqawarn "QA Notice: #!/sbin/runscript is deprecated, use #!/sbin/openrc-run instead:"
					while read -r ;
						do eqawarn "   ${REPLY}"
					done <<< "${i//${ED}}"
				fi
			fi
			bash -n "${i}" || die "The init.d file has syntax errors: ${i}"
		done
	done

	local checkbashisms=$(type -P checkbashisms)
	if [[ -n ${checkbashisms} ]] ; then
		d=/etc/init.d
		if [[ -d ${ED}${d} ]]; then
			for i in "${ED}${d}"/* ; do
				[[ -e ${i} ]] || continue
				[[ -L ${i} ]] && continue
				f=$("${checkbashisms}" -n -f "${i}" 2>&1)
				[[ $? != 0 && -n ${f} ]] || continue
				eqawarn "QA Notice: OpenRC shell script appears to use non-POSIX feature(s):"
				while read -r ;
					do eqawarn "   ${REPLY}"
				done <<< "${f//${ED}}"
			done
		fi
	fi
}

openrc_check
: # guarantee successful exit

# vim:ft=bash
