#!/usr/bin/env python

import os
from stat import *

def change_ext(name, ext):
  k = name.rfind('.')
  if k >= 0:
    name = name[:k] + ext
  else:
    name = name + ext
  return name

inputs = {'abstract-close-dialog.ui': 'AbstractCloseDialog',
          'abstract-preferences-dialog.ui': 'AbstractPreferencesDialog',
          'abstract-project-dialog.ui': 'AbstractProjectDialog',
          'abstract-project-dialog-options.ui': 'AbstractProjectDialogOptions',
          'abstract-project-dialog-remove.ui': 'AbstractProjectDialogRemove',
          'abstract-native-builder-preferences.ui': 'AbstractNativeBuilderPreferences',
          'abstract-executable-preferences.ui': 'AbstractExecutablePreferences'}

def up_to_date (fin, fout):
  if os.path.exists (fout) and os.path.getmtime (fin) <= os.path.getmtime (fout):
    return True
  else:
    return False

for f in inputs.keys():
  class_name = inputs[f]

  # ui -> vala
  output = change_ext (f, '.vala')
  if not up_to_date ('libvalide/ui/' + f, 'libvalide/ui/' + output):
    print 'Wrote ' + output
    os.system ('xsltproc -nodtdattr --stringparam \'namespace\' \'Valide\' --stringparam \'class-name\' \'' + class_name + '\' gen-vala-gtk-widget-bindings.xslt libvalide/ui/' + f + ' > libvalide/ui/' + output)
