#!/bin/sh
# test platform ... devuan dash
# GPL_3+ license
cat << 'EEE' > /dev/null
/* dbg .... repl dbg helper as C-lang debag macro. bourne-shell script.
 * Copyright (C) 2017 Momi-g
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
EEE

# setting
dbgflg="dbg"	# dbg aaa $*	... # DG a b  ... dbgflg="DG"
fullpath=1	# 0 is name only.  /home/aaa/bbb.txt  or  bbb.txt
func_dbg="dbg_27dd"		#dbg_27dd() { ...	# use uniqe name in your script.






# ---usage
# source code flg .. # dbg aaa $@ --msg bbb 
# ~$ dbg ./aaa.sh -a 111 -b 333 
# del flg: ~$dbg -D ./aaa.sh

# ---help
if [ "$#" -eq 0 ] || [ "$1" = '-h' ] ; then
cat << 'EEE'
HowTo (repl dbg helper as C-lang debag macro. bourne-shell script.)
option: -h (this), -D (del)
------
eg.)     #!/bin/sh
         a=123; g=000; set -- 1 2 3 "$@"
               # dbg a "g" --hello,world '$@'		
         echo "foo" ; typo

 ~$ dbg aaa.sh -h
 >>>  ### DBG 3+30:/home/aaa.sh// hello,world $a=123  $g=000  $@=1 2 3 -h 
     foo + err 34:typo
 
     3+30: '3' is line num of '#dbg'. +30 is amount of add code.(err 34-30=4)
    ...//: filename. write 'fullpath=0/1' to 'dbg' source directly.

- dbg marking format...		# dbg a g --hello,world $@		
   '#'  : 1 char. #dbg #(\t)dbg etc
   'dbg': you can use other string(DBG,DG,debag etc). change source.
   vars : a/'a'/"a"/$a/'$a'/"$a"	is treated as varname 'a'.
   --msg: comment. you cant use space or tab. "--msg"/'--msg' is invalid.

 ~$ dbg -D aaa.sh   >>> del all '# dbg' line from aaa.sh directly. (sed -i)
EEE
exit 0
fi



# del #dbg line
if [ "$1" = "-D" ] ; then
	shift
	sed -ie '/^[ \t]*#[ \t]*dbg/d' "$1"
exit
fi
# ignore
if [ "$1" = "--" ] ; then
	shift
fi



safe=`cat << 'EEE'
s/'/'"'"'/g
1 s/^/'/
$ s/$/'/
EEE
`

#	dmy'`

# file only
if [ -d "$1" ] ; then
	echo "$0: file not found" >/dev/stderr
	exit 1
fi

# get fullpath
buf=${1#*/}
if [ "$1" = "$buf" ] ; then
	raw=`pwd -P ; printf @`
	raw="${raw%??}""/$1"
else
	bk=`pwd -P ; printf @`
	bk="${raw%??}"

	buf=${1%/*}
	cd "$buf"
	buf=`pwd -P ; printf @`
	raw="${buf%??}""/${1##*/}"
	cd "$bk"
fi

if [ "$fullpath" = "0" ] ; then
	raw=${raw##*/}
fi

file="$raw"
shift

#--- opt end



add="$func_dbg"'() {'
add="$add"$(cat << 'EEE'

	if [ "$1" = "-c" ] ; then
		shift
		echo $#		# "$aaa" = 1
		return 0
	fi

(
	file="$1"
	lno="$2"
	fat="$3"
	msg="$4"
	shift 4		
	printf "### DBG %s+%s:%s// %s" "$lno" "$fat" "$file" "$msg"
	
	while [ $# -gt 0 ]		# ヘッダ以降。 $(dbg_27dd -c "$aa") 'aa' "$aa"
	do
		num=$1
		shift
		printf ' $%s=' "$1"		#shift num 'aaa' "$aaa" ... 2
		shift
		for ii in $(seq 1 $num)
		do
			printf "%s " "$1"
			shift
		done
	done
	echo
) >/dev/stderr
}
EEE
)

#---main
fat=`printf '%s\n' "$add" | wc -l`

#	source ... # dbg aaa * --unko
#	>>>  ... dbg lno23 fat40 commemt_unko  +  '$aaa' '$*'
#	>>>  ... dbg lno...unko +  `dbg -c "$aaa"` 'aaa' "$aaa"  // + (*)  ...繰り返し
#	
#	-cは$@対策。可変なのでカウント機能を。
#	コメント処理が面倒くさい。raw -- 以外はキック。"--asdf" . '--eee' とかはinvalid。

rexp='^[ \t]*#[ \t]*'"$dbgflg"'[ \t]'		# grep '# dbg' 

buf=$(cat "$raw" | 
awk -v add="$add" -v file="$file" -v fat="$fat" -v dbgflg="$dbgflg" -v func_dbg="$func_dbg" '
BEGIN {
	ere="^[ \t]*#[ \t]*" dbgflg "[ \t]"
	qu="\047"
	dqu="\042"
}

NR == 2 {print add}
$0 ~ ere {
	sub(ere, "", $0)
	
	# msg err
	for(i=1;i<=NF;i++){
		arrnum=split($i, arr, "--")
		if( arrnum > 1 && arr[1] != ""){
			$0 = dbgfls " --###_invalid_format_(msg_must_start_with_--)"
		}
	}
	
	# normalize			# a $a 'a' '$a' "$a  " "a" -> all simple a
	msg=""
	for(i=1;i<=NF;i++) {
		arrnum=split($i, arr, "--")
		if( arrnum != 1 ){
			sub(/--/, "", $i)
			msg=msg $i
			$i=""
			# ... grep msg. del
		} else {
			gsub(dqu, "", $i)
			gsub(qu, "", $i)
			gsub(/[$]/, "", $i)	# ...char '$'
			
			#  aaa >>>
			#  `dbg -c "$aaa"` 'aaa' "$aaa"
			$i=sprintf ("$(%s -c \042$%s\042) \047%s\047 \042$%s\042", func_dbg, $i, $i, $i  )
		}
	}

	# msg quote
	gsub(qu, dqu qu dqu, msg)
	msg= qu msg qu
	gsub(qu, dqu qu dqu, file)
	file= qu file qu

	
	#... dbg lno23 fat commemt -- (`dbg -c "$aaa"` 'aaa' "$aaa") , (2), ...
	$0=sprintf("%s %s %s %s %s   %s", func_dbg, file, NR, fat, msg,  $0 )
}
{print $0}'
)

#echo "fat_$fat"
#echo "$buf"
#exit

sh -c -- "$buf" "$raw" "$@"
