#! /usr/bin/ksh

if [ $# -lt 1 ]   
then
    print "USAGE: conv_config conf_file_to_be_converted"
    exit
fi

# now the real thing   
/usr/bin/awk  '
BEGIN  { comment_line = 0 
         listen_line = 0
         announce_line = 0
	 rip_line = 0
	 hello_line = 0
	 redirect = 0
}

# copy all the comment lines without interpreting them
substr($1,1,1) == "#" || substr($1, 1, 2) == "\*"   {
      print $0 
      comment_line = 1
}

# search for the interface statements
$1 ~ /[Ii][Nn][Tt][Ee][Rr][Ff][Aa][Cc][Ee]/  {
      if (/[Me][Ee][Tt][Rr][Ii][Cc]/ ||  \
          /[Pp][Rr][Ee][Ff][Ee][Rr][Ee][Nn][Cc][Ee]/ || \
          /[Pp][Aa][Ss][Ss][Ii][Vv][Ee]/ )  {
          new_str = tolower ($0)
          split (new_str, parm, " ")
	  split ($0, parm1, " ")
          sub ( /[Ii][Nn][Tt][Ee][Rr][Ff][Aa][Cc][Ee]/, "interfaces { \n    interface ") 
	  if (/[Mm][Ee][Tt][Rr][Ii][Cc]/ ) {
	      for (i =1; i <= NF; ++i)   
         	   if (parm[i] == "metric" )
		       break
           sub (parm1[i],"")
           sub (parm1[++i],"")
          }
	  sub ( /;/, ";\n};")
      }
}

# if rip_line is set and there is no more statements for RIP, just
# add an closing bracket.

{  if (rip_line == 1)  { 
       if ($1 ~ /;/ )  
         printf ("} ;\n")
       rip_line = 0
    }
}

# if hello_line is set and there is no more statements for Hello, just
# add an closing bracket.

{  if (hello_line == 1)  { 
       if ($1 ~ /;/ ) 
         printf ("} ;\n")
       hello_line = 0
    }
}

# search for rip protocol statements 
$1 ~ /[Rr][Ii][Pp]/ && comment_line == 0  {
           if ($2 == "quiet" || $2 == "pointopoint" || $2 == "supplier") {
	       printf ("%s ", $1)
	       if ($2 == "quiet") {
		   printf ("yes  {\n")
		   printf ("	nobroadcast ;\n")
               }
	       else if ($2 == "pointopoint") {
		   printf ("yes  {\n")
		   printf ("	nobroadcast ;\n")
               }
	       else if ($2 == "supplier") {
		   printf ("yes  {\n")
		   printf ("	broadcast ;\n")
               }
           if ($NF == ";")
 	       printf ("} ;\n")
           else  
               rip_line = 1 
        }
        else 
               print $0
        comment_line = 1
}


# search for hello protocol statements 
$1 ~ /[Hh][Ee][Ll][Ll][Oo]/ && comment_line != 1  {
           if ($2 == "quiet" || $2 == "pointopoint" || $2 == "supplier") {
               printf ("%s ", $1)
	       if ($2 == "quiet") {
		   printf ("yes  {\n")
		   printf ("	nobroadcast ;\n")
               }
	       else if ($2 == "pointopoint") {
		   printf ("yes  {\n")
		   printf ("	nobroadcast ;\n")
               }
	       else if ($2 == "supplier") {
		   printf ("yes  {\n")
		   printf ("	broadcast ;\n")
               }
	       if ($NF == ";")
		   printf ("} ;\n")
               else
                   hello_line = 1 
            }
            else
                   print $0
            comment_line = 1
}

# substitue noicmpin to noredirects
{  sub ( /[Nn][Oo][Ii][Cc][Mm][Pp][Ii][Nn]/, "noredirects") }

{ if ( redirect == 1 )
     if ( ($NF == ";" && $(NF-1) == "}") || $NF == "};" )
          redirect = 0
}

{ if (/[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]/ )
      if ( $NF != ";" || $NF == "{" )
           redirect = 1
}

# make necessary conversion changes for the statements in the egp protocol
{ sub ( /[Aa][Ss][Ii][Nn]/, "peeras")  }
{ sub ( /[Aa][Ss][Oo][Uu][Tt]/, "localas")  }

{ if (/[Ii][Nn][Tt][Ee][Rr][Ff][Aa][Cc][Ee]/ && NF == 2 && $2 != "{"  && redirect == 0)
  sub (/[Ii][Nn][Tt][Ee][Rr][Ff][Aa][Cc][Ee]/, "lcladdr")
}

# replace the string of accept to import
{ sub ( /[Aa][Cc][Cc][Ee][Pp][Tt]/, "import") }

# change the acceptance_list to import_list
$1 ~ /[Nn][Oo][Ll][Ii][Ss][Tt][Ee][Nn]/   {
     sub ( /[Nn][Oo][Ll][Ii][Ss][Tt][Ee][Nn]/, "") 
     if ( /[Pp][Rr][Ee][Ff][Ee][Rr][Ee][Nn][Cc][Ee]/)
          sub (/[Pp][Rr][Ee][Ff][Ee][Rr][Ee][Nn][Cc][Ee]/, "restrict preference")
     else if ( /;/ )
          sub ( /;/, "restrict ;") 
     listen_line = 1
}

$1 ~ /[Ll][Ii][Ss][Tt][Ee][Nn]/  { 
   if ( listen_line != 1 )  sub ( /[Ll][Ii][Ss][Tt][Ee][Nn]/, "") }

# change the announce_list 
$1 ~ /[Nn][Oo][Aa][Nn][Nn][Oo][Uu][Nn][Cc][Ee]/   {
     sub ( /[Nn][Oo][Aa][Nn][Nn][Oo][Uu][Nn][Cc][Ee]/, "") 
     if ( /[Mm][Ee][Tt][Rr][Ii][Cc]/ )
          sub (/[Mm][Ee][Tt][Rr][Ii][Cc]/, "restrict metric")
     else if ( /;/ )
          sub ( /;/, "restrict ;") 
     announce_line = 1
}

$1 ~ /[Aa][Nn][Nn][Oo][Uu][Nn][Cc][Ee]/   {
     if ( announce_line != 1 )  sub ( /[Aa][Nn][Nn][Oo][Uu][Nn][Cc][Ee]/, "")
}

# replace the string of propagate to export
{ sub ( /[Pp][Rr][Oo][Pp][Aa][Gg][Aa][Tt][Ee]/, "export" ) }

# if none of the above patterns matches, copy the whole line
{ if (comment_line == 0)    print $0 } 

# reset the match bit
{  comment_line = 0  
   listen_line = 0
   announce_line = 0

}
' $1

