aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java
blob: 29a9daa2c398bcd758adceab98eb5c468218ea47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cc.polyfrost.oneconfig.config.profiles;

import cc.polyfrost.oneconfig.internal.config.OneConfigConfig;
import cc.polyfrost.oneconfig.internal.config.core.ConfigCore;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Profiles {
    private static final Logger LOGGER = LogManager.getLogger("OneConfig Profiles");
    public static final File nonProfileSpecificDir = new File("OneConfig/config");
    public static final File profileDir = new File("OneConfig/profiles");
    public static ArrayList<String> profiles;

    public static String getCurrentProfile() {
        if (!profileDir.exists() && !profileDir.mkdir()) {
            LOGGER.fatal("Could not create profiles folder");
            return null;
        }
        if (profiles == null) {
            String[] profilesArray = profileDir.list((file, s) -> file.isDirectory());
            if (profilesArray != null) profiles = new ArrayList<>(Arrays.asList(profilesArray));
        }
        if (!getProfileDir(OneConfigConfig.currentProfile).exists()) {
            createProfile(OneConfigConfig.currentProfile);
        }
        return OneConfigConfig.currentProfile;
    }

    public static void createProfile(String name) {
        File folder = new File(profileDir, name);
        if (!folder.exists() && !folder.mkdir()) {
            LOGGER.fatal("Could not create profile folder");
            return;
        }
        profiles.add(name);
    }

    public static File getProfileDir() {
        return getProfileDir(getCurrentProfile());
    }

    public static File getProfileDir(String profile) {
        return new File(profileDir, profile);
    }

    public static File getProfileFile(String file) {
        return new File(getProfileDir(), file);
    }

    public static File getNonProfileSpecificDir(String file) {
        return new File(nonProfileSpecificDir, file);
    }

    public static void loadProfile(String profile) {
        ConfigCore.saveAll();
        OneConfigConfig.currentProfile = profile;
        //OneConfig.config.save(); todo do we actually need this
        ConfigCore.reInitAll();
    }

    public static void renameProfile(String name, String newName) {
        try {
            File newFile = new File(profileDir, newName);
            FileUtils.moveDirectory(getProfileDir(name), newFile);
            if (OneConfigConfig.currentProfile.equals(name)) OneConfigConfig.currentProfile = newName;
            profiles.remove(name);
            profiles.add(newName);
        } catch (IOException e) {
            LOGGER.error("Failed to rename profile");
        }
    }

    public static void deleteProfile(String name) {
        if (name.equals(getCurrentProfile())) {
            if (profiles.size() == 1) {
                LOGGER.error("Cannot delete only profile!");
                return;
            }
            loadProfile(profiles.stream().filter(entry -> !entry.equals(name)).findFirst().get());
        }
        try {
            FileUtils.deleteDirectory(getProfileDir(name));
            profiles.remove(name);
        } catch (IOException e) {
            LOGGER.error("Failed to delete profile");
        }
    }
}