#!/pro/bin/perl

use 5.16.2;
use warnings;

use File::Find;

my $start = "/usr/lib/locale/en_US.utf8";
chdir $start or die "Cannot start in $start\n";

# 'locale -ck LC_TIME' to generate the source

local $/;

if (open my $lct, "<", "LC_TIME") {
    say "Reading  LC_TIME";
    my $lc = <$lct>;
    close $lct;

    $_ = $lc;

    # Date 05/04/2007 => 04-05-2007
    s{%m/%d/%Y}
     {%d-%m-%Y};

    # Date Thu Apr  5 11:04:08 CEST 2007 => Thu  5 Apr 2007 11:04:08
    s{%a %b %e %H:%M:%S %Z %Y}
     {%a %e %b %Y %H:%M:%S\0%Z};

    # Time 5:00 PM => 17:00
    s{%I:%M:%S %p}
     {%H:%M:%S\0\0\0};
    s{%r}
     {%R}g;

    if ($_ ne $lc) {
	say "Updating LC_TIME";
	open  $lct, ">", "LC_TIME" or die "LC_TIME: $!\n";
	print $lct $_;
	close $lct;

	foreach my $l (qw( ADDRESS MEASUREMENT MONETARY PAPER )) {
	    qx{ cp ../nl_NL.utf8/LC_$l LC_$l };
	    }
	}
    }

# Now fix the lock screen for Breeze
find (sub {
    m{^(Clock|InfoPane)\.qml$} or return;
    my $qmlf = $File::Find::name;
    open my $fh, "<", $File::Find::name or return;
    local $/;
    my $qml = <$fh>;
    close $fh;
    $_ = $qml;
    m{formatTime.*, *"hh:mm"} or
	s{text: [ ] Qt\.formatTime \(timeSource.data \["Local"\] \["DateTime"\] \K \) }
	 {, "hh:mm")}x;
    m{ddd dd MMM yyyy} or
	s{text: [ ] Qt .formatDate \(timeSource.data \["Local"\] \["DateTime"\], [ ] \K Qt\.DefaultLocaleLongDate \);? }
	 {"ddd dd MMM yyyy")\n        font.pointSize: 14}x;
    if ($_ ne $qml) {
	say "Updating $qmlf";
	open  $fh, ">", $qmlf;
	print $fh $_;
	close $fh;
	}
    }, "/usr/share/sddm/themes", "/usr/share/plasma/look-and-feel");

# https://nl.opensuse.org/openSUSE:Launch_party_12.3
# Now fix the extremely stupid default mount location for USB disks
my $usbf = "/usr/lib/udev/rules.d/80-udisks2.rules";
my $ud80 = do { local (@ARGV, $/) = ($usbf); <> };
unless ($ud80 =~ m/ENV\{UDISKS_FILESYSTEM_SHARED\}="1"/) {
    say "Fixing mount points";
    open my $fh, ">", $usbf or die "$usbf: $!\n";
    print $fh join "\n" => $ud80,
	q{# Use /media, not /run/media/<username>},
	q{ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"},
	"";
    close $fh;

    system "udevadm", "control", "--reload";
    }

# Go check if my/PROCURA Compose is still included
my $cmpd = "/usr/share/X11/locale/en_US.UTF-8";
if (chdir $cmpd) {
    if (-f "Compose-utf8.pro" and open my $fh, "<", "Compose") {
	my @c = <$fh>;
	close $fh;
	unless (grep m/PROCURA/i => @c) {
	    say "Updating Compose";
	    open $fh, "<", "Compose-utf8.pro";
	    push @c, "\n", <$fh>;
	    close $fh;
	    open $fh, ">", "Compose";
	    print $fh @c;
	    close $fh;
	    }
	}
    }
