#!/usr/bin/ruby

var apps = [];
var desktopFilesPaths = [%d'/usr/share/applications'];

desktopFilesPaths.each { |path|

    path.is_a(Dir)        || next;
    path.open(\var dir_h) || next;

    var counter = 0;
    dir_h.entries.shuffle.each { |fullpath|

        fullpath.is_a(File) || next;

        fullpath.to_s ~~ /\.desktop\z/           || next;
        fullpath.sysopen(\var fh, File.O_RDONLY) || next;
        fh.sysread(\var content, fullpath.size);

        content.decode_utf8!;

        var fields = Hash();
        var regex = /^(\w.*?)=(.*\S)/m;

        while (var match = content.gmatch(regex)) {
            var (key, value) = match...
            fields{key} = value
        }

        apps.append(fields);
        counter++ == 3 && break;
    }
}

say apps.rand;
