const fs = require('fs');
const xml2js = require('xml2js');
const c_process = require('child_process');
const parser = new xml2js.Parser({ignoreAttrs: false});
const builder = new xml2js.Builder();

const relative_down_sitemap_path = "./public/sitemap.xml"
const store_path = './up_domain'
const relative_up_sitemap_path = store_path + "/public/sitemap.xml"
const relative_up_sitemap_dir = store_path + "/public/"

const up_sub_sitemap_name = "spring-sitemap.xml"
const root_repo_git_url = "git@gitcode.net:dev-cloud/spring-docs.git"


const up_domain = "https://docs.gitcode.net/"
const down_domain = "https://docs.gitcode.net/spring/guide/"

// 读取xml 文件内容
function get_sitemap_content(sitemap_path){
    return fs.readFileSync(sitemap_path)
}
// 解析xml
function parse_sitemap_content(sitemap_string){
    return new Promise(function(resolve, reject){
        parser.parseStringPromise(sitemap_string).then(function(sitemap_content){
            resolve(sitemap_content)
        }).catch(function(err){
            reject(err)
        })
    })
}
// build xml
function build_sitemap_from_json_content(sitemap_json){
    return new Promise(function(resolve, reject){
        var xml = builder.buildObject(sitemap_json);
        resolve(xml)
    })
}
// 添加子站sitemap
function ensure_up_sitemap(up_sitemap, up_sub_sitemap_name){
    var flag = false
    up_sitemap.sitemapindex.sitemap.forEach(item => {
        if(item.loc[0].endsWith(up_sub_sitemap_name)){
            flag = true
            item.lastmod = JSON.stringify(new Date())
        } 
    })
    if(!flag){
        up_sitemap.sitemapindex.sitemap.push({
            loc: [ up_domain +  up_sub_sitemap_name],
            lastmod: [ JSON.stringify(new Date()) ]
          })
    }
    return up_sitemap
}
// 子站 sitemap 更新时间
function ensure_down_sitemap(down_sitemap){
    down_sitemap.urlset.url.forEach(item => {
        console.log(item.loc)
        item.lastmod = JSON.stringify(new Date())
    })
    return down_sitemap
}
// 保存到文件
function write_to_file(path, content){
    fs.writeFileSync(path, content)
}
// 获取仓库
function fetch_up_repo(repo_url, store_path){
    return new Promise(function(resolve, reject){
        var cmd = "git clone --depth=1 " + repo_url + " " + store_path
        c_process.exec(cmd, function(error, stdout, stderr) {
            if(error) return reject(stderr)
            resolve(stdout)
        });
    })
    
}
// 推送仓库
function push_up_repo(store_path){
    return new Promise(function(resolve, reject){
        var cmd = "git -C " + store_path + " add . ;git -C " + store_path + " commit -m 'update sitemap';git -C " + store_path + " push"
        console.log(cmd)
        c_process.exec(cmd, function(error, stdout, stderr) {
            if(error) return reject(stderr)
            resolve(stdout)
        });
    })
}
// 删除
function clean(store_path){
    return new Promise(function(resolve, reject){
        var cmd = "rm -rf " + store_path
        console.log(cmd)
        c_process.exec(cmd, function(error, stdout, stderr) {
            if(error) return reject(stderr)
            resolve(stdout)
        });
    })
}


fetch_up_repo(root_repo_git_url, store_path).then(function(result){
    var down_sitemap_content = get_sitemap_content(relative_down_sitemap_path)
    return parse_sitemap_content(down_sitemap_content)
}).then(function(result){
    var down_sitemap_json = ensure_down_sitemap(result)
    return build_sitemap_from_json_content(down_sitemap_json)
}).then(function(down_sitemap_xml){
    write_to_file(relative_up_sitemap_dir + up_sub_sitemap_name, down_sitemap_xml)
    var up_sitemap_content = get_sitemap_content(relative_up_sitemap_path)
    return parse_sitemap_content(up_sitemap_content)
}).then(function(result){
    var up_sitemap_json = ensure_up_sitemap(result, up_sub_sitemap_name)
    return build_sitemap_from_json_content(up_sitemap_json)
}).then(function(up_sitemap_xml){
    write_to_file(relative_up_sitemap_path, up_sitemap_xml)
    return push_up_repo(store_path)
}).then(function(result){
    return clean(store_path)
}).then(function(result){
    console.log("Done!!")
}).catch(function(err){
    console.log("Err:", err)
})

