package xyz.wbsite.config;

import xyz.wbsite.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.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class SQLiteConfig {

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

    @PostConstruct
    public void generateDB() {
        try {
            Pattern compile = Pattern.compile("jdbc:sqlite:(.*.db3).*");
            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();
                    if (!file.exists()) file.createNewFile();
                    ResourceUtil.copyToFile("nginx-admin.db3", file);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
