#!/usr/bin/perl

use XML::Parser;
use strict;

sub xfconf_set {
    my ($prop, $type, $val) = @_;
    my $cmd = "xfconf-query -n -c xfce4-desktop -p $prop -t $type -s $val";
    print $cmd."\n";
    system($cmd);
}

sub xfconf_set_color {
    my ($prop, $val) = @_;

    if($val =~ /\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)/) {
        my ($r,$g,$b) = ($1,$2,$3);

        my $cmd = "xfconf-query -n -c xfce4-desktop -p $prop ";
        $cmd .= "-t uint16 -t uint16 -t uint16 ";
        $cmd .= "-s $r -s $g -s $b";

        print $cmd."\n";
        system($cmd);
    } else {
        print STDERR "Couldn't parse color \"$val\"";
    }
}

sub tobool {
    my $val = shift;
    return ($val == 0 ? 'false' : 'true');
}

if(!defined($ENV{'XDG_CONFIG_HOME'})) {
    $ENV{'XDG_CONFIG_HOME'} = $ENV{'HOME'}.'/.config';
}

my $p = XML::Parser->new(Style => 'Tree');
my $mcs = $p->parsefile($ENV{'XDG_CONFIG_HOME'}.'/xfce4/mcs_settings/desktop.xml') or do {
    print STDERR "xfdesktop: no desktop.xml; nothing to migrate\n";
    exit(0);
};


my $mcs_option_element = -1;
for(my $i = 0; $i < scalar(@{$mcs}); $i++) {
    if(${$mcs}[$i] eq 'mcs-option') {
        $mcs_option_element = $i;
        last;
    }
}

my @mcs_option = @{${$mcs}[$mcs_option_element+1]};
my %options = ();
for(my $i = 0; $i < scalar(@mcs_option); $i++) {
    if($mcs_option[$i] eq 'option') {
        $options{${${$mcs_option[$i+1]}[0]}{'name'}} = ${$mcs_option[$i+1]}[0];
    }
}

foreach my $name (keys(%options)) {
    print "$name\n";
}

print $options{'brightness_0_0'}->{'type'}."\n";

my %xfconf_opts;

if(defined($options{'managedesktop-show-warning'})) {
    xfconf_set('/manage-desktop/show-warning-off',
               'bool',
               tobool($options{'managedesktop-show-warning'}->{'value'}));
}

if(defined($options{'managedesktop-show-warning-on'})) {
    xfconf_set('/manage-desktop/show-warning-on',
               'bool',
               tobool($options{'managedesktop-show-warning-on'}->{'value'}));
}

if(defined($options{'desktopiconstyle'})) {
    xfconf_set('/desktop-icons/style',
               'int',
               $options{'desktopiconstyle'}->{'value'});
}

if(defined($options{'icons_font_size'})) {
    xfconf_set('/desktop-icons/font-size',
               'uint',
               $options{'icons_font_size'}->{'value'});
}

if(defined($options{'icons_icon_size'})) {
    xfconf_set('/desktop-icons/icon-size',
               'uint',
               $options{'icons_icon_size'}->{'value'});
}

if(defined($options{'icons_use_system_font_size'})) {
    xfconf_set('/desktop-icons/use-system-font-size',
               'bool',
               tobool($options{'icons_use_system_font_size'}->{'value'}));
}

if(defined($options{'showdm'})) {
    xfconf_set('/desktop-menu/show',
               'bool',
               tobool($options{'showdm'}->{'value'}));
}

if(defined($options{'showdmi'})) {
    xfconf_set('/desktop-menu/show-icons',
               'bool',
               tobool($options{'showdmi'}->{'value'}));
}

if(defined($options{'showwl'})) {
    xfconf_set('/windowlist-menu/show',
               'bool',
               tobool($options{'showwl'}->{'value'}));
}

if(defined($options{'wl_show_ws_names'})) {
    xfconf_set('/windowlist-menu/show-workspace-names',
               'bool',
                tobool($options{'wl_show_ws_names'}->{'value'}));
}

if(defined($options{'wl_sticky_once'})) {
    xfconf_set('/windowlist-menu/show-sticky-once',
               'bool',
               tobool($options{'wl_sticky_once'}->{'value'}));
}

if(defined($options{'wl_submenus'})) {
    xfconf_set('/windowlist-menu/show-submenus',
               'bool',
               tobool($options{'wl_submenus'}->{'value'}));
}

# this is a bit overkill.  attempt up to 8 screens, 8 monitors
for(my $s = 0; $s < 8; $s++) {
    for(my $m = 0; $m < 8; $m++) {
        my $suf = '_'.$s.'_'.$m;
        my $p = "/backdrop/screen$s/monitor$m";

        # this is a crappy non-screen-numbered setting
        if(defined($options{'xineramastretch'})) {
            xfconf_set("/backdrop/screen$s/xinerama-stretch",
                       'bool',
                       tobool($options{'xineramastretch'}->{'value'}));
        }

        if(defined($options{'brightness'.$suf})) {
            xfconf_set("$p/brightness",
                       'int',
                       $options{'brightness'.$suf}->{'value'});
        }

        if(defined($options{'color1'.$suf})) {
            xfconf_set_color("$p/color1",
                             $options{'color1'.$suf}->{'value'});
        }

        if(defined($options{'color2'.$suf})) {
            xfconf_set_color("$p/color2",
                             $options{'color2'.$suf}->{'value'});
        }

        if(defined($options{'colorstyle'.$suf})) {
            xfconf_set("$p/color-style",
                       'int',
                       $options{'colorstyle'.$suf}->{'value'});
        }

        if(defined($options{'imagepath'.$suf})) {
            xfconf_set("$p/image-path",
                       'string',
                       $options{'imagepath'.$suf}->{'value'});
        }

        if(defined($options{'imagestyle'.$suf})) {
            xfconf_set("$p/image-style",
                       'int',
                       $options{'imagestyle'.$suf}->{'value'});
        }

        if(defined($options{'showimage'.$suf})) {
            xfconf_set("$p/image-show",
                       'bool',
                       tobool($options{'showimage'.$suf}->{'value'}));
        }
    }
}
