issue on dynamic config script

I have one issue with my existing code.Actually i want to write parameter's at the end of the section not begin of section.please find my code snapshots...

while read cfgfilename sectionname parameter
do
  case "$cfgfilename" in
    cfgfilename)
      FILE=$HOME/config/$parameter
      unset SECT
      [-f $FILE] || {echo -e $FILE is not found under $CFGFILEPATH directory...} | tee -a $LOGFILE
    ;;
    sectionname)
      [-z $FILE] && continue
      SECT="$parameter"
      while true
      do
        N=$(grep -Fwnx "$SECT" $FILE | cut -d ':' -f1)
        ((N)) && break
        echo -e $SECT section not found in $FILE ,adding section | tee -a $LOGFILE
        echo -e "$SECT\n" >> $FILE
      done
    ;;
    *)
      if [-n "$FILE"] && [-n "$SECT"]
      then
        ((N++))
        sed -i"{$N}" 'i\' "$cfgfilename $sectionname $parameter" $FILE

Actually abobe code take cfgfilename and section name and patrameter from a input file and check in config director for specific cfgfilename if found take sectionnmae and searcg in cfgfilename ,if section found it's simply added parameter's begin of section,and if not found then add it at end of file along with parameter's.
Actually i want,if section is found in existing filename,then it should write at end of section not the begn...like example..
common.cfg

[section1]
p1
p2
p3
[section2]
p4
p5
p6

If i want to add parameter's in common.cfg file's section1,it shoul write after parameter p3 not before p1.

Please help me..
thnks
surya

This doesn't look like all your code... where are you actually writing the new settings to the file?