#!/bin/sh

# creates autoconfig.js and elektra.cfs for configure-firefox

FFLibDir=$1
AutoConfigScript=$2
AutoConfigLauncher=$3

cat << EOF > "${FFLibDir}/${AutoConfigScript}"
//


var console = Components.classes["@mozilla.org/consoleservice;1"] .getService(Components.interfaces.nsIConsoleService);
console.logStringMessage("Elektra autoconfig started");
const configName = getPref("elektra.config.file");
console.logStringMessage("Elektra autoconfig: config file: "+configName);
const serverPort = getPref("elektra.config.reload_trigger_port");
console.logStringMessage("Elektra autoconfig: Server port: "+serverPort);

var enable=0;

if(configName && serverPort)
{
    enable=1;
}
else
{
    console.logStringMessage("Elektra autoconfig: no configuration found, disabling autoconfig script");
    displayError("Initializing Elektra autoconfig failed, please run kdb configure-firefox -s again");
}

function reload()
{

    console.logStringMessage("Elektra autoconfig: reloading config file");
    let infile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    infile.initWithPath(configName);
    var outfile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
    outfile.append("prefs.js");

    const nsIPrefService = Components.interfaces.nsIPrefService;
    const PrefServiceContractID = "@mozilla.org/preferences-service;1"
        let prefService = Components.classes[PrefServiceContractID].getService(nsIPrefService);

    prefService.readUserPrefs(infile);
    prefService.savePrefFile(outfile);          //save user prefs to prefs.js
    prefService.readUserPrefs(outfile);         //don't own my input file!!!
}

var listener = {
onSocketAccepted: function(serverSocket, clientSocket) {
                      var stream = clientSocket.openInputStream(0,0,0);
                      var sin = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
                      try{
                          sin.init(stream);
                          var bytes = sin.available();
                          var request = "";
                          request = sin.read(bytes);
                          console.logStringMessage("Elektra autoconfig: received message: "+request);
                          if (request.valueOf() == "reload"){
                              reload();
                          }
                      } finally {
                          sin.close();
                          stream.close();
                      }

                  }
}

function init()
{
    reload();       //read once on startup

    var serverSocket = Components.classes["@mozilla.org/network/server-socket;1"].createInstance(Components.interfaces.nsIServerSocket);
    serverSocket.init(serverPort, true, 1);         //only listen on loopback

    console.logStringMessage("Elektra autoconfig: server socket started");

    serverSocket.asyncListen(listener);
}

if(enable == 1)
{
    init();
}

EOF

cat << EOF > "${FFLibDir}/defaults/pref/${AutoConfigLauncher}"
pref("general.config.filename", "${AutoConfigScript}");
pref("general.config.obscure_value", 0);
EOF

