<commit_before>( function _AdvancedMixin_s_() {

'use strict';

if( typeof module !== 'undefined' )
{

  if( !wTools.PlatformProvider.Abstract )
  require( './PlatformProviderAbstract.s' );

  if( process.platform === 'darwin' )
  var plist = require( 'plist' );
}

var _ = wTools;
var Abstract = _.PlatformProvider.Abstract;

//

function _mixin( cls )
{

  var dstProto = cls.prototype;

  _.assert( arguments.length === 1 );
  _.assert( _.routineIs( cls ) );

  _.mixinApply
  ({
    dstProto : dstProto,
    descriptor : Self,
  });

}

//

function _xvfbDisplayGet()
{
  _.assert( arguments.length <= 1 );

  var o = { display : arguments[ 0 ] };

  _.routineOptions( _xvfbDisplayGet, o );

  _.assert( _.numberIs( o.display ) );

  var exists = ( filePath ) => _.objectIs( _.fileProvider.fileStat( filePath ) );

  var xvfbLockFile;

  do
  {
    xvfbLockFile = `/tmp/.X${o.display}-lock`;
    o.display += 1;
  }
  while( exists( xvfbLockFile ) );

  o.display -= 1;

  return o.display;
}

_xvfbDisplayGet.defaults =
{
  display : 99
}

//

function _xvfbDisplaySet( display )
{
  var self = this;

  _.assert( _.numberIs( display ) );

  var _xvfbDisplaySetup = `Xvfb :${display} -screen 0 800x600x24 &`;

  return _.shell( _xvfbDisplaySetup )
  .ifNoErrorThen( function()
  {
    return _.shell
    ({
      mode : 'spawn',
      code : 'pidof Xvfb',
    })
    .ifNoErrorThen( function( o )
    {
      self._xvfbDisplayPid = Number.parseFloat( o.output );
    })
  });
}

//

function _xvfbDisplayKill()
{
  var self = this;

  var con = new wConsequence().give();

  /*
    Kill xvfb display instance with delay to avoid X server error
    "Resource temporarily unavailable on X server :display"
  */

  con.timeOutThen( 1000, function()
  {
    if( self.verbosity )
    console.log( 'Killing Xvfb display instance, pid: ' + self._xvfbDisplayPid );

    var res = process.kill( self._xvfbDisplayPid );
    _.assert( res, 'Display process: ' +  self._xvfbDisplayPid + ' not killed' );
  })

  return con;
}

//

function _plistPathGet()
{
  var self = this;

  var ins = 'Contents';
  var fileName = 'Info.plist';
  var index = self._appPath.indexOf( ins );
  if( index !== -1 )
  {
    self._plistPath = _.pathJoin( self._appPath.slice( 0, index + ins.length ), fileName );
  }
}

//

function _plistEdit()
{
  var self = this;

  self._plistPathGet();

  if( !_.strIs( self._plistPath ) )
  return;

  var raw = _.fileProvider.fileRead( self._plistPath );
  var list = plist.parse( raw );
  list.LSBackgroundOnly = true;
  list = plist.build( list );

  _.fileProvider.fileWrite( self._plistPath, list );

  self._plistChanged = true;
}

//

function _plistRestore()
{
  var self = this;

  if( _.fileProvider.fileStat( self._plistPath ) )
  {
    var raw = _.fileProvider.fileRead( self._plistPath );
    var list = plist.parse( raw );
    if( _.definedIs( list[ 'LSBackgroundOnly' ] ) )
    {
      delete list[ 'LSBackgroundOnly' ];
      list = plist.build( list );
      _.fileProvider.fileWrite( self._plistPath, list );

      self._plistChanged = false;
    }
  }
}

// --
// relationship
// --

var Composes =
{
}

var Aggregates =
{
}

var Associates =
{
}

var Restricts =
{
}

// --
// prototype
// --

var Supplement =
{

  _xvfbDisplayGet : _xvfbDisplayGet,
  _xvfbDisplaySet : _xvfbDisplaySet,
  _xvfbDisplayKill : _xvfbDisplayKill,

  _plistPathGet : _plistPathGet,
  _plistEdit : _plistEdit,
  _plistRestore : _plistRestore,
<commit_msg>plist issue fix<commit_after>( function _AdvancedMixin_s_() {

'use strict';

if( typeof module !== 'undefined' )
{

  if( !wTools.PlatformProvider.Abstract )
  require( './PlatformProviderAbstract.s' );

  if( process.platform === 'darwin' )
  var plist = require( 'plist' );
}

var _ = wTools;
var Abstract = _.PlatformProvider.Abstract;

//

function _mixin( cls )
{

  var dstProto = cls.prototype;

  _.assert( arguments.length === 1 );
  _.assert( _.routineIs( cls ) );

  _.mixinApply
  ({
    dstProto : dstProto,
    descriptor : Self,
  });

}

//

function _xvfbDisplayGet()
{
  _.assert( arguments.length <= 1 );

  var o = { display : arguments[ 0 ] };

  _.routineOptions( _xvfbDisplayGet, o );

  _.assert( _.numberIs( o.display ) );

  var exists = ( filePath ) => _.objectIs( _.fileProvider.fileStat( filePath ) );

  var xvfbLockFile;

  do
  {
    xvfbLockFile = `/tmp/.X${o.display}-lock`;
    o.display += 1;
  }
  while( exists( xvfbLockFile ) );

  o.display -= 1;

  return o.display;
}

_xvfbDisplayGet.defaults =
{
  display : 99
}

//

function _xvfbDisplaySet( display )
{
  var self = this;

  _.assert( _.numberIs( display ) );

  var _xvfbDisplaySetup = `Xvfb :${display} -screen 0 800x600x24 &`;

  return _.shell( _xvfbDisplaySetup )
  .ifNoErrorThen( function()
  {
    return _.shell
    ({
      mode : 'spawn',
      code : 'pidof Xvfb',
    })
    .ifNoErrorThen( function( o )
    {
      self._xvfbDisplayPid = Number.parseFloat( o.output );
    })
  });
}

//

function _xvfbDisplayKill()
{
  var self = this;

  var con = new wConsequence().give();

  /*
    Kill xvfb display instance with delay to avoid X server error
    "Resource temporarily unavailable on X server :display"
  */

  con.timeOutThen( 1000, function()
  {
    if( self.verbosity )
    console.log( 'Killing Xvfb display instance, pid: ' + self._xvfbDisplayPid );

    var res = process.kill( self._xvfbDisplayPid );
    _.assert( res, 'Display process: ' +  self._xvfbDisplayPid + ' not killed' );
  })

  return con;
}

//

function _plistPathGet()
{
  var self = this;

  var ins = 'Contents';
  var fileName = 'Info.plist';
  var index = self._appPath.indexOf( ins );
  if( index !== -1 )
  {
    self._plistPath = _.pathJoin( self._appPath.slice( 0, index + ins.length ), fileName );
  }
}

//

function _plistEdit()
{
  var self = this;

  self._plistPathGet();

  var tmpPath = _.pathResolve( __dirname, '../../../../tmp.tmp' );
  self.plistBackupPath = _.pathJoin( tmpPath, 'plistBackup.plist' );

  if( !_.strIs( self._plistPath ) )
  return;

  _.fileProvider.fileCopy( self.plistBackupPath, self._plistPath );

  var raw = _.fileProvider.fileRead( self._plistPath );
  var list = plist.parse( raw );
  list.LSBackgroundOnly = true;
  list = plist.build( list );

  _.fileProvider.fileWrite( self._plistPath, list );

  self._plistChanged = true;
}

//

function _plistRestore()
{
  var self = this;

  if( _.fileProvider.fileStat( self._plistPath ) )
  {
    // var raw = _.fileProvider.fileRead( self._plistPath );
    // var list = plist.parse( raw );
    // if( _.definedIs( list[ 'LSBackgroundOnly' ] ) )
    // {
    //   delete list[ 'LSBackgroundOnly' ];
    //   list = plist.build( list );
    //   _.fileProvider.fileWrite( self._plistPath, list );

    //   self._plistChanged = false;
    // }

    if( self._plistChanged )
    {
      _.fileProvider.fileCopy( self._plistPath, self.plistBackupPath );

      if( _.fileProvider.fileStat( self.plistBackupPath ) )
      _.fileProvider.fileDelete( self.plistBackupPath );

      self._plistChanged = false;
    }

  }
}

// --
// relationship
// --

var Composes =
{
  plistBackupPath : null
}

var Aggregates =
{
}

var Associates =
{
}

var Restricts =
{
}

// --
// prototype
// --

var Supplement =
{

  _xvfbDisplayGet : _xvfbDisplayGet,
  _xvfbDisplaySet : _xvfbDisplaySet,
  _xvfbDisplayKill : _xvfbDisplayKill,

  _plistPathGet : _plistPathGet,
  _plistEdit : _plistEdit,
  _plistRestore : _plistRestore,

{'fraction_non_alphanumeric': 0.14402465166130762, 'fraction_numerical': 0.005894962486602358, 'mean_word_length': 2.8341037493579866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7550594', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2642, 'n_words': 578}