var fs = require('fs');
var path = require('path');
var GRADLE_SCRIPT_PATH = path.join(process.cwd(), 'android', 'build.gradle');

// load build.gradle content
try {
  var cfg = fs.readFileSync(GRADLE_SCRIPT_PATH);
} catch(err) {
  console.log(err.stack);
  console.log('Failed to load `android/build.gralde` when linking react-native-spinkit');
}

var depStr = String(cfg).match(/allprojects(.|[\r\n])+/);

if(depStr === null) {
  console.log('Could not find `allprojects { }` block in build.gradle');
}

// search fro allprojects {...} block
var bracketCount = 0;
var str = depStr[0];
var replacePos = 0;
for(var i in str) {
  if(str[i] === '{')
    bracketCount ++;
  else if(str[i] === '}'){
    bracketCount --;
    // block found
    if(bracketCount === 0) {
      replacePos = i;
      break;
    }
  }
}


// add jitpack repo to `repositories` block
var dep = str.substr(0, replacePos);

// Chech if the repository already exists
if (String(dep).match(/url[^h]*https\:\/\/jitpack\.io/) === null) {

  dep = String(dep).replace(/repositories[^\{]*\{/, 'repositories {\r\n\t\t\t\t// Add jitpack repository (added by react-native-spinkit)\r\n\t\t\t\tmaven { url "https://jitpack.io" }');
  str = dep + str.substr(replacePos, str.length - replacePos);

  // replace original build script
  depStr = String(cfg).replace(/allprojects(.|[\r\n])+/, str);
  fs.writeFileSync(GRADLE_SCRIPT_PATH, depStr);
}

@@ -1394,12 +1394,1485 @@
  depStr);%0A%7D%0A
+%0A// check if RN version %3E= 0.29 and add project dependency to MainApplication.java%0A%0Avar PACKAGE_JSON = process.cwd() + '/package.json';%0Avar hasNecessaryFile = fs.existsSync(PACKAGE_JSON);%0A%0Aif (!hasNecessaryFile) %7B%0A  throw 'RNSpinkit could not link Android automatically, some files could not be found.'%0A%7D%0A%0Avar package = JSON.parse(fs.readFileSync(PACKAGE_JSON));%0Avar APP_NAME = package.name;%0Avar APPLICATION_MAIN = process.cwd() + '/android/app/src/main/java/com/' + APP_NAME.toLocaleLowerCase() + '/MainApplication.java';%0A%0Aconsole.log('RNSpinkit checking app version ..');%0A%0Avar APP_VERSION = parseFloat(/%5Cd%5C.%5Cd+(?=%5C.)/.exec(package.dependencies%5B'react-native'%5D));%0A%0Aif(APP_VERSION %3E= 0.29) %7B%0A  console.log('RNSpinkit patching MainApplication.java .. ');%0A  if(!fs.existsSync(APPLICATION_MAIN)) %7B%0A    throw 'RNSpinkit could not link Android automatically, MainApplication.java not found in path : ' + APPLICATION_MAIN%0A  %7D%0A  var main = fs.readFileSync(APPLICATION_MAIN);%0A  if(String(main).match('new RNSpinkitPackage()') !== null) %7B%0A    console.log('skipped');%0A    return%0A  %7D%0A  main = String(main).replace('new MainReactPackage()', 'new RNSpinkitPackage(),%5Cn           new MainReactPackage()');%0A  main = String(main).replace('import com.facebook.react.ReactApplication;', 'import com.facebook.react.ReactApplication;%5Cnimport com.react.rnspinkit.RNSpinkitPackage;')%0A%0A  fs.writeFileSync(APPLICATION_MAIN, main);%0A  console.log('RNSpinkit patching MainApplication.java .. ok')%0A%0A%7D%0A

Change rnpm link script for link problem on RN >= 0.29