// models\api\v1
const fs = require('fs')
var path = require('path');
const { readdir } = require("fs").promises
var verse = require('../serververse');
const fswalk = verse.fs.walk
var path = require('path');

const Rx = require('rxjs');

const cliargs = verse.cliargs;
const processedArgs = cliargs(process.argv.slice(2));

// console.dir(processedArgs)
// console.dir(Rx)

// https://github.com/coolaj86/node-walk

// PB : TODO -- convert to an input json config.
var outputfile = processedArgs._[0]; // ../elixir-client/physio/linkclient.bat
var startdir = processedArgs._[1];  // ../elixir-client/physio-1/app/models/api/v1
var inprefix = processedArgs._[2]; // '../../../../../elixir-client/client/'
if(!inprefix) inprefix = '..\\client\\'
var unprefix = processedArgs._[3];  // ../elixir-client/physio-1/
if(!unprefix) unprefix = '../elixir-client/physio-1/'
var outprefix = processedArgs._[4]; // '../../../../../elixir-client/physio/'
if(!outprefix) outprefix = ''

var fswalkoptions = {} // all files recurscively

// var fswalkoptions = { includerootdir : true,  includedirectories : true, expand : true } // example walk and output everything.
// (unprefix, outprefix)
// console.log(path.normalize(unprefix))

const writeStream = ( fs.createWriteStream(outputfile) )


fswalk.call( fswalkoptions, startdir ).then( s => {
    s.subscribe( entry => {
      // var matches = /.*?data.*/.exec(utils.fs.direntToFullname(entry)) 
      // if(matches) console.dir( matches[0] ) // Filters can be done on the stream itself..
      var i = entry
      var pathnodes = i.path.replace( path.normalize(unprefix), '' ).split('\\')
      var relative = Array(pathnodes.length)
      writeStream.write( '\nmklink ' + (entry.isDirectory() ? ' /D ' : ' ') + i.path.replace( path.normalize(unprefix), outprefix ) + ' '
        + relative.join('..\\') + inprefix + i.path.replace( path.normalize(unprefix), outprefix )
      )
      // console.dir( entry )
    }
  )
  // ; s.unsubscribe()
})