// va chercher les arguments, strips les deux premieres entre par defaut
args = process.argv.slice(2);
// met le premier arguments dans la variable arg et le second dans extFunction
serial = args[0];
extFunction = args[1];

// fait pointe la variable sur le script externe de Zaid
//let provisioningData = require('/opt/genieacs/ext/provisioning.js');

if ( extFunction == "getWifi" ) {
  let provisioningData = require('/opt/genieacs/ext/wifi.js');
  provisioningData[extFunction](args,function(err, res){
    if (err != null) {
      console.log(err);
      process.exit();
    } else {
      const wifiList = res
      let i = 0;
      let w = 1;
      // verifie si le retour est vide mais vérifie la première valeur du array car le retour contient au minimum les braquets du json
      if ( wifiList[i] ) {
        // passe en boucle tout les entrees reçu dans wifiList
        const wifiListLength = wifiList.length
        //console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.*=path:"+wifiListLength);
        while ( i < wifiListLength ) {
          // desactive l'instance s'il y a une ou des informations manquante ou si desactive
          if ( !wifiList[i].enable || !wifiList[i].ssid || !wifiList[i].password || wifiList[i].enable == 0 ) {
            console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.${"+w+"}.Enable=false");
            console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.*.instance}.SSID=ssidDisabledByACS");
            console.log("")
          } else {
            // active l'instance et inscrit le ssid ainsi que le mot de passe
            console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.${"+w+"}.Enable=true");
            console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.${"+w+"}.SSID="+wifiList[i].ssid);
            console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.${"+w+"}.PreSharedKey="+wifiList[i].password);
            console.log("")
          }
        i++
        w++
        }
        // desactive tout les instances wifi si vide
      } else {
        //console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.*=path:1");
        console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.*.RadioEnabled=false");
        console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.*.Enable=false");
        console.log("InternetGatewayDevice.LANDevice.1.WLANConfiguration.*.instance}.SSID=radioDisabledByACS");
      }
    }
  process.exit();
  })
} else if ( extFunction == "getVoip"  ) {
  let provisioningData = require('/opt/genieacs/ext/voip.js');
  provisioningData[extFunction](args,function(err, res){
    if (err != null) {
      console.log(err);
      process.exit();
    } else {
      const voipList = res
      let i = 0;
      let v = 1;
      if ( voipList[i] ) {
        const voipListLength = voipList.length;
        while ( i < voipListLength ) {
          if ( !voipList[i].enable || !voipList[i].username || !voipList[i].password || voipList[i].enable == 0 ) {
            console.log("InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line."+v+".Enable = Disabled");
          } else {
            console.log("InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line."+v+".Enable = Enabled");
            console.log("InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line."+v+".SIP.AuthUserName = "+voipList[i].username);
            console.log("InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line."+v+".SIP.AuthPassword = "+voipList[i].password);
          }
          i++;
          v++;
        }
      } else {
        console.log("InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.*.Enable = Disabled");
      }
    }
  process.exit();
  })
} else {
  console.log("unknow fonction")
  process.exit();
}
