/*****************************************
 * File: "templates/agent.mustache"
 * --------------------------------
 * Sidecar Frida Agent Mustache Template
 *
 *  https://github.com/huan/sidecar
 *  Huan <zixia@zixia.net>
 *  June 24, 2021
 *
 * All sidecar agent variable/function names
 *  MUST be started with the namespace `__sidecar__`
 *
 * i.e.:
 *  const __sidecar__variableName = {}
 *  function __sidecar__functionName () {}
 *
 *****************************************/

/***********************************
 * File: "templates/agent.mustache"
 *  > Partials: "libs/payload.cjs"
 ***********************************/
{{> libs/payload.cjs}}

/***********************************
 * File: "templates/agent.mustache"
 *  > Partials: "libs/log.cjs"
 ***********************************/
{{> libs/log.cjs }}

log.level('{{ logLevel }}')

/****************************************************
 * File: "templates/agent.mustache"
 *  > Get base address for target "{{{ sidecarTarget.target }}}"
 ****************************************************/
const __sidecar__moduleBaseAddress = Module.getBaseAddress('{{{ moduleName }}}')

/***********************************
 * File: "templates/agent.mustache"
 *  > Variable: "initAgentScript"
 ***********************************/
;;;
{{{ initAgentScript }}}
;;;

/********************************************
 * File: "templates/agent.mustache"
 *  > Partials: "native-functions.mustache"
 ********************************************/
{{> native-functions.mustache }}

/**************************************
 * File: "templates/agent.mustache"
 *  > Partials: "interceptors.mustache"
 **************************************/
{{> interceptors.mustache }}

/********************
 * RPC Exports Init *
 ********************/
function __sidecar__init () {
  log.verbose('SidecarAgent', 'init()')

  /**
   * DEBUG: Huan(202106) return 42 to let caller to make sure that
   *  this function has been runned successfully.
   */
  return 42
}

rpc.exports = {
  init: __sidecar__init,
  ...rpc.exports,
}

/**************************************
 * File: "templates/agent.mustache"
 *  > Partials: "rpc-exports.mustache"
 **************************************/
{{> rpc-exports.mustache }}
