#!/bin/bash

if [ -n "$GI_TYPELIB_PATH" ]; then
    export GI_TYPELIB_PATH=/usr/lib64/gnome-shell
else
    export GI_TYPELIB_PATH=/usr/lib64/gnome-shell:$GI_TYPELIB_PATH
fi

if [ -n "$LD_LIBRARY_PATH" ] ; then
    export LD_LIBRARY_PATH=/usr/lib64/gnome-shell
else
    export LD_LIBRARY_PATH=/usr/lib64/gnome-shell:$LD_LIBRARY_PATH
fi

# for live logging into the terminal that started this
GJS_DEBUG_OUTPUT=stderr
GJS_DEBUG_TOPICS="JS ERROR;JS LOG"

export GJS_DEBUG_OUTPUT GJS_DEBUG_TOPICS


# test script to show how writing a keyfile buffer to a file garbles its content.

/usr/bin/gjs-console -I /usr/share/gnome-shell/js -c "const GLib = imports.gi.GLib;
                                                      const Lang = imports.lang;
                                                      const Mainloop = imports.mainloop;
                                                      const Gtk = imports.gi.Gtk;
                                                      
                                                      let here = GLib.get_current_dir();
                                                      let keyfile = new GLib.KeyFile();
                                                      
                                                      // read .desktop file into keyfile keeping all translations and comments
                                                      let loaded = keyfile.load_from_file(here + '/gedit-in.desktop', (GLib.KeyFileFlags.KEEP_COMMENTS | GLib.KeyFileFlags.KEEP_TRANSLATIONS));
                                                      if (loaded) {
                                                          
                                                          Mainloop.timeout_add_seconds(5, Lang.bind(this, function(p) {
                                                          // add message in your locale to the buffer, if your locale was not in the buffer
                                                          // it is added as last item in the Desktop Entry section
                                                          keyfile.set_locale_string('Desktop Entry', 'Name', 'en_US.utf8', 'blahblah');
                                                          
                                                          // log to stdout and write new file as gedit-out.desktop
                                                          let [data, datalen] = keyfile.to_data();
                                                          log(data);
                                                          log(datalen);
                                                          let success = GLib.file_set_contents(here + '/gedit-out.desktop', data, -1);
                                                          log(success);
                                                          }));
                                                      }

                                                      Gtk.init(null, 0);
                                                      Gtk.main();
                                                      " "$@"

                                                      
