#!/bin/bash
#
# Copyright, the authors of the Linux man-pages project
# SPDX-License-Identifier: GPL-3.0-or-later

set -Eefuo pipefail;


# Defaults:
t='false';
t_r='false';
t_v='false';


grepc_err()
{
	>&2 printf '%s\n' "$(basename "$0"): error: $*";
	exit 1;
}


while getopts "t:" opt; do
	case "$opt" in
	t)
		case "$OPTARG" in
		r)	t_r='true';	;;&
		v)	t_v='true';	;;&
		[rv])
			t='true';
			;;
		*)
			grepc_err "-$opt: $OPTARG: Unknown argument.";
			;;
		esac;
		;;
	\? | *)
		exit 1;
		;;
	esac;
done;
shift $((OPTIND-1));

if test $# -lt 1; then
	grepc_err "Missing identifier.";
fi;
identifier="$1";
shift;

if test $# -gt 0; then
	grepc_err "$1: Unexpected argument.";
fi;

if ! "$t"; then
	t_v='true';
fi;


grepc_mk_r()  { echo '(?s)^(\$\()?'"$1"'\)?\s*:[^=].*?(?<!\\)$(?:(?!^[^\t]).)*'; }
grepc_mk_v()  { echo '(?s)^'"$1"'\s*[:?+]*=.*?(?<!\\)$'; }


if "$t_r";  then grepc_mk_r "$identifier";  fi;
if "$t_v";  then grepc_mk_v "$identifier";  fi;
