#!/usr/bin/env node

const fs = require('fs')
const {join, resolve} = require('path')
const path = require('path')

// 创建递归复制函数
function copyRecursiveSync(src, dest) {
    const stats = fs.statSync(src)

    if (stats.isDirectory()) {
        // 如果是目录，创建目标目录
        fs.mkdirSync(dest, { recursive: true })
        // 读取目录内容并递归复制
        fs.readdirSync(src).forEach(item => {
            copyRecursiveSync(path.join(src, item), path.join(dest, item))
        })
    } else {
        // 如果是文件，直接复制
        fs.copyFileSync(src, dest)
    }
}


let cwd = process.cwd();

//复制owlconfig.json到 当前目录
var templatePath = join(__dirname, "../defaultTemplate")
var buildProperties = join(cwd,'/buildProperties/build.properties');
var antLibPath = join(__dirname,'../antLib')
let owlconfig = {
    "models":"models",
    "templates":templatePath,
    "generatedApps":"generatedApps",
    "deployApps": ["owlsysApps"],
    "buildType":"release",
    "buildProperties":buildProperties,
    "libpath":antLibPath
}

let dst = resolve(cwd, "owlconfig.json")
fs.writeFileSync(dst, JSON.stringify(owlconfig), {flag: 'w'})

//在当前目录创建models目录

let modelsDir = join(cwd,"models");
fs.mkdirSync(modelsDir)

//在models Dir 目录下创建一个example.json
let exampleDir = resolve( __dirname,"../examples/models")
copyRecursiveSync(exampleDir,modelsDir);

let appsDir = resolve( __dirname,"../examples/owlsysApps");
let destAppsDir = join(cwd,"owlsysApps");
copyRecursiveSync(appsDir,destAppsDir);

//在project目录下面创建一个build.properties


let buildPropertiesPath = join(__dirname,'../examples/buildProperties');
let fullname = resolve(buildPropertiesPath,'build.properties');
let buildPropertiesDir = join(cwd,"buildProperties");
fs.mkdirSync(buildPropertiesDir)


let dstBuildProperties = resolve(buildPropertiesDir,'build.properties')

let buildPropertiesContent = "deployUrl=http://localhost/deployer/deploy.jsp\n" +
    "deployPass=change_it_in_catalina_sh\n";

buildPropertiesContent += "antlibpath=" + antLibPath.replace(/\\/g, "/") + "\n";
fs.writeFileSync(dstBuildProperties, buildPropertiesContent, {flag: 'w'})



//生成.gitignore
let gitignorePath = join(__dirname,'../examples/gitignore');
let gitignoreSrc = resolve(gitignorePath,'gitignore_example.txt');
let gitignoreDst = resolve(cwd,'.gitignore')
fs.copyFileSync(gitignoreSrc, gitignoreDst)

