#!/usr/bin/perl

#   Copyright (C) 2010 Michael Leach (LunixKing)


#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA
#
#   Version 0.1 beta

use File::Spec;
$location = File::Spec->rel2abs($0);

print "$location\n";
$self = conkymate;
$self =~ s/\.\///;
print "$self\n";
$location =~ s/$self\///;
print "$location\n";
`cd $location`;

system ("conky -c $location/conkyrc");

while ($? != 1536) {

#@DUMP = `$location/autoglade text.glade conkytext`;
@DUMP = `$location/autoglade $location/text.glade $location/conkytext`;

foreach $line (@DUMP) {
eval "\$$line";
print $line;
 }


if ($? != 1536) {

$FontColor =~ s/\#//g;
$ShadeColor =~ s/\#//g;
$Color1 =~ s/\#//g;
$Color2 =~ s/\#//g;
$XML = "<?xml version=\"1.0\"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.16 -->
  <!-- interface-naming-policy project-wide -->
  <widget class=\"GtkDialog\" id=\"dialog1\">
    <property name=\"border_width\">5</property>
    <property name=\"title\" translatable=\"yes\">ConkyMate</property>
    <property name=\"type_hint\">normal</property>
    <property name=\"opacity\">0.90000000022351745</property>
    <property name=\"has_separator\">False</property>
    <child internal-child=\"vbox\">
      <widget class=\"GtkVBox\" id=\"dialog-vbox1\">
        <property name=\"visible\">True</property>
        <property name=\"orientation\">vertical</property>
        <property name=\"spacing\">2</property>
        <child>
          <widget class=\"GtkScrolledWindow\" id=\"scrolledwindow1\">
            <property name=\"visible\">True</property>
            <property name=\"can_focus\">True</property>
            <property name=\"hscrollbar_policy\">automatic</property>
            <property name=\"vscrollbar_policy\">automatic</property>
            <child>
              <widget class=\"GtkTextView\" id=\"textview1:auto:init:cmdlineargs[1]\">
                <property name=\"height_request\">236</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
              </widget>
            </child>
          </widget>
          <packing>
            <property name=\"padding\">5</property>
            <property name=\"position\">1</property>
          </packing>
        </child>
        <child>
          <widget class=\"GtkTable\" id=\"table1\">
            <property name=\"visible\">True</property>
            <property name=\"n_rows\">6</property>
            <property name=\"n_columns\">4</property>
            <property name=\"homogeneous\">True</property>
            <child>
              <widget class=\"GtkCheckButton\" id=\"UseXFT\">
                <property name=\"label\" translatable=\"yes\">XFT Fonts</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">False</property>
                <property name=\"active\">$UseXFT</property>
                <property name=\"draw_indicator\">True</property>
              </widget>
              <packing>
                <property name=\"top_attach\">1</property>
                <property name=\"bottom_attach\">2</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkCheckButton\" id=\"DoubleBuffer\">
                <property name=\"label\" translatable=\"yes\">Double Buffer</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">False</property>
                <property name=\"active\">True</property>
                <property name=\"draw_indicator\">True</property>
              </widget>
              <packing>
                <property name=\"top_attach\">2</property>
                <property name=\"bottom_attach\">3</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkCheckButton\" id=\"OwnWindow\">
                <property name=\"label\" translatable=\"yes\">Own Window</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">False</property>
                <property name=\"active\">$OwnWindow</property>
                <property name=\"draw_indicator\">True</property>
              </widget>
              <packing>
                <property name=\"top_attach\">3</property>
                <property name=\"bottom_attach\">4</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkCheckButton\" id=\"Transparent\">
                <property name=\"label\" translatable=\"yes\">Transparent</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">False</property>
                <property name=\"active\">$Transparent</property>
                <property name=\"draw_indicator\">True</property>
              </widget>
              <packing>
                <property name=\"top_attach\">4</property>
                <property name=\"bottom_attach\">5</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkCheckButton\" id=\"DrawShades\">
                <property name=\"label\" translatable=\"yes\">Draw Shades</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">False</property>
                <property name=\"active\">$DrawShades</property>
                <property name=\"draw_indicator\">True</property>
              </widget>
              <packing>
                <property name=\"top_attach\">5</property>
                <property name=\"bottom_attach\">6</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkSpinButton\" id=\"GapX\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"xalign\">0.5</property>
                <property name=\"adjustment\">$GapX 0 9999 1 10 10</property>
                <property name=\"climb_rate\">1</property>
                <property name=\"numeric\">True</property>
              </widget>
              <packing>
                <property name=\"left_attach\">1</property>
                <property name=\"right_attach\">2</property>
                <property name=\"top_attach\">1</property>
                <property name=\"bottom_attach\">2</property>
                <property name=\"x_padding\">2</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkSpinButton\" id=\"GapY\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"xalign\">0.5</property>
                <property name=\"adjustment\">$GapY 0 9999 1 10 10</property>
                <property name=\"climb_rate\">1</property>
                <property name=\"numeric\">True</property>
              </widget>
              <packing>
                <property name=\"left_attach\">2</property>
                <property name=\"right_attach\">3</property>
                <property name=\"top_attach\">1</property>
                <property name=\"bottom_attach\">2</property>
                <property name=\"x_padding\">2</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label1\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Gap X</property>
              </widget>
              <packing>
                <property name=\"left_attach\">1</property>
                <property name=\"right_attach\">2</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label2\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Gap Y</property>
              </widget>
              <packing>
                <property name=\"left_attach\">2</property>
                <property name=\"right_attach\">3</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label3\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Font Color</property>
              </widget>
              <packing>
                <property name=\"left_attach\">1</property>
                <property name=\"right_attach\">2</property>
                <property name=\"top_attach\">2</property>
                <property name=\"bottom_attach\">3</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label4\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Shade Color</property>
              </widget>
              <packing>
                <property name=\"left_attach\">2</property>
                <property name=\"right_attach\">3</property>
                <property name=\"top_attach\">2</property>
                <property name=\"bottom_attach\">3</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkColorButton\" id=\"FontColor\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">True</property>
                <property name=\"use_alpha\">True</property>
                <property name=\"color\">\#$FontColor</property>
              </widget>
              <packing>
                <property name=\"left_attach\">1</property>
                <property name=\"right_attach\">2</property>
                <property name=\"top_attach\">3</property>
                <property name=\"bottom_attach\">4</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkColorButton\" id=\"ShadeColor\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">False</property>
                <property name=\"use_alpha\">True</property>
                <property name=\"color\">\#$ShadeColor</property>
              </widget>
              <packing>
                <property name=\"left_attach\">2</property>
                <property name=\"right_attach\">3</property>
                <property name=\"top_attach\">3</property>
                <property name=\"bottom_attach\">4</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkEntry\" id=\"WindowType\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"text\" translatable=\"yes\">$WindowType</property>
                <property name=\"xalign\">0.5</property>
              </widget>
              <packing>
                <property name=\"left_attach\">1</property>
                <property name=\"right_attach\">2</property>
                <property name=\"top_attach\">5</property>
                <property name=\"bottom_attach\">6</property>
                <property name=\"x_padding\">2</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label5\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Window Type</property>
              </widget>
              <packing>
                <property name=\"left_attach\">1</property>
                <property name=\"right_attach\">2</property>
                <property name=\"top_attach\">4</property>
                <property name=\"bottom_attach\">5</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label6\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Alignment</property>
              </widget>
              <packing>
                <property name=\"left_attach\">2</property>
                <property name=\"right_attach\">3</property>
                <property name=\"top_attach\">4</property>
                <property name=\"bottom_attach\">5</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkEntry\" id=\"Alignment\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"text\" translatable=\"yes\">$Alignment</property>
                <property name=\"xalign\">0.5</property>
              </widget>
              <packing>
                <property name=\"left_attach\">2</property>
                <property name=\"right_attach\">3</property>
                <property name=\"top_attach\">5</property>
                <property name=\"bottom_attach\">6</property>
                <property name=\"x_padding\">3</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label7\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Update Time</property>
              </widget>
              <packing>
                <property name=\"left_attach\">3</property>
                <property name=\"right_attach\">4</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkSpinButton\" id=\"UpdateTime\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"xalign\">0.5</property>
                <property name=\"adjustment\">$UpdateTime 0 100 1 10 10</property>
              </widget>
              <packing>
                <property name=\"left_attach\">3</property>
                <property name=\"right_attach\">4</property>
                <property name=\"top_attach\">1</property>
                <property name=\"bottom_attach\">2</property>
                <property name=\"x_padding\">2</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkColorButton\" id=\"Color1\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">True</property>
                <property name=\"use_alpha\">True</property>
                <property name=\"color\">\#$Color1</property>
              </widget>
              <packing>
                <property name=\"left_attach\">3</property>
                <property name=\"right_attach\">4</property>
                <property name=\"top_attach\">3</property>
                <property name=\"bottom_attach\">4</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkColorButton\" id=\"Color2\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">True</property>
                <property name=\"use_alpha\">True</property>
                <property name=\"color\">\#$Color2</property>
              </widget>
              <packing>
                <property name=\"left_attach\">3</property>
                <property name=\"right_attach\">4</property>
                <property name=\"top_attach\">5</property>
                <property name=\"bottom_attach\">6</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label8\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Font Color 1</property>
              </widget>
              <packing>
                <property name=\"left_attach\">3</property>
                <property name=\"right_attach\">4</property>
                <property name=\"top_attach\">2</property>
                <property name=\"bottom_attach\">3</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label9\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Font Color 2</property>
              </widget>
              <packing>
                <property name=\"left_attach\">3</property>
                <property name=\"right_attach\">4</property>
                <property name=\"top_attach\">4</property>
                <property name=\"bottom_attach\">5</property>
              </packing>
            </child>
            <child>
              <placeholder/>
            </child>
          </widget>
          <packing>
            <property name=\"position\">2</property>
          </packing>
        </child>
        <child>
          <widget class=\"GtkFontButton\" id=\"Font\">
            <property name=\"visible\">True</property>
            <property name=\"can_focus\">True</property>
            <property name=\"receives_default\">True</property>
            <property name=\"font_name\">$Font</property>
          </widget>
          <packing>
            <property name=\"position\">3</property>
          </packing>
        </child>
        <child>
          <widget class=\"GtkHBox\" id=\"hbox1\">
            <property name=\"visible\">True</property>
            <property name=\"homogeneous\">True</property>
            <child>
              <widget class=\"GtkLabel\" id=\"label10\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Max Width</property>
              </widget>
              <packing>
                <property name=\"position\">0</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label11\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">Min Size</property>
              </widget>
              <packing>
                <property name=\"position\">1</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkLabel\" id=\"label12\">
                <property name=\"visible\">True</property>
                <property name=\"label\" translatable=\"yes\">User Spacer</property>
              </widget>
              <packing>
                <property name=\"position\">2</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name=\"position\">4</property>
          </packing>
        </child>
        <child>
          <widget class=\"GtkHBox\" id=\"hbox2\">
            <property name=\"visible\">True</property>
            <property name=\"homogeneous\">True</property>
            <child>
              <widget class=\"GtkSpinButton\" id=\"MaxWidth\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"adjustment\">$MaxWidth 0 9999 1 10 10</property>
                <property name=\"numeric\">True</property>
              </widget>
              <packing>
                <property name=\"position\">0</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkSpinButton\" id=\"MinimumSize\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"adjustment\">$MinimumSize 0 9999 1 10 10</property>
                <property name=\"numeric\">True</property>
              </widget>
              <packing>
                <property name=\"position\">1</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkEntry\" id=\"UserSpacer\">
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"invisible_char\">&#x25CF;</property>
                <property name=\"text\" translatable=\"yes\">$UserSpacer</property>
              </widget>
              <packing>
                <property name=\"position\">2</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name=\"position\">5</property>
          </packing>
        </child>
        <child internal-child=\"action_area\">
          <widget class=\"GtkHButtonBox\" id=\"dialog-action_area1\">
            <property name=\"visible\">True</property>
            <property name=\"layout_style\">end</property>
            <child>
              <widget class=\"GtkButton\" id=\"button:auto:textview1\">
                <property name=\"label\">gtk-save</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">True</property>
                <property name=\"use_stock\">True</property>
              </widget>
              <packing>
                <property name=\"expand\">False</property>
                <property name=\"fill\">False</property>
                <property name=\"position\">0</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkButton\" id=\"button1\">
                <property name=\"label\">gtk-apply</property>
                <property name=\"response_id\">-5</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">True</property>
                <property name=\"use_stock\">True</property>
              </widget>
              <packing>
                <property name=\"expand\">False</property>
                <property name=\"fill\">False</property>
                <property name=\"position\">1</property>
              </packing>
            </child>
            <child>
              <widget class=\"GtkButton\" id=\"button2\">
                <property name=\"label\">gtk-quit</property>
                <property name=\"response_id\">-6</property>
                <property name=\"visible\">True</property>
                <property name=\"can_focus\">True</property>
                <property name=\"receives_default\">True</property>
                <property name=\"use_stock\">True</property>
              </widget>
              <packing>
                <property name=\"expand\">False</property>
                <property name=\"fill\">False</property>
                <property name=\"position\">2</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name=\"expand\">False</property>
            <property name=\"pack_type\">end</property>
            <property name=\"position\">0</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>";

$XML =~ s/'//g;

open (GLADE, "> $location/text.glade")  or die $!;
print GLADE "$XML";
close (GLADE)                 or die $!;

open (FOOTER, "< $location/conkytext");
#undef $/;
@textsection = <FOOTER>;
close (FOOTER);


$FontColor =~ s/\#//g;
$ShadeColor =~ s/\#//g;
$Color1 =~ s/\#//g;
$Color2 =~ s/\#//g;
$Font =~ s/ (\d)/:pixelsize=$1/;
$CONKYRC = 
"background yes 
 use_xft $UseXFT
 xftfont $Font
 double_buffer $DoubleBuffer
 own_window $OwnWindow
 own_window_type $WindowType
 own_window_transparent $Transparent
 own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
 own_window_colour $ShadeColor
 default_color $FontColor
 color1 $Color1
 color2 $Color2
 draw_shades $DrawShades
 update_interval $UpdateTime
 alignment $Alignment
 gap_x $GapX
 gap_y $GapY
 minimum_size $MinimumSize
 maximum_width $MaxWidth
 use_spacer $UserSpacer
 no_buffers yes
 pad_percents 3

 TEXT
@textsection
";

$CONKYRC =~ s/'//g;
$CONKYRC =~ s/True/yes/g;
$CONKYRC =~ s/False/no/g;

open (CONK, "> $location/conkyrc")  or die $!;
print CONK "$CONKYRC";
close (CONK)                 or die $!;
  }

}
`killall conky`;
