#!/usr/bin/env node

// Adds simple release notes to config.json and appends build number
//  e.g. release_notes.js 42 -> uses 42 as build number

var args = process.argv.slice(2),
  fs = require('fs'),
  xml2js = require('xml2js'),
  xmlParser = new xml2js.Parser(),
  builder = new xml2js.Builder(),
  exec = require('child_process').exec;

var buildNo = args[0],
  configJSON = require(__dirname + '/../src/config.json'),
  configXML = fs.readFileSync(__dirname + '/../config.xml');


xmlParser.parseString(configXML, function (err, result) {
  var appId = result.widget.$.id;
  var version = result.widget.$.version;
  console.log('buildNo' + buildNo);
  console.log('appId' + appId);
  console.log('version' + version);
  result.widget.$['android-versionCode'] = buildNo;
  result.widget.$['ios-CFBundleVersion'] = buildNo;
  var xml = builder.buildObject(result);
  fs.writeFile(__dirname + '/../config.xml', xml, function (err) {
    if (err) throw err;
  });


  exec('git describe --tags --abbrev=0', function (err, tag) {
    if (err instanceof Error) {
      throw err;
    }
    exec('git log ' + tag.replace(/\n$/, '') + '..HEAD --oneline', function (err, releaseNotes) {
      if (err instanceof Error) {
        throw err;
      }
      fs.writeFile(__dirname + '/../src-gen/RELEASE_NOTES', releaseNotes.replace(/"/, ''), function (err) {
        if (err) throw err;
      });
      configJSON.info.version.value = version + '(' + buildNo + ')';
      configJSON.info.release_notes.value = releaseNotes.replace(/"/, '').replace(/\n/g, '<br>');
      fs.writeFile(__dirname + '/../src-gen/config.json', JSON.stringify(configJSON), function (err) {
        if (err) throw err;
      });

    });
  });
});

exec('which agvtool >/dev/null', function (err, tag) {
  if (err instanceof Error) {
    // ignore
  } else {
    exec('cd ' + __dirname + '/../platforms/ios &&  agvtool new-version -all ' + buildNo, function (err) {
      if (err instanceof Error) {
        if (err) throw err;
      }
    });
  }
});

@@ -1716,308 +1716,4 @@
 %7D);%0A
-%0Aexec('which agvtool %3E/dev/null', function (err, tag) %7B%0A  if (err instanceof Error) %7B%0A    // ignore%0A  %7D else %7B%0A    exec('cd ' + __dirname + '/../platforms/ios &&  agvtool new-version -all ' + buildNo, function (err) %7B%0A      if (err instanceof Error) %7B%0A        if (err) throw err;%0A      %7D%0A    %7D);%0A  %7D%0A%7D);%0A

Fix build error