#!/usr/bin/perl

######################################################################
#
# backtick_wrap:	by greenfly <greenfly@greenfly.org>
#
# usage: backtick_wrap filename
#
# This script will take any input line containing items in the FIRST 
# pair of parenthesis and wrap each item in backticks, so for instance:
# 	(foo, bar, baz, quux) xyzzy (foobar, barbaz, quuux)
# becomes:
# 	(`foo`, `bar`, `baz`, `quux`) xyzzy (foobar, barbaz, quuux)
#
# notice the items in the second pair of parens are unaffected
#
#
######################################################################


while(<>)
{
# if the line has has parens then process it, otherwise just print the line out
   if(/^([^(]+\()	# match some number of nonparens, then a paren,
      			# and put it into the $1 variable

      (.*?)		# match anycharacter, but as few as possible, so you
      			# don't go past the very next closing paren.  Then put
			# what you match in $2 variable

      (\).*$)		# match a closing paren, and anything else up to the
      			# end of the line.  put it in the $3 variable
      /x)
   {
      $a=$1;	# store these temporary variables somewhere
      $b=$2;
      $c=$3;

      $b =~ s/"//g;	# strip any quotes (could strip other 'bad' chars here)
      
      $b = "`" . 	# start with an opening backtick
      	join("`, `", 	# join each item from the array output from the 
			# following split command into a single string, with	
			# each item separated with `, `

		split(/,\s*/, $b))	# this split command takes each comma
					# delimited item and splits it into an
					# individual string in an array

	  . "`"; 	# put the closing ` on the string


      print "$a$b$c\n";
   }
   else
   {
      print;
   }
}
