print qq(/* THIS FILE WAS AUTOGENERATED BY GLOBALNOTIFIERGEN.PL --- DO NOT EDIT BY HAND!!! */\n\n);

my @vars = ();
foreach $file (@ARGV) {
    open(FILE, $file);

    while (<FILE>) {
      if (m|^\s*OWLVAR_([A-Z_0-9]+)\s*\(\s*"([^"]+)"\s*/\*\s*%OwlVarStub:?([a-z0-9_]+)?\s*\*/|) {   # "
        my $vartype = $1;
        my $varname = $2;
        my $altvarname = $2;
        $altvarname = $3 if ($3);

        my $propname = $altvarname;
        $propname =~ tr/a-z/A-Z/;
        $propname = "PROP_$propname";

        my $detailname = $altvarname;
        $detailname =~ s/[^a-zA-Z0-9]/-/g;
        $detailname =~ s/^[^a-zA-Z]+//;

        push @vars, {type => $vartype,
                     name => $varname,
                     altname => $altvarname,
                     propname => $propname,
                     detailname => $detailname};
      }
    }
    close(FILE);
}

print <<EOT;
#include "globalnotifier.h"
#include "owl.h"

/* properties */
enum {
  PROP_NONE,
  /* normal properties */
  PROP_RIGHTSHIFT,
  PROP_CURMSG_VERT_OFFSET,
  /* generated from variable listings */
EOT

for my $var (@vars) {
  print "  " . $var->{propname} . ",\n";
}

print <<EOT;
};

G_DEFINE_TYPE(OwlGlobalNotifier, owl_global_notifier, G_TYPE_OBJECT)

static void owl_global_notifier_set_property(GObject *object,
                                             guint property_id,
                                             const GValue *value,
                                             GParamSpec *pspec)
{
  OwlGlobalNotifier *notifier = OWL_GLOBAL_NOTIFIER(object);

  switch (property_id) {
    /* normal properties */
    case PROP_RIGHTSHIFT:
      notifier->g->rightshift = g_value_get_int(value);
      break;
    case PROP_CURMSG_VERT_OFFSET:
      notifier->g->curmsg_vert_offset = g_value_get_int(value);
      break;
    /* generated from variable listings */
EOT

for my $var (@vars) {
    my $varname = $var->{name};
    my $propname = $var->{propname};
    print "    case $propname:\n";

    if ($var->{type} =~ /^BOOL/) {
      print <<EOT;
      if (g_value_get_boolean(value)) {
        owl_variable_set_bool_on(&notifier->g->vars, "$varname");
      } else {
        owl_variable_set_bool_off(&notifier->g->vars, "$varname");
      }
EOT
    } elsif ($var->{type} =~ /^PATH/ or $var->{type} =~ /^STRING/) {
      print "      owl_variable_set_string(&notifier->g->vars, \"$varname\", g_value_get_string(value));\n";
    } elsif ($var->{type} =~ /^INT/ or $var->{type} =~ /^ENUM/) {
      print "      owl_variable_set_int(&notifier->g->vars, \"$varname\", g_value_get_int(value));\n";
    } 
    print "      break;\n";
}

print <<EOT;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  }
}

static void owl_global_notifier_get_property(GObject *object,
                                             guint property_id,
                                             GValue *value,
                                             GParamSpec *pspec)
{
  OwlGlobalNotifier *notifier = OWL_GLOBAL_NOTIFIER(object);

  switch (property_id) {
    /* normal properties */
    case PROP_RIGHTSHIFT:
      g_value_set_int(value, owl_global_get_rightshift(notifier->g));
      break;
    case PROP_CURMSG_VERT_OFFSET:
      g_value_set_int(value, owl_global_get_curmsg_vert_offset(notifier->g));
      break;
    /* generated from variable listings */
EOT
for my $var (@vars) {
    my $varname = $var->{name};
    my $propname = $var->{propname};
    print "    case $propname:\n";

    if ($var->{type} =~ /^BOOL/) {
      print "      g_value_set_boolean(value, owl_variable_get_bool(&notifier->g->vars, \"$varname\"));\n";
    } elsif ($var->{type} =~ /^PATH/ or $var->{type} =~ /^STRING/) {
      print "      g_value_set_string(value, owl_variable_get_string(&notifier->g->vars, \"$varname\"));\n";
    } elsif ($var->{type} =~ /^INT/ or $var->{type} =~ /^ENUM/) {
      print "      g_value_set_int(value, owl_variable_get_int(&notifier->g->vars, \"$varname\"));\n";
    } 
    print "      break;\n";
}
print <<EOT;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
  }
}

static void owl_global_notifier_class_init(OwlGlobalNotifierClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
  GParamSpec *pspec;

  gobject_class->get_property = owl_global_notifier_get_property;
  gobject_class->set_property = owl_global_notifier_set_property;

  /* Register properties */
  
  pspec = g_param_spec_int("rightshift",
                           "rightshift",
                           "How much we shift to the right",
                           0,
                           INT_MAX,
                           0,
                           G_PARAM_READABLE|G_PARAM_WRITABLE
                          |G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB);
  g_object_class_install_property(gobject_class, PROP_RIGHTSHIFT, pspec);

EOT
for my $var (@vars) {
    my $varname = $var->{name};
    my $propname = $var->{propname};
    my $detailname = $var->{detailname};
    if ($var->{type} =~ /^BOOL/) {
      print <<EOT
  pspec = g_param_spec_boolean("$detailname",
                               "$varname",
                               "$varname", /* TODO: parse out the summary too */
                               FALSE,
                               G_PARAM_READABLE|G_PARAM_WRITABLE
                              |G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB);
EOT
    } elsif ($var->{type} =~ /^PATH/ or $var->{type} =~ /^STRING/) {
      print <<EOT
  pspec = g_param_spec_string("$detailname",
                              "$varname",
                              "$varname", /* TODO: parse out the summary too */
                              "",
                              G_PARAM_READABLE|G_PARAM_WRITABLE
                             |G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB);
EOT
    } elsif ($var->{type} =~ /^INT/ or $var->{type} =~ /^ENUM/) {
      print <<EOT
  pspec = g_param_spec_int("$detailname",
                           "$varname",
                           "$varname", /* TODO: parse out the summary too */
                           INT_MIN,
                           INT_MAX,
                           0,
                           G_PARAM_READABLE|G_PARAM_WRITABLE
                          |G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB);
EOT
    } 
    print "  g_object_class_install_property(gobject_class, $propname, pspec);\n\n";
}
print <<EOT;
}

static void owl_global_notifier_init(OwlGlobalNotifier *self)
{
}

OwlGlobalNotifier *owl_global_notifier_new(owl_global *g)
{
  OwlGlobalNotifier *gn;
  
  gn = g_object_new(OWL_TYPE_GLOBAL_NOTIFIER, NULL);
  gn->g = g;
  return gn;
}
EOT
