#!/bin/sh
#] 
#] *********************
#] $ bash "$d_bin""txtDoc insert indexes.sh"  - 
# www.BillHowell.ca  23Oct2019 initial

# do NOT run this script directly - set up a calling file
# must define many variables before calling
# example - Lucas project, see "$d_bin""Lucas txtDoc header.sh"


#**********************************


# relineNum - re-line-number a list (eg Table of Contents, List of equations)
# 23Oct2019 - this fails if ":" is also in the line read from file

#] reLineNum()  - 

	reLineNum()
{  
IFS=''
while read -u 7 line; do
	let "lineNum=$( echo $line  |  sed  's/^\([0-9]\+\):.*/\1/' )"
	let "lineNum_new= $lineNum + $2 "
	lineBas=$( echo "$line"  |  sed  's/.*\(:.*\)/\1/' )
	lineNew="$lineNum_new$lineBas"
	echo "$lineNew"   >>"$3"
done 7< "$1" 
}  


#tests : 
# echo "4037:    Lucas 5 - Extension of the Universal Force Law to include acceleration"  |  sed  's/^\([0-9]\+\):.*/\1/'
# echo "4037:    Lucas 5 - Extension of the Universal Force Law to include acceleration"  |  sed  's/.*\(:.*\)/\1/'
#


#] txtDoc_insert_indexes()  - 

	txtDoc_insert_indexes()
{ 
IFS=''
while read -u 6 line; do
	echo  "$line"  >>"$4"
		flag=$( echo "$line"  |  grep  "$3" )
		if	 	[ -n "$flag" ]; then
			flag1=$( echo "$line"  |  sed 's#^\/\*##' )
			if	 	[ -n "$flag1" ]; then
				cat  "$2"   >>"$4"
			fi
		fi
done 6< "$1" 
}

#stopper="no"
#while read -u 6 line; do
#	echo  "$line"  >>"$p_augmented_final"
#	if  [[ -n  "$stopper" ]]; then 
#		flag=$( echo "$line"  |  grep  "$3" )
#		if	 	[ -n "$flag" ]; then
#			cat  "$2"   >>"$p_augmented_final"
#		fi
#		stopper=""
#	fi
#done 6< "$1" 


#] insert_indexes()  - 

	insert_indexes()
{  
p_headings_init="$d_temp""headings init.txt"
p_equatins_init="$d_temp""equatins init.txt"
p_headings_done="$d_temp""headings done.txt"
p_equatins_done="$d_temp""equatins done.txt"
p_augmented_1="$d_temp""p_augmented_1.txt"
p_augmented_final="$d_augmented$f_augmented"

p_formulae="d_inn""formulae- $f_augmented"

# clear the output files to make sure that we are not piling new in the old.
rm -f "$p_headings_init"
rm -f "$p_equatins_init"
rm -f "$p_headings_done"
rm -f "$p_equatins_done"
rm -f "$p_augmented_1"  

# Default - extract the first 3 levels of headings only
 grep -n  "^\(>>>\)\{1,3\} " "$p_augmented"  |  sed  's/>>>/   /g'  >>"$p_headings_init"
# All headings : "
#grep -n  "^>>>" "$p_augmented"  |  sed  's/>>>/   /g'   >>"$p_headings_init"
# Extract all 'add_eqn' results (one line only)
 grep -n  "\/\*------> " "$p_augmented"  |  sed  's/\/\*------> /   /g'  >>"$p_equatins_init"

n_headings=$( wc -l  <"$p_headings_init"  )
n_equatins=$( wc -l  <"$p_equatins_init"  )
let "n_total= $n_headings + $n_equatins "
#echo "n_headings= $n_headings; n_equatins= $n_equatins; n_total= $n_total"

reLineNum  "$p_headings_init"  "$n_total"  "$p_headings_done"
reLineNum  "$p_equatins_init"  "$n_total"  "$p_equatins_done"

txtDoc_insert_indexes  "$p_augmented"	  "$p_headings_done"  '/*_Insert_Table_of_Contents'	"$p_augmented_1"
txtDoc_insert_indexes  "$p_augmented_1"  "$p_equatins_done"  '/*_Insert_equations'				"$p_augmented_final"

#extract_formulae
} 




# enddoc
