#!/bin/sh
#] 
#] *********************
#] $ bash "$d_bin"'review format.sh' 
# www.BillHowell.ca  17Apr2025 initial for my reviews
# view in text editor, using constant-width font (eg courier), tabWidth = 3


#24************************24




#24************************24
# List of operators, generated with :
# $ grep  "^#]"  "$d_bin"'review format.sh' |  sed "s/^#\]/  /" 

#



#08********08
# Setup 

source  "$d_bin"'standard header.sh'
source  "$d_bin"'strings.sh'

d_review="$d_PROJECTS"'My Reviews/'
d_revTst="$d_bin"'0_test/reviews/'

colMax=80


#08********08
# code 

#] str_cutAtLastWhitespace_str1_str2()  - cut str at last whitespace within non-whitepace

	str_cutAtLastWhitespaceBeforeColmax_str1_str2()
{
	local colLst=''
	local colFst=''
	local strLen=''
	local str3=''

	line="$1"

	str1=$( echo  "$line"  |  cut -c  1-"$colMax" )
	strLen=$( str_get_length  "$line" ) 
	#	echo  "str1In= $str1"
	#	echo  "strLen= $strLen"

	# deetmine if whitespace across strMaxx
	let "colFst = $colMax - 1" 
	chrLst=$( echo  "$str1"  |  cut -c  "$colMax"-"$colMax" )
	chrFst=$( echo  "$str1"  |  cut -c  "$colFst"-"$colFst" )
	chrBoth="$chrLst$chrFst"

	bolWhit=$( echo  "$chrBoth"  |  grep "[ 	]" )
	if  [ "$bolWhite" == '' ]; then 
		# 18Apr2025 problem if no spaces [after initial [ \t], before strEnd] !!
		str3=$( echo  "$str1"  |  sed 's|\(.*\)[ ].*|\1|'  )
		colLst=$( str_get_length  "$str3" ) 
		let  "colLst = $colLst + 2"
		str2=$( echo  "$line"  |  cut -c  "$colLst"-"$strLen"  )
		str2="$indent$str2"
		str1="$str3"
			echo  "str1= $str1"
			echo  "str2= $str2"
	else
		let "colLst = $colMax + 1"
		colLst=$( str_get_length  "$str1" ) 
		str1=$( echo  "$str1"  |  cut -c  1        -"$colLst" )
		str2=$( echo  "$str1"  |  cut -c  "$colLst"-"$strLen"  )
		str2="$indent$str2"
			echo  "str1= $str1"
			echo  "str2= $str2"
	fi
}

#	12h59m 18Apr25$ echo  '#] 0_test/reviews/simple test.txt  - test for'  |  sed 's|\(.*\) .*|\1|' 
#	#] 0_test/reviews/simple test.txt  - test
#	~


#] pinn_reviewFormat_pout()  -  tabs-to-3spaces, 65 chrs-per-line

	pinn_reviewFormat_pout()
{
	local pinn=''
	local pout=''
	local strLen=''

	pinn="$1"
	pout="$2"
	if  [ -f  "$pout" ]; then  rm  "$pout";  fi

	pTmp="$d_temp"'review_format result.txt'
	sed  's|\-	|\-  |;s|\t|   |g'  "$pinn"  >"$pTmp"

	while IFS='' read -r line; do
		indent=$( echo  "$line"  |  sed 's|^\([ ]*\).*|\1|'  )
		if  [ "$indent" == '' ]; then 
			firstChrs=$( echo  "$line"  |  grep  "^#\] \|^p[0-9]\+L[0-9]*\|^p[0-9]\+c[0-9]h[01]\.[0-9]\+*\|^>> \|^\-  " )
			if  ! [ "$firstChrs"  ==  '' ]; then  indent='   ';  fi
		fi
		strLen=$( str_get_length  "$line" ) 
		if [ "$strLen"  -ge  "$colMax" ]; then  
			str_cutAtLastWhitespaceBeforeColmax_str1_str2  "$line"
			echo  "$str1"  >>"$pout"
			line="$str2"
			bolStrLen=1
			while [ "$bolStrLen" == 1 ];  do  
				#strLen=$( str_get_length  "$indent$line" ) 
				strLen=$( str_get_length  "$line" ) 
					echo  "strLen= $strLen"
				if [ "$strLen"  -ge  "$colMax" ]; then  
					str_cutAtLastWhitespaceBeforeColmax_str1_str2  "$line"
					echo  "$str1"  >>"$pout"
					line="$str2"
				else  
					#echo  "$indent$line"  >>"$pout"
					echo  "$line"  >>"$pout"
					bolStrLen=0 
				fi
			done
		else 
			echo  "$line"  >>"$pout"
		fi
	done <"$pTmp"
}





#08********08
# Procedures -  To run :



#08********08
# run - select one of options

#	reviewBase='simple test'
#	pinn_reviewFormat_pout  "$d_revTst$reviewBase"'.txt'  "$d_revTst$reviewBase"' formatted.txt'

	reviewBase='NCAA-D-25-00100R1 r anon AViAMS: Brain-inspired Audio-Visual Associative Memories using Spiking Neural Networks'
	pinn_reviewFormat_pout  "$d_review$reviewBase"'.txt'  "$d_review$reviewBase"' formatted.txt'


# 17Apr2025 problem with ramdisk owner? (not yesterday!)
# cp  "$d_PROJECTS"'My Reviews/NCAA-D-25-00100R1 r anon AViAMS: Brain-inspired Audio-Visual Associative Memories using Spiking Neural Networks.txt'  "$d_temp"'NCAA-D-25-00100R1 r anon AViAMS: Brain-inspired Audio-Visual Associative Memories using Spiking Neural Networks.txt'




# $ bash  "$d_bin"'review format.sh'  

# enddoc

