let Utils = require('../utils/smgutils')
let utils = new Utils()
let config = utils.readConfig();
let fs = require('fs-extra'), path = require('path')

let file = path.join(process.cwd(), 'anydesk.dmg')


let unzippath = path.join(process.cwd(), 'upload')


async function Main() {

    console.log('unzipping')

    console.log(await utils.unzipfile(file, unzippath, true, true,true))

    console.log('finished')
}

Main()


async function unziper(file, unzippath, deleteFolder, deleteSource,deleteSourceIfUnzipFails) {
    let fs = require('fs-extra')
    let unzip = require('unzip')

    if (!(await fs.pathExists(file))) {
        return {err: 1, msg: 'file not found'}
    }
    if (deleteFolder) {
        await fs.remove(unzippath)
    }

    await fs.ensureDir(unzippath)

    let result = await (new Promise((resolve, reject) => {
        fs.createReadStream(file).pipe(unzip.Extract({path: unzippath}).on('error', (err) => {
            resolve({err: 2, msg: err.toString()})


        })).on('close', () => {
            resolve({err: 0, msg: 'success'})
        });
    }))

    if (result.err == 0 && deleteSource) {
        await fs.remove(file)
    }
    if (result.err == 2 && deleteSourceIfUnzipFails) {
        await fs.remove(file)
    }
    return result;

}


setInterval(() => {
}, 500)