{"size": 3351, "ext": "js", "max_stars_repo_path": "src/config.js", "max_stars_repo_name": "punkhere/anigrab", "max_stars_repo_stars_event_min_datetime": "2020-05-03T17:59:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T11:29:02.000Z", "max_issues_repo_path": "src/config.js", "max_issues_repo_name": "punkhere/anigrab", "max_issues_repo_issues_event_min_datetime": "2020-07-06T03:32:45.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T19:33:20.000Z", "max_forks_repo_path": "src/config.js", "max_forks_repo_name": "punkhere/anigrab", "max_forks_repo_forks_event_min_datetime": "2020-06-18T20:22:09.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-30T00:46:17.000Z"}
{"max_stars_count": 103, "max_issues_count": 23, "max_forks_count": 28, "avg_line_length": 27.0241935484, "max_line_length": 136, "alphanum_fraction": 0.5481945688}
// Code taken and modified from https://github.com/vn-ki/anime-downloader/blob/master/anime_downloader/config.py

'use strict';

const path = require('path');
const fs = require('fs');
const os = require('os');

let Config;
const CONFIG_DIR =
    os.platform() === 'win32'
        ? path.join(process.env.APPDATA, 'anigrab')
        : path.join(process.env.HOME, '.config', 'anigrab');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');

const DEFAULT_CONFIG = {
    dl: {
        directory: '.',
        quality: '1080p',
        site: 'animekisa',
        aria2c: ['-c true', '-x 2', '-V'],
    },
    players: {
        mpv: [],
    },
    siteconfig: {
        animekisa: {
            server: 'gcloud',
            fallbackServers: ['mp4upload'],
        },
        animepahe: {
            version: 'subbed',
            fansub: 'HorribleSubs',
            fallbackFansubs: ['Okay-Subs', 'any'],
        },
        animerush: {
            server: 'mp4upload',
            fallbackServers: ['yourupload'],
        },
        gogoanime: {
            server: 'gcloud',
            fallbackServers: ['mp4upload', 'yourupload'],
        },
        ryuanime: {
            version: 'subbed',
            server: 'trollvid',
            fallbackServers: ['mp4upload'],
        },
        animeflix: {
            server: 'AUEngine',
            fallbackServers: ['FastStream'],
            version: 'sub',
        },
    },
};

function writeConfig(config) {
    fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 4));
}

function readConfig() {
    try {
        const config = JSON.parse(fs.readFileSync(CONFIG_FILE));
        return config;
    } catch (error) {
        console.error('Incorrectly formatted config file');
        console.error(error);
    }
}

function update(gkey, toObj, fromObj) {
    if (!toObj.hasOwnProperty(gkey)) toObj[gkey] = fromObj[gkey];

    for (const [key, val] of Object.entries(fromObj[gkey])) {
        if (!toObj[gkey].hasOwnProperty(key)) {
            toObj[gkey][key] = val;
        } else if (
            typeof fromObj[gkey][key] === 'object' &&
            fromObj[gkey][key] !== null
        ) {
            update(key, toObj[gkey], fromObj[gkey]);
        }
    }
}

function getConfig() {
    if (typeof Config === 'object' && Config !== null) return Config;

    try {
        fs.mkdirSync(CONFIG_DIR, { recursive: true });
    } catch (error) {
        // Failing hard like this should be avoided, will change in the future
        console.error('Failed to make config directory');
        console.error(error);
        process.exit();
    }

    if (!fs.existsSync(CONFIG_FILE)) {
        writeConfig(DEFAULT_CONFIG);
        Config = DEFAULT_CONFIG;
        return DEFAULT_CONFIG;
    } else {
        let config = readConfig();

        for (const prop in DEFAULT_CONFIG) {
            update(prop, config, DEFAULT_CONFIG);
        }
        writeConfig(config);
        // Taken from https://stackoverflow.com/questions/21363912/how-to-resolve-a-path-that-includes-an-environment-variable-in-nodejs
        const directory = config.dl.directory;
        config.dl.directory = directory.replace(
            /%([^%]+)%/g,
            (_, n) => process.env[n]
        );
        Config = config;
        return config;
    }
}

module.exports = {
    getConfig,
    CONFIG_DIR,
};
