const fs = require("fs");
const YAML = require("yaml");
const child_process = require("child_process");
async function read_html(filename){
    return  fs.readFileSync(filename,"utf8");
}


 function parse_du_line(text) {
    res={}
    
    r=text.split("\t")
    if (r.length <2) return [text]; // valid line mus have a column size and folder
    foldername=r[1]
    res["size"]=r[0]
    res["folderpath"]=foldername
    res["folder"]=foldername.split("/")[foldername.split("/").length-1]
    //console.log([foldername.split("/")[foldername.split("/").length-1], foldername.split("/"),foldername.split("/").length])
    r1=foldername.replace(/(\d{3})[\._]?([A\d]{3})/i,"$1.$2")
    r1=r1.match(/(\d{3})\.([A\d]{3})/i)
    if (!r1) return res;
    res["lvanr"] = r1[0]
    return res
}

function du_unizeug(){
buf = child_process.execSync("du /mnt/save/daten/Unizeug/ -d 1 -h",{"maxBuffer": 1024*1024*48}).toString()
    tab=[];

    buf.split("\n").forEach(value => {
    text=parse_du_line(value)
    tab.push(text)
    })
    lookup=tab.reduce((acc,item,index)=>{
        if (item["lvanr"]) {
            if (acc[item["lvanr"]]) acc[item["lvanr"]].push(index);
            else acc[item["lvanr"]] =[index];
    }
    return acc;
    }, {});
    return {"folders": tab, "lookup": lookup}

}
module.exports = {read_html,du_unizeug}