const os=require('os'),diskusage=require('diskusage');
const Logger = require('../logs/clsLogger').clsLogger;
const logger = new Logger();

class DiskUsage{
     freeSpace(callback){
       if (os.platform() == 'win32' && os.release().substring(0, 2) == '5.') {
           //if windows xp
           logger.System('getting disk space from windows xp');
           let cmd = require("cmd-exec").init();
           cmd
               .exec("fsutil volume diskfree " + rootPath)
               .then((res) => {
                   let cc = res.message.trim().replace('\n', '').split('\r');
                   callback( parseInt(cc[0].split(':')[1]))
               })
               .fail((err) => {
                    logger.System('error getting free space from windows xp:',err);
                   callback(0) ;
               })
       }
       else {
           logger.System('getting disk space from os');
          callback( diskusage.checkSync(process.cwd()).available)

       }
   }

    freeSpaceSync(){

        if (os.platform() == 'win32' && os.release().substring(0, 2) == '5.') {
            //if windows xp
            logger.System('getting disk space from windows xp');
            try {
                let res = execSync("fsutil volume diskfree " + rootPath).toString();
                let cc = res.message.trim().replace('\n', '').split('\r');
                return parseInt(cc[0].split(':')[1])
            }catch(err){
                logger.System('error getting disk space from xp');
                return 0;
            }

        }else {
            logger.System('getting disk space from os');
            return diskusage.checkSync(process.cwd()).available
        }

    }
}

module.exports=DiskUsage;