'use strict'

module.exports = function parseArgv (config) {
  var setobj = {}
  var args = process.argv
  var check = /^--./

  for (var i = 0, param; param = args[i]; i++) {
    if (check.test(param)) {
      param = param.slice(2)
      var val = args[i + 1]
      if (check.test(val)) {
        val = true
      } else {
        i++
        var def = config[param] && config[param].$val
        if (!def || typeof def !== 'string') {
          try {
            val = JSON.parse(val)
          } catch (err) {}
        }
      }
      setobj[param] = val
    }
  }

  var configSet = setobj.config
  if (configSet) {
    try {
      configSet = JSON.parse(configSet)
      delete setobj.config
      for (var s in setobj) {
        configSet[s] = setobj[s]
      }
      setobj = configSet
    } catch (err) {

    }
  }

  return setobj
}

@@ -98,16 +98,17 @@
 ss.argv%0A
+%0A
   var ch
@@ -623,197 +623,573 @@
     
-try %7B%0A      configSet = JSON.parse(configSet)%0A      delete setobj.config%0A      for (var s in setobj) %7B%0A        configSet%5Bs%5D = setobj%5Bs%5D%0A      %7D%0A      setobj = configSet%0A    %7D catch (err) %7B%0A
+var decoded%0A    var parsed%0A    try %7B%0A      decoded = decodeURIComponent(configSet)%0A      parsed = JSON.parse(decoded)%0A      configSet = parsed%0A      delete setobj.config%0A      // console.log('configset is', configSet)%0A      for (var s in setobj) %7B%0A        // console.log('set dat field', s, 'to%5Cn', setobj%5Bs%5D)%0A        configSet%5Bs%5D = setobj%5Bs%5D%0A      %7D%0A      setobj = configSet%0A    %7D catch (err) %7B%0A      console.log('CONFIG ERROR! could not parse --config:%5Cn',%0A        setobj.config,%0A        '%5Cngot from decodeURIComponent:', decoded,%0A        '%5Cnerror: ', err%0A      )
 %0A   

read encoded config from dowhap if present