import chalk from 'chalk';
import { createLogUpdate } from './util/log';
import { genModuleLinks, handleModuleLinking,
         determineLinks } from './link';
import { determinePackLinkSrcDst, buildPackRef } from './pack-ref';
import findPackages from './find-packages';

export default function scanAndLink(config, rtenv, rootDirs) {
  const logUpdate = createLogUpdate(config, rtenv);

  return findPackages(config, rtenv, rootDirs, logUpdate)
    .takeWhile(() => !rtenv.cancelled)
    .mergeMap(
      eiDN => determinePackLinkSrcDst(config, rtenv, eiDN),
      config.concurrentOps
    )
    .takeWhile(() => !rtenv.cancelled)
    .do(lnkSrcDst => {
      rtenv.currentPackageDir = lnkSrcDst.dst;
      logUpdate();
    })
    .mergeMap(
      lnkSrcDst => {
        if (config.dryrun) {
          return determineLinks(config, rtenv, lnkSrcDst, false);
        } else if (config.genLnCmds) {
          return genModuleLinks(config, rtenv, lnkSrcDst);
        }
        return handleModuleLinking(config, rtenv, lnkSrcDst);
      },
      config.concurrentOps
    )
    .scan(
      (acc, [src, dst, size]) => {
        acc += size;
        return acc;
      },
      0
    )
    .do(savedBytes => { rtenv.savedByteCount = savedBytes; })
    .do(savedBytes => logUpdate());
}

@@ -382,16 +382,358 @@
 tenv);%0A%0A
+  const outputSrcDstForDryrun = (config.dryrun) ?%0A                                lnkSrcDst =%3E %7B%0A      const %7B devNameVer: dnv, src, dst %7D = lnkSrcDst;%0A      rtenv.log.clear();%0A      rtenv.out(chalk.bold(dnv.split(':')%5B0%5D)); // nameVersion%0A      rtenv.out(%60  $%7Bsrc%7D%60);%0A      rtenv.out(%60  $%7Bdst%7D%60);%0A      rtenv.out('');%0A    %7D :%0A    () =%3E %7B%7D;%0A%0A
   return
@@ -782,16 +782,16 @@
 Update)%0A
-
     .tak
@@ -964,24 +964,79 @@
 .cancelled)%0A
+    .do(lnkSrcDst =%3E outputSrcDstForDryrun(lnkSrcDst))%0A
     .do(lnkS
@@ -1452,30 +1452,31 @@
 ncurrentOps%0A
-
     )%0A
+%0A
     .scan(%0A 

add some output for dryrun