package ${basePackage}.config;

import ${basePackage}.frame.utils.ResourceUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class SQLiteConfig {

    @Value("${r'${spring.datasource.url}'}")
    private String url;

    @PostConstruct
    public void generateDB() {
        Pattern compile = Pattern.compile("jdbc:sqlite:(.*.db).*");
        Matcher matcher = compile.matcher(url);
        if (matcher.find()) {
            String group = matcher.group(1);
            File file = new File(group);
            if (!file.exists()) {
                File path = file.getAbsoluteFile().getParentFile();
                if (!path.exists()) path.mkdirs();
                ResourceUtil.copyResource2File("${projectName}.db", file);
            }
        }
    }
}
