aboutsummaryrefslogtreecommitdiff
path: root/cli/main.cpp
blob: 614a274cab0f8c62d68649258abef7ebf2722463 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <filesystem>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <optional>
#include <boost/algorithm/string.hpp>
#include "katja/database.hpp"

bool trim_info_line(std::string& info_value)
{
    if (boost::algorithm::ends_with(info_value, "\""))
    {
        info_value.pop_back();
        return false;
    }
    else if (boost::algorithm::ends_with(info_value, "\\"))
    {
        info_value.pop_back();
        return true;
    }
    return false;
}

struct info_file
{
    const std::string program_name;
    const std::string version;
    const std::string homepage;
    const std::string email;
    const std::string maintainer;

    info_file(const std::string& program_name, const std::string& version,
            const std::string homepage, const std::string& email, const std::string& maintainer)
        : program_name(program_name), version(version), homepage(homepage), email(email), maintainer(maintainer)
    {
    }
};

std::optional<info_file> read_slackbuild_info(const std::filesystem::path& info_filepath)
{
    std::ifstream info_stream{ info_filepath };
    std::string info_line, info_variable, info_value;
    std::string program_name, version, homepage, email, maintainer;
    std::vector<std::string> download, md5sum, download_x86_64, md5sum_x86_64, requires;
    bool continuation{ false };

    while (std::getline(info_stream, info_line))
    {
        if (info_line.empty())
        {
            continue;
        }
        else if (!continuation)
        {
            auto equals_position = std::find(info_line.cbegin(), info_line.cend(), '=');
            info_variable = std::string(info_line.cbegin(), equals_position);

            if (equals_position == info_line.cend()
                    || ++equals_position == info_line.cend() || *equals_position != '"')
            {
                return std::nullopt;
            }
            info_value = std::string(std::next(equals_position), info_line.cend());
        }
        else
        {
            info_value += info_line;
        }
        continuation = trim_info_line(info_value);

        if (!continuation)
        {
            if (info_variable == "PRGNAM")
            {
                std::swap(program_name, info_value);
            }
            else if (info_variable == "VERSION")
            {
                std::swap(version, info_value);
            }
            else if (info_variable == "HOMEPAGE")
            {
                std::swap(homepage, info_value);
            }
            else if (info_variable == "EMAIL")
            {
                std::swap(email, info_value);
            }
            else if (info_variable == "MAINTAINER")
            {
                std::swap(maintainer, info_value);
            }
            else if (info_variable == "DOWNLOAD")
            {
                boost::split(download, info_value, boost::is_any_of(" "), boost::token_compress_on);
            }
            else if (info_variable == "MD5SUM")
            {
                boost::split(md5sum, info_value, boost::is_any_of(" "), boost::token_compress_on);
            }
            else if (info_variable == "DOWNLOAD_x86_64")
            {
                boost::split(download_x86_64, info_value, boost::is_any_of(" "), boost::token_compress_on);
            }
            else if (info_variable == "MD5SUM_x86_64")
            {
                boost::split(md5sum_x86_64, info_value, boost::is_any_of(" "), boost::token_compress_on);
            }
            else if (info_variable == "REQUIRES")
            {
                boost::split(requires, info_value, boost::is_any_of(" "), boost::token_compress_on);
            }
       }
    }
    return std::make_optional<info_file>(program_name, version, homepage, email, maintainer);
}

void search_for_slackbuilds(std::vector<info_file>& info_files, const std::filesystem::path& directory)
{
    for (const auto& entry : std::filesystem::directory_iterator(directory))
    {
        std::filesystem::path entry_path = entry;
        std::filesystem::path info_filename = entry_path;
        info_filename.replace_extension(".info");

        std::filesystem::path info_filepath = entry_path / info_filename;

        if (std::filesystem::exists(info_filepath))
        {
            auto slackbuild_info = read_slackbuild_info(info_filepath);

            if (slackbuild_info.has_value())
            {
                info_files.emplace_back(std::move(slackbuild_info.value()));
            }
            else
            {
                std::cout << info_filepath << std::endl;
            }
        }
        else if (std::filesystem::is_directory(entry_path))
        {
            search_for_slackbuilds(info_files, entry_path);
        }
    }
}

int main(int argc, const char **argv)
{
    std::vector<info_file> info_files;

    if (argc > 1)
    {
        std::filesystem::path slackbuild_repository{ argv[1] };

        search_for_slackbuilds(info_files, slackbuild_repository);
        for (const auto& slackbuild_info : info_files)
        {
            std::cout << slackbuild_info.program_name << " " << slackbuild_info.version << " " << std::endl;
        }
    }
    else
    {
        for (const auto& entry : std::filesystem::directory_iterator(katja::database))
        {
            katja::database_package database_entry{ entry.path().filename() };

            std::cout << database_entry.to_string() << std::endl;
        }
    }
    return 0;
}