#include <chrono>
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

void printPerms(fs::perms p) {
    std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
              << ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
              << ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
              << ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
              << ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
              << ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
              << ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
              << ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
              << ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
              << '\n';
}

void printPermissionsForDir() {
    for (const auto& entry : fs::directory_iterator(fs::current_path() / "../project")) {
        if (fs::is_regular_file(entry.path())) {
            std::cout << "File: "
                      << std::left << std::setw(20)
                      << entry.path().filename()
                      << " | Permissions: ";
            printPerms(fs::status(entry.path()).permissions());
        }
    }
    std::cout << std::string(80, '_') << "\n\n";
}

void changePermissions() {
    for (auto& entry : fs::directory_iterator(fs::current_path() / "../project")) {
        if (!entry.is_regular_file()) {
            continue;
        }

        if (entry.path().extension() == ".exe") {
            fs::permissions(entry.path(), fs::perms::owner_all |
                                              fs::perms::group_read |
                                              fs::perms::group_write |
                                              fs::perms::others_read |
                                              fs::perms::others_write);
        } else if (entry.path().extension() == ".cpp" || entry.path().extension() == ".hpp") {
            fs::permissions(entry.path(), fs::perms::owner_read |
                                              fs::perms::owner_write |
                                              fs::perms::group_read |
                                              fs::perms::others_read);
        }
    }
}

int main() {
    printPermissionsForDir();
    changePermissions();
    printPermissionsForDir();
}