#!/usr/bin/env node

var fs = require('fs-extra');
var path = require('path');
var klawSync = require('klaw-sync')

var androidPlatformsDir = path.resolve(__dirname, '../../platforms/android/res'); 

var copyAllIcons = function(iconDir) {
    var densityDirs = klawSync(iconDir, {nofile: true})
    // console.log("densityDirs = "+JSON.stringify(densityDirs));
    densityDirs.forEach(function(dDir) {
      var files = klawSync(dDir.path, {nodir: true});
      files.forEach(function(file) {
        var dirName = path.basename(dDir.path);
        var fileName = path.basename(file.path);
        if (dirName.startsWith("mipmap")) {
            var drawableName = dirName.replace("mipmap", "drawable");
            var srcName = path.join(iconDir, dirName, fileName);
            var dstName = path.join(iconDir, drawableName, fileName);
            console.log("About to copy file "+srcName+" -> "+dstName);
            fs.copySync(srcName, dstName);
        }
      });
    });
};

var copyIconsFromAllDirs = function() {
  copyAllIcons(androidPlatformsDir);
}

var platformList = process.env.CORDOVA_PLATFORMS;

if (platformList == undefined) {
  console.log("Testing by running standalone script, invoke anyway");
  copyIconsFromAllDirs();
} else {
  var platforms = platformList.split(",");
  if (platforms.indexOf('android') < 0) {
    console.log("Android platform not specified, skipping...");
  } else {
    copyIconsFromAllDirs();
  }
}

Move the standard launcher icon from `mipmap` to `drawable`
Move the standard launcher icon from `mipmap` to `drawable`

The most recent android versions store their icon under
`mipmap..." (e.g. mipmap-hdpi, mipmap-xhdpi, mipmap-xxxhdpi)
https://android-developers.googleblog.com/2014/10/getting-your-apps-ready-for-nexus-6-and.html

But that means that if we want to have our app icon as the large icon, we need
to have it in assets - only `drawables` are loaded otherwise. And if it an
asset - unsure if sizing and other things will work well.

So we add another hook that will copy `icon.png` from the `mipmap-*` directories to the corresponding `drawable` directories.
