<commit_before>﻿#include "stdafx.h"
#include "error.h"
#include "fs.h"
#include "reg.h"
#include "shortcut.h"
#include "utils.h"

namespace po = boost::program_options;

#ifdef __MINGW32__
extern "C"
#endif
int wmain(int argc, wchar_t **argv) {
	wstr name;
	po::options_description desc("Options");
	desc.add_options()(",n", po::wvalue<wstr>(&name)->required(), "Name of the distribution");
	po::variables_map vm;
	auto parse_args = [&]() {
		po::store(po::parse_command_line(argc - 1, argv + 1, desc), vm);
		po::notify(vm);
	};

	try {
#ifndef LXRUNOFFLINE_NO_WIN10
		if (win_build < 17134) {
			throw error_other(err_version_old, { L"1803",L"17134" });
		}
#endif
		if (argc < 2) {
			throw error_other(err_no_action, {});
#ifdef LXRUNOFFLINE_VERSION
		} else if (!wcscmp(argv[1], L"version")) {
			std::wcout << L"LxRunOffline " << LXRUNOFFLINE_VERSION << std::endl;
#endif
		} else if (!wcscmp(argv[1], L"l") || !wcscmp(argv[1], L"list")) {
			for (crwstr s : list_distros()) {
				std::wcout << s << std::endl;
			}
		} else if (!wcscmp(argv[1], L"gd") || !wcscmp(argv[1], L"get-default")) {
			std::wcout << get_default_distro() << std::endl;
		} else if (!wcscmp(argv[1], L"sd") || !wcscmp(argv[1], L"set-default")) {
			parse_args();
			set_default_distro(name);
		} else if (!wcscmp(argv[1], L"i") || !wcscmp(argv[1], L"install")) {
			wstr dir, file, root, conf_path;
			bool shortcut;
			desc.add_options()
				(",d", po::wvalue<wstr>(&dir)->required(), "The directory to install the distribution into.")
				(",f", po::wvalue<wstr>(&file)->required(), "The tar file containing the root filesystem of the distribution to be installed.")
				(",r", po::wvalue<wstr>(&root), "The directory in the tar file to extract. This argument is optional.")
				(",c", po::wvalue<wstr>(&conf_path), "The config file to use. This argument is optional.")
				(",s", po::bool_switch(&shortcut), "Create a shortcut for this distribution on Desktop.");
			parse_args();
			reg_config conf;
			if (!conf_path.empty()) conf.load_file(conf_path);
			register_distro(name, dir);
			conf.configure_distro(name, config_all);
			auto writer = wsl_v1_writer(dir);
			archive_reader(file, root).run(writer);
			if (shortcut) {
				wchar_t *s;
				auto hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, 0, &s);
				if (FAILED(hr)) throw error_hresult(err_create_shortcut, {}, hr);
				auto dp = unique_ptr_del<wchar_t *>(s, &CoTaskMemFree);
				create_shortcut(name, dp.get() + (L'\\' + name + L".lnk"), L"");
			}
		} else if (!wcscmp(argv[1], L"ui") || !wcscmp(argv[1], L"uninstall")) {
			parse_args();
			auto dir = get_distro_dir(name);
			unregister_distro(name);
			delete_directory(dir);
		} else if (!wcscmp(argv[1], L"rg") || !wcscmp(argv[1], L"register")) {
			wstr dir, conf_path;
			desc.add_options()
				(",d", po::wvalue<wstr>(&dir)->required(), "The directory containing the distribution.")
				(",c", po::wvalue<wstr>(&conf_path), "The config file to use. This argument is optional.");
			parse_args();
			reg_config conf;
			if (!conf_path.empty()) conf.load_file(conf_path);
			register_distro(name, dir);
			conf.configure_distro(name, config_all);
		} else if (!wcscmp(argv[1], L"ur") || !wcscmp(argv[1], L"unregister")) {
			parse_args();
			unregister_distro(name);
		} else if (!wcscmp(argv[1], L"m") || !wcscmp(argv[1], L"move")) {
			wstr dir;
			desc.add_options()(",d", po::wvalue<wstr>(&dir)->required(), "The directory to move the distribution to.");
			parse_args();
			auto sp = get_distro_dir(name);
			if (!move_directory(sp, dir)) {
				auto writer = wsl_v1_writer(dir);
				select_wsl_reader(get_distro_version(name), sp)->run(writer);
			}<commit_msg>Use the same version when moving.<commit_after>﻿#include "stdafx.h"
#include "error.h"
#include "fs.h"
#include "reg.h"
#include "shortcut.h"
#include "utils.h"

namespace po = boost::program_options;

#ifdef __MINGW32__
extern "C"
#endif
int wmain(int argc, wchar_t **argv) {
	wstr name;
	po::options_description desc("Options");
	desc.add_options()(",n", po::wvalue<wstr>(&name)->required(), "Name of the distribution");
	po::variables_map vm;
	auto parse_args = [&]() {
		po::store(po::parse_command_line(argc - 1, argv + 1, desc), vm);
		po::notify(vm);
	};

	try {
#ifndef LXRUNOFFLINE_NO_WIN10
		if (win_build < 17134) {
			throw error_other(err_version_old, { L"1803",L"17134" });
		}
#endif
		if (argc < 2) {
			throw error_other(err_no_action, {});
#ifdef LXRUNOFFLINE_VERSION
		} else if (!wcscmp(argv[1], L"version")) {
			std::wcout << L"LxRunOffline " << LXRUNOFFLINE_VERSION << std::endl;
#endif
		} else if (!wcscmp(argv[1], L"l") || !wcscmp(argv[1], L"list")) {
			for (crwstr s : list_distros()) {
				std::wcout << s << std::endl;
			}
		} else if (!wcscmp(argv[1], L"gd") || !wcscmp(argv[1], L"get-default")) {
			std::wcout << get_default_distro() << std::endl;
		} else if (!wcscmp(argv[1], L"sd") || !wcscmp(argv[1], L"set-default")) {
			parse_args();
			set_default_distro(name);
		} else if (!wcscmp(argv[1], L"i") || !wcscmp(argv[1], L"install")) {
			wstr dir, file, root, conf_path;
			bool shortcut;
			desc.add_options()
				(",d", po::wvalue<wstr>(&dir)->required(), "The directory to install the distribution into.")
				(",f", po::wvalue<wstr>(&file)->required(), "The tar file containing the root filesystem of the distribution to be installed.")
				(",r", po::wvalue<wstr>(&root), "The directory in the tar file to extract. This argument is optional.")
				(",c", po::wvalue<wstr>(&conf_path), "The config file to use. This argument is optional.")
				(",s", po::bool_switch(&shortcut), "Create a shortcut for this distribution on Desktop.");
			parse_args();
			reg_config conf;
			if (!conf_path.empty()) conf.load_file(conf_path);
			register_distro(name, dir);
			conf.configure_distro(name, config_all);
			auto writer = wsl_v1_writer(dir);
			archive_reader(file, root).run(writer);
			if (shortcut) {
				wchar_t *s;
				auto hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, 0, &s);
				if (FAILED(hr)) throw error_hresult(err_create_shortcut, {}, hr);
				auto dp = unique_ptr_del<wchar_t *>(s, &CoTaskMemFree);
				create_shortcut(name, dp.get() + (L'\\' + name + L".lnk"), L"");
			}
		} else if (!wcscmp(argv[1], L"ui") || !wcscmp(argv[1], L"uninstall")) {
			parse_args();
			auto dir = get_distro_dir(name);
			unregister_distro(name);
			delete_directory(dir);
		} else if (!wcscmp(argv[1], L"rg") || !wcscmp(argv[1], L"register")) {
			wstr dir, conf_path;
			desc.add_options()
				(",d", po::wvalue<wstr>(&dir)->required(), "The directory containing the distribution.")
				(",c", po::wvalue<wstr>(&conf_path), "The config file to use. This argument is optional.");
			parse_args();
			reg_config conf;
			if (!conf_path.empty()) conf.load_file(conf_path);
			register_distro(name, dir);
			conf.configure_distro(name, config_all);
		} else if (!wcscmp(argv[1], L"ur") || !wcscmp(argv[1], L"unregister")) {
			parse_args();
			unregister_distro(name);
		} else if (!wcscmp(argv[1], L"m") || !wcscmp(argv[1], L"move")) {
			wstr dir;
			desc.add_options()(",d", po::wvalue<wstr>(&dir)->required(), "The directory to move the distribution to.");
			parse_args();
			auto sp = get_distro_dir(name);
			if (!move_directory(sp, dir)) {
				auto ver = get_distro_version(name);
				auto writer = select_wsl_writer(ver, dir);
				select_wsl_reader(ver, sp)->run(*writer);
			}
{'fraction_non_alphanumeric': 0.19174560216508796, 'fraction_numerical': 0.011231393775372125, 'mean_word_length': 4.828864353312303, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3474227', 'n_tokens_mistral': 3081, 'n_tokens_neox': 2784, 'n_words': 615}