User:Scsbot/examples/addrhyme

file="$1" rhyme="$2" rhymebase="Rhymes:English:"

if test -z "$file" -o -z "$rhyme" then echo "arg count" &gt;&amp;2 exit 1 fi

lang="English"


 * 1) 1. find English section


 * 1) 1a. Find level-2 headers

l2hdrs=`grep -n '^== *[^=][^=]*==' $file`

if test -z "$l2hdrs" then echo "no level 2 (language) headers" exit 1 fi

nl=`echo "$l2hdrs" | grep -ic "^[1-9][0-9]*:== *$lang *=="`

if test $nl -ne 1 then echo "article has $nl sections for $lang" exit 1 fi


 * 1) find end of English section --
 * 2) either next level-2 header, or EOF

li=`echo "$l2hdrs" | grep -n "^[1-9][0-9]*:== *$lang *==" | sed 's/:.*//'` # line in $l2hdrs

el=`echo "$l2hdrs" | grep "^[1-9][0-9]*:== *$lang *==" | sed 's/:.*//'`   # line in $file

nl2=`echo "$l2hdrs" | wc -l`

if test $li -lt $nl2 then li2=`expr $li + 1` eoe=`echo "$l2hdrs" | line $li2 | sed 's/:.*//'` eoe=`expr $eoe - 1` else eoe=`wc -l &lt; $file` fi

# now grab pronunciation lines between


 * 1) 2. Find Pronunciation header(s)

prohdrs=`line -f $el -t $eoe $file | grep -n '^==* *Pronunciation *='`

needprohdr=no

if test -z "$prohdrs" then echo "adding Pronunciation section" &gt;&amp;2 needprohdr=yes elif test `echo "$prohdrs" | wc -l` -gt 1 then echo "multiple Pronunciation sections in $lang section" exit 1 fi

if test $needprohdr = no then


 * 1) now find next header (any level) after target pronunciation header

proline=`expr "$prohdrs" : '\([^:]*\)'`

nxthdr=`line -f $el -t $eoe $file | grep -n '^=' | awk -F: "\\$1 &gt; $proline {print; exit}"`

if test -n "$nxthdr" then nxtline=`expr "$nxthdr" : '\([^:]*\)'` else nxtline=`expr $eoe - $el + 1 + 1` fi

proline=`expr $el + $proline - 1` nxtline=`expr $el + $nxtline - 1`


 * 1) echo "pronunciation line: $proline"
 * 2) echo "next header line: $nxtline"


 * 1) now count blank lines preceding $nxtline

i=`expr $nxtline - 1` while test $i -gt $proline do if line $i $file | grep -q. then	break fi i=`expr $i - 1` done

else

# If there's an Etymology section, insert after that. # Otherwise insert at beginning of English section.

etyhdr=`line -f $el -t $eoe $file | grep -n '^==* *Etymology' | head -1`

if test -n "$etyhdr" then etyline=`expr "$etyhdr" : '\([^:]*\)'` firstline=$etyline else firstline=1	# in $el space fi

# find next header (any header)

nxthdr=`line -f $el -t $eoe $file | grep -n '^=' | awk -F: "\\$1 &gt; $firstline {print; exit}"`

if test -n "$nxthdr" then nxtline=`expr "$nxthdr" : '\([^:]*\)'` else nxtline=`expr $eoe - $el + 1 + 1` fi

nxtline=`expr $el + $nxtline - 1`

i=`expr $nxtline - 1`

# now count blank lines preceding $nxtline

needtrailing=yes

while test $i -gt $firstline do if line $i $file | grep -q. then	break fi needtrailing=no i=`expr $i - 1` done fi


 * 1) now $i is line to insert after

nins=1 if test $needprohdr = yes then nins=`expr $nins + 2` if test "$needtrailing" = yes then	 nins=`expr $nins + 1` fi fi

( echo ${i}a

if test $needprohdr = yes then echo echo "===Pronunciation===" fi

echo "*:Rhymes: $rhyme"

if test $needprohdr = yes -a "$needtrailing" = yes then	 echo fi

echo. echo w ) | ed - $file

echo "expect $nins insertions"

exit 0