// File: ivan-tkatchev[P]makerpm/makerpm.cc
// Function: main
int main(int argc, char** argv) {

    try {

        bool is_file = (strcmp(argv[4], "-i") == 0);

        if (argc < 5 || (is_file && argc != 6)) {
            std::cout << "Usage:" << std::endl
                      << argv[0] << " <rpm props config> <output rpm file> <path prefix> <files to package>*" << std::endl
                      << "or" << std::endl
                      << argv[0] << " <rpm props config> <output rpm file> <path prefix> -i <input file>" << std::endl;
            return 1;
        }

        //

        std::string propsfile = argv[1];
        std::string output = argv[2];
        std::string prefix = argv[3];

        std::vector<std::string> files;

        if (is_file) {
            std::ifstream infile(argv[5]);
            infile.exceptions(std::ifstream::badbit);
            std::string line;
            while (std::getline(infile, line)) {
                files.push_back(line);
            }
            if (files.empty()) {
                throw std::runtime_error("Empty input file");
            }
        } else {
            for (int i = 4; i < argc; ++i) {
                files.push_back(argv[i]);
            }
        }

        //

        rpmprops_t props;

        mfile propsconfig(propsfile);
        propsparser::parse_props(propsconfig, props);

        //

        std::string cpiofile = output + ".cpio";

        create_cpio(cpiofile, prefix, files, props);

        //

        std::string header;
        mfile payload = make_rpm(props, cpiofile, header);

        ::unlink(output.c_str());
        int fd = ::open(output.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0666);
        
        if (fd < 0) {
            throw std::runtime_error("Could not open: " + output);
        }

        if (::write(fd, header.c_str(), header.size()) != (ssize_t)header.size()) {
            throw std::runtime_error("Could not write to : " + output);
        }

        if (::write(fd, payload.addr, payload.size) != (ssize_t)payload.size) {
            throw std::runtime_error("Could not write to : " + output);
        }

        ::close(fd);

        ::unlink((cpiofile + ".gz").c_str());
        ::unlink(cpiofile.c_str());

    } catch (std::exception& e) {
        std::cout << "ERROR: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x950, %rsp            # imm = 0x950
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x10808(%rip), %rsi     # 0x316c0
callq 0x45e0
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movb %al, -0x11(%rbp)
cmpl $0x5, -0x8(%rbp)
jl 0x20ee2
testb $0x1, -0x11(%rbp)
je 0x21010
cmpl $0x6, -0x8(%rbp)
je 0x21010
movq 0x190df(%rip), %rdi     # 0x39fc8
leaq 0x107d3(%rip), %rsi     # 0x316c3
callq 0x43d0
movq %rax, -0x850(%rbp)
jmp 0x20efe
movq -0x850(%rbp), %rdi
movq 0x190dc(%rip), %rsi     # 0x39fe8
callq 0x4720
movq %rax, -0x858(%rbp)
jmp 0x20f1a
movq -0x858(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x43d0
movq %rax, -0x860(%rbp)
jmp 0x20f36
movq -0x860(%rbp), %rdi
leaq 0x10786(%rip), %rsi     # 0x316ca
callq 0x43d0
movq %rax, -0x868(%rbp)
jmp 0x20f52
movq -0x868(%rbp), %rdi
movq 0x19088(%rip), %rsi     # 0x39fe8
callq 0x4720
movq %rax, -0x870(%rbp)
jmp 0x20f6e
movq -0x870(%rbp), %rdi
leaq 0x10796(%rip), %rsi     # 0x31712
callq 0x43d0
movq %rax, -0x878(%rbp)
jmp 0x20f8a
movq -0x878(%rbp), %rdi
movq 0x19050(%rip), %rsi     # 0x39fe8
callq 0x4720
movq %rax, -0x880(%rbp)
jmp 0x20fa6
movq -0x880(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x43d0
movq %rax, -0x888(%rbp)
jmp 0x20fc2
movq -0x888(%rbp), %rdi
leaq 0x10745(%rip), %rsi     # 0x31715
callq 0x43d0
movq %rax, -0x890(%rbp)
jmp 0x20fde
movq -0x890(%rbp), %rdi
movq 0x18ffc(%rip), %rsi     # 0x39fe8
callq 0x4720
jmp 0x20ff3
movl $0x1, -0x4(%rbp)
jmp 0x21973
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x218d0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8a0(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x898(%rbp)
callq 0x46c0
movq -0x8a0(%rbp), %rsi
movq -0x898(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x45f0
jmp 0x21048
leaq -0x49(%rbp), %rdi
callq 0x4820
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x8b0(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x8a8(%rbp)
callq 0x46c0
movq -0x8b0(%rbp), %rsi
movq -0x8a8(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x45f0
jmp 0x21089
leaq -0x71(%rbp), %rdi
callq 0x4820
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x8c0(%rbp)
leaq -0x99(%rbp), %rdi
movq %rdi, -0x8b8(%rbp)
callq 0x46c0
movq -0x8c0(%rbp), %rsi
movq -0x8b8(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0x45f0
jmp 0x210d0
leaq -0x99(%rbp), %rdi
callq 0x4820
leaq -0xb8(%rbp), %rdi
callq 0x21e50
testb $0x1, -0x11(%rbp)
je 0x212c6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
leaq -0x2c0(%rbp), %rdi
movl $0x8, %edx
callq 0x4060
jmp 0x2110d
movq -0x2c0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x2c0(%rbp,%rax), %rdi
movl $0x1, %esi
callq 0x4120
jmp 0x2112c
leaq -0x2e0(%rbp), %rdi
callq 0x42a0
leaq -0x2c0(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x4390
movq %rax, -0x8c8(%rbp)
jmp 0x21154
movq -0x8c8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x42c0
movb %al, -0x8c9(%rbp)
jmp 0x21172
movb -0x8c9(%rbp), %al
testb $0x1, %al
jne 0x21181
jmp 0x2121c
leaq -0xb8(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x21f50
jmp 0x21196
jmp 0x21138
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x4820
jmp 0x218d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x4820
jmp 0x218c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x99(%rbp), %rdi
callq 0x4820
jmp 0x218be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x218a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x212b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x212a9
leaq -0xb8(%rbp), %rdi
callq 0x23850
testb $0x1, %al
jne 0x2122e
jmp 0x2128c
movl $0x10, %edi
callq 0x42f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x8d8(%rbp)
leaq 0x1050d(%rip), %rsi     # 0x31759
callq 0x41f0
jmp 0x21253
movq -0x8d8(%rbp), %rdi
movq 0x18d7f(%rip), %rsi     # 0x39fe0
movq 0x18d70(%rip), %rdx     # 0x39fd8
callq 0x4430
jmp 0x21990
movq -0x8d8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4630
jmp 0x212a9
leaq -0x2e0(%rbp), %rdi
callq 0x4540
leaq -0x2c0(%rbp), %rdi
callq 0x4050
jmp 0x2139e
leaq -0x2e0(%rbp), %rdi
callq 0x4540
leaq -0x2c0(%rbp), %rdi
callq 0x4050
jmp 0x218a6
movl $0x4, -0x2e4(%rbp)
movl -0x2e4(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x2139c
movq -0x10(%rbp), %rax
movslq -0x2e4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8e8(%rbp)
leaq -0x309(%rbp), %rdi
movq %rdi, -0x8e0(%rbp)
callq 0x46c0
movq -0x8e8(%rbp), %rsi
movq -0x8e0(%rbp), %rdx
leaq -0x308(%rbp), %rdi
callq 0x45f0
jmp 0x21324
leaq -0xb8(%rbp), %rdi
leaq -0x308(%rbp), %rsi
callq 0x23890
jmp 0x21339
leaq -0x308(%rbp), %rdi
callq 0x4540
leaq -0x309(%rbp), %rdi
callq 0x4820
movl -0x2e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2e4(%rbp)
jmp 0x212d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x2138b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x4540
leaq -0x309(%rbp), %rdi
callq 0x4820
jmp 0x218a6
jmp 0x2139e
leaq -0x730(%rbp), %rdi
callq 0x238c0
jmp 0x213ac
leaq -0x748(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x22ed0
jmp 0x213be
leaq -0x748(%rbp), %rdi
leaq -0x730(%rbp), %rsi
callq 0x66c0
jmp 0x213d3
leaq 0x10390(%rip), %rdx     # 0x3176a
leaq -0x768(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x23340
jmp 0x213ec
leaq -0x768(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
leaq -0x730(%rbp), %rcx
callq 0x200a0
jmp 0x2140f
leaq -0x788(%rbp), %rdi
movq %rdi, -0x8f0(%rbp)
callq 0x42a0
movq -0x8f0(%rbp), %rcx
leaq -0x7a0(%rbp), %rdi
leaq -0x730(%rbp), %rsi
leaq -0x768(%rbp), %rdx
callq 0x1f710
jmp 0x21445
leaq -0x70(%rbp), %rdi
movq %rdi, -0x900(%rbp)
callq 0x4100
movq %rax, %rdi
callq 0x41c0
movq -0x900(%rbp), %rdi
callq 0x4100
movq %rax, %rdi
xorl %eax, %eax
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x4340
movl %eax, -0x8f4(%rbp)
jmp 0x21485
movl -0x8f4(%rbp), %eax
movl %eax, -0x7a4(%rbp)
cmpl $0x0, -0x7a4(%rbp)
jge 0x215a1
movb $0x1, -0x7c9(%rbp)
movl $0x10, %edi
callq 0x42f0
movq %rax, -0x908(%rbp)
leaq 0x102b3(%rip), %rsi     # 0x31770
leaq -0x7c8(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x22dc0
jmp 0x214cf
movq -0x908(%rbp), %rdi
leaq -0x7c8(%rbp), %rsi
callq 0x4230
jmp 0x214e4
movq -0x908(%rbp), %rdi
movb $0x0, -0x7c9(%rbp)
movq 0x18ae7(%rip), %rsi     # 0x39fe0
movq 0x18ad8(%rip), %rdx     # 0x39fd8
callq 0x4430
jmp 0x21990
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x2189a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x2188e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x21882
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x21876
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x2186a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x21585
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x7c8(%rbp), %rdi
callq 0x4540
testb $0x1, -0x7c9(%rbp)
jne 0x21590
jmp 0x2159c
movq -0x908(%rbp), %rdi
callq 0x4630
jmp 0x2186a
movl -0x7a4(%rbp), %eax
movl %eax, -0x91c(%rbp)
leaq -0x788(%rbp), %rdi
movq %rdi, -0x928(%rbp)
callq 0x4100
movq -0x928(%rbp), %rdi
movq %rax, -0x918(%rbp)
callq 0x4270
movl -0x91c(%rbp), %edi
movq -0x918(%rbp), %rsi
movq %rax, %rdx
callq 0x4710
movq %rax, -0x910(%rbp)
jmp 0x215f1
leaq -0x788(%rbp), %rdi
callq 0x4270
movq %rax, %rcx
movq -0x910(%rbp), %rax
cmpq %rcx, %rax
je 0x216be
movb $0x1, -0x7f1(%rbp)
movl $0x10, %edi
callq 0x42f0
movq %rax, -0x930(%rbp)
leaq 0x10152(%rip), %rsi     # 0x31781
leaq -0x7f0(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x22dc0
jmp 0x21641
movq -0x930(%rbp), %rdi
leaq -0x7f0(%rbp), %rsi
callq 0x4230
jmp 0x21656
movq -0x930(%rbp), %rdi
movb $0x0, -0x7f1(%rbp)
movq 0x18975(%rip), %rsi     # 0x39fe0
movq 0x18966(%rip), %rdx     # 0x39fd8
callq 0x4430
jmp 0x21990
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x216a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x7f0(%rbp), %rdi
callq 0x4540
testb $0x1, -0x7f1(%rbp)
jne 0x216ad
jmp 0x216b9
movq -0x930(%rbp), %rdi
callq 0x4630
jmp 0x2186a
movl -0x7a4(%rbp), %edi
movq -0x7a0(%rbp), %rsi
movq -0x790(%rbp), %rdx
callq 0x4710
movq %rax, -0x938(%rbp)
jmp 0x216e0
movq -0x938(%rbp), %rax
cmpq -0x790(%rbp), %rax
je 0x217a2
movb $0x1, -0x819(%rbp)
movl $0x10, %edi
callq 0x42f0
movq %rax, -0x940(%rbp)
leaq 0x1006e(%rip), %rsi     # 0x31781
leaq -0x818(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x22dc0
jmp 0x21725
movq -0x940(%rbp), %rdi
leaq -0x818(%rbp), %rsi
callq 0x4230
jmp 0x2173a
movq -0x940(%rbp), %rdi
movb $0x0, -0x819(%rbp)
movq 0x18891(%rip), %rsi     # 0x39fe0
movq 0x18882(%rip), %rdx     # 0x39fd8
callq 0x4430
jmp 0x21990
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x21786
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x818(%rbp), %rdi
callq 0x4540
testb $0x1, -0x819(%rbp)
jne 0x21791
jmp 0x2179d
movq -0x940(%rbp), %rdi
callq 0x4630
jmp 0x2186a
movl -0x7a4(%rbp), %edi
callq 0x41b0
jmp 0x217af
leaq 0xfd99(%rip), %rdx      # 0x3154f
leaq -0x840(%rbp), %rdi
leaq -0x768(%rbp), %rsi
callq 0x23340
jmp 0x217cb
leaq -0x840(%rbp), %rdi
callq 0x4100
movq %rax, %rdi
callq 0x41c0
leaq -0x840(%rbp), %rdi
callq 0x4540
leaq -0x768(%rbp), %rdi
callq 0x4100
movq %rax, %rdi
callq 0x41c0
leaq -0x7a0(%rbp), %rdi
callq 0x232f0
leaq -0x788(%rbp), %rdi
callq 0x4540
leaq -0x768(%rbp), %rdi
callq 0x4540
leaq -0x748(%rbp), %rdi
callq 0x232f0
leaq -0x730(%rbp), %rdi
callq 0x23de0
leaq -0xb8(%rbp), %rdi
callq 0x22bf0
leaq -0x98(%rbp), %rdi
callq 0x4540
leaq -0x70(%rbp), %rdi
callq 0x4540
leaq -0x48(%rbp), %rdi
callq 0x4540
jmp 0x2196c
leaq -0x7a0(%rbp), %rdi
callq 0x232f0
leaq -0x788(%rbp), %rdi
callq 0x4540
leaq -0x768(%rbp), %rdi
callq 0x4540
leaq -0x748(%rbp), %rdi
callq 0x232f0
leaq -0x730(%rbp), %rdi
callq 0x23de0
leaq -0xb8(%rbp), %rdi
callq 0x22bf0
leaq -0x98(%rbp), %rdi
callq 0x4540
leaq -0x70(%rbp), %rdi
callq 0x4540
leaq -0x48(%rbp), %rdi
callq 0x4540
movl -0x24(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2197f
movq -0x20(%rbp), %rdi
callq 0x4080
movq %rax, -0x848(%rbp)
movq 0x186d1(%rip), %rdi     # 0x39fc8
leaq 0xfe99(%rip), %rsi      # 0x31797
callq 0x43d0
movq %rax, -0x948(%rbp)
jmp 0x2190c
movq -0x848(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0x948(%rbp), %rdi
movq %rax, %rsi
callq 0x43d0
movq %rax, -0x950(%rbp)
jmp 0x21934
movq -0x950(%rbp), %rdi
movq 0x186a6(%rip), %rsi     # 0x39fe8
callq 0x4720
jmp 0x21949
movl $0x1, -0x4(%rbp)
callq 0x4550
jmp 0x21973
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4550
jmp 0x2196a
jmp 0x2197f
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x950, %rsp            # imm = 0x950
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x4440
movq %rax, %rdi
callq 0x23fd0