#! /usr/bin/python3

import sys

class App:
  def __init__(self):
    self.filename = sys.argv[1]

  def count_par(self, line):
    if self.libname == None:
      return

    for c in line:
      if c == '(':
        self.par += 1
      if c == ')':
        self.par -= 1

    if self.par == 0:
      self.result += self.indent + 'pkgconfig.generate({}, install_dir : plugins_pkgconfig_install_dir)\n'.format(self.libname)
      print(self.libname)
      self.libname = None

  def parse(self):
    self.libname = None
    self.par = 0
    self.result = []

    with open(self.filename, 'r') as f:
      self.lines = f.readlines()

    for line in self.lines:
      self.result += line
      self.count_par(line)
      equal_pos = line.find('= library')
      if equal_pos > 0:
        self.indent = ''
        n = 0
        while line[n] == ' ':
          self.indent += ' '
          n += 1
        self.libname = line[n:equal_pos].strip()
        self.count_par(line)

  def write(self):
    with open(self.filename, 'w') as f:
      f.writelines(self.result)

app = App()
app.parse()
app.write()
