// File: parth-shel[P]xpac/client.cpp
// Function: main
int main(int argc, char ** argv){
	if(argc<2){
		print_err(1);
		exit(1);
	}

	//Initializing:
	xpac_init();
	
	if(!strcmp(argv[1],"-install")){
		if(argc<3){
			print_err(2);
			exit(1);
		}

		char * pkg_name = strdup(argv[2]);

		//Checking if package available in the repository:
		auto find_itr = universe_list.find(std::string(pkg_name));
		if(find_itr == universe_list.end()){	//Package not available to install in the repo!
			std::cout<<"Sorry, package not available in the repository! Please check the website for more information!"<<std::endl;
			exit(0);
		}

		if(user_installed_list.find(std::string(pkg_name)) != user_installed_list.end()) {
			std::cout<<"Already installed "<<pkg_name<<"!"<<std::endl;
			exit(1);
		}

		//Installing the package:
		bool result = install_package(pkg_name);

		//Terminating:
		if(result)
			terminate(pkg_name);
		else
			exit(1);	//Should not reach here!
	}
	else if(!strcmp(argv[1],"-update")){
		std::cout<<"Updating xpac"<<std::endl;
		std::cout<<"Fetching updated package list from server: "<<std::endl;
		update_from_server();
		parse_universe_of_packages(universe_file);
		std::cout<<"Calculating differences: "<<std::endl;
		calculate_differences();
	}
	else if(!strcmp(argv[1], "-upgrade")){
		std::cout<<"Upgrading all packages"<<std::endl;
		upgrade_all_packages();
	}
	else if(!strcmp(argv[1],"-list")){
		if(argc > 2) {
			if(!strcmp(argv[2],"--installed")){
				if(user_installed_list.size() == 0){
					std::cout<<"No packages installed on this system by xpac!"<<std::endl;
					exit(0);
				}
				std::cout<<"Listing all packages installed on the system: "<<std::endl;
				print_package_set(user_installed_list);
				exit(0);
			}
			else{
				print_err(1);
				exit(1);
			}
		}
		std::cout<<"Listing all packages available for installation: "<<std::endl;
		print_package_set(universe_list);	
	}
	else if(!strcmp(argv[1],"-protect")){
		char * pkg_name = strdup(argv[2]);

		auto find_itr = user_installed_list.find(std::string(pkg_name));
		if(find_itr == user_installed_list.end()){	//Package not available to install in the repo!
			std::cout<<"Sorry, package not installed on the system!"<<std::endl;
			exit(1);
		}

		//Actually protecting the package right now:
		if(std::find(base->get_dep_list()->begin(), base->get_dep_list()->end(), std::string(pkg_name)) == base->get_dep_list()->end()){
			terminate(pkg_name);
		}
		std::cout<<"Package "<<pkg_name<<" successfully added to the protected list"<<std::endl;
		exit(0);
	}
	else if(!strcmp(argv[1],"-help")){
		man_help();
	}
	else if(!strcmp(argv[1],"-remove")){
		auto find_itr = user_installed_list.find(std::string(argv[2]));
		if(find_itr == user_installed_list.end()){	//Package not available to install in the repo!
			std::cout<<"Sorry, the package requested for removal is not installed on the current system!"<<std::endl;
			exit(0);
		}
		calculate_indegrees();
		remove_package(argv[2]);
		std::cout<<"Note: Removal of packages often leads to orphaned packages! Please run xpac -cleanup to remove any orphaned packages!"<<std::endl;
	}
	else if(!strcmp(argv[1],"-cleanup")){
		calculate_indegrees();
		remove_orphans();
	}
	else{
		print_err(1);
		exit(1);
	}
	return 0; //Successful exit
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x6d23
movl $0x1, %edi
callq 0x7850
movl $0x1, %edi
callq 0x44c0
callq 0x7890
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe40f(%rip), %rsi      # 0x15146
callq 0x4590
cmpl $0x0, %eax
jne 0x7049
cmpl $0x3, -0x8(%rbp)
jge 0x6d5f
movl $0x2, %edi
callq 0x7850
movl $0x1, %edi
callq 0x44c0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x4720
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x208(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x46e0
movq -0x208(%rbp), %rsi
movq -0x200(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x4560
jmp 0x6da4
leaq 0x17875(%rip), %rdi     # 0x1e620
leaq -0x40(%rbp), %rsi
callq 0x9440
movq %rax, -0x210(%rbp)
jmp 0x6dbd
movq -0x210(%rbp), %rax
movq %rax, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x47e8
leaq -0x41(%rbp), %rdi
callq 0x4460
leaq 0x1783f(%rip), %rdi     # 0x1e620
callq 0x93e0
movq %rax, -0x60(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x9db0
testb $0x1, %al
jne 0x6dfd
jmp 0x6e57
movq 0x171cc(%rip), %rdi     # 0x1dfd0
leaq 0xe344(%rip), %rsi      # 0x1514f
callq 0x4400
movq %rax, %rdi
movq 0x1719e(%rip), %rsi     # 0x1dfb8
callq 0x4440
xorl %edi, %edi
callq 0x44c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x6e49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x47e8
leaq -0x41(%rbp), %rdi
callq 0x4460
jmp 0x783a
movq -0x18(%rbp), %rax
movq %rax, -0x220(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x46e0
movq -0x220(%rbp), %rsi
movq -0x218(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x4560
jmp 0x6e91
leaq 0x177d8(%rip), %rdi     # 0x1e670
leaq -0x88(%rbp), %rsi
callq 0x9440
movq %rax, -0x228(%rbp)
jmp 0x6ead
movq -0x228(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq 0x177b1(%rip), %rdi     # 0x1e670
callq 0x93e0
movq %rax, -0x98(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x93b0
movb %al, -0x229(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x47e8
leaq -0x89(%rbp), %rdi
callq 0x4460
movb -0x229(%rbp), %al
testb $0x1, %al
jne 0x6f05
jmp 0x6f83
movq 0x170c4(%rip), %rdi     # 0x1dfd0
leaq 0xe29b(%rip), %rsi      # 0x151ae
callq 0x4400
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x4400
movq %rax, %rdi
leaq 0xe541(%rip), %rsi      # 0x1546f
callq 0x4400
movq %rax, %rdi
movq 0x1707b(%rip), %rsi     # 0x1dfb8
callq 0x4440
movl $0x1, %edi
callq 0x44c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x6f72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x47e8
leaq -0x89(%rbp), %rdi
callq 0x4460
jmp 0x783a
movq -0x18(%rbp), %rdi
callq 0x5960
andb $0x1, %al
movb %al, -0x99(%rbp)
testb $0x1, -0x99(%rbp)
je 0x703a
movq -0x18(%rbp), %rax
movq %rax, -0x240(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x46e0
movq -0x240(%rbp), %rsi
movq -0x238(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x4560
jmp 0x6fdb
leaq -0xc0(%rbp), %rdi
callq 0x5ee0
jmp 0x6fe9
leaq -0xc0(%rbp), %rdi
callq 0x47e8
leaq -0xc1(%rbp), %rdi
callq 0x4460
jmp 0x7044
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7029
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x47e8
leaq -0xc1(%rbp), %rdi
callq 0x4460
jmp 0x783a
movl $0x1, %edi
callq 0x44c0
jmp 0x782f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe169(%rip), %rsi      # 0x151c1
callq 0x4590
cmpl $0x0, %eax
jne 0x713a
movq 0x16f63(%rip), %rdi     # 0x1dfd0
movq %rdi, -0x258(%rbp)
leaq 0xe14e(%rip), %rsi      # 0x151c9
callq 0x4400
movq %rax, %rdi
movq 0x16f2e(%rip), %rsi     # 0x1dfb8
movq %rsi, -0x250(%rbp)
callq 0x4440
movq -0x258(%rbp), %rdi
leaq 0xe133(%rip), %rsi      # 0x151d7
callq 0x4400
movq -0x250(%rbp), %rsi
movq %rax, %rdi
callq 0x4440
callq 0x7980
leaq 0x173ac(%rip), %rsi     # 0x1e470
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x41d0
movq -0x248(%rbp), %rdi
callq 0x120e0
jmp 0x70e5
leaq -0xe8(%rbp), %rdi
callq 0x47e8
movq 0x16ed8(%rip), %rdi     # 0x1dfd0
leaq 0xe104(%rip), %rsi      # 0x15203
callq 0x4400
movq %rax, %rdi
movq 0x16eaa(%rip), %rsi     # 0x1dfb8
callq 0x4440
callq 0x4eb0
jmp 0x782d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x47e8
jmp 0x783a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe0d4(%rip), %rsi      # 0x1521d
callq 0x4590
cmpl $0x0, %eax
jne 0x717f
movq 0x16e76(%rip), %rdi     # 0x1dfd0
leaq 0xe0c5(%rip), %rsi      # 0x15226
callq 0x4400
movq %rax, %rdi
movq 0x16e48(%rip), %rsi     # 0x1dfb8
callq 0x4440
callq 0x5340
jmp 0x782b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe0af(%rip), %rsi      # 0x1523d
callq 0x4590
cmpl $0x0, %eax
jne 0x7276
cmpl $0x2, -0x8(%rbp)
jle 0x7243
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0xe08e(%rip), %rsi      # 0x15243
callq 0x4590
cmpl $0x0, %eax
jne 0x722f
leaq 0x174aa(%rip), %rdi     # 0x1e670
callq 0x9de0
cmpq $0x0, %rax
jne 0x71fa
movq 0x16df8(%rip), %rdi     # 0x1dfd0
leaq 0xe070(%rip), %rsi      # 0x1524f
callq 0x4400
movq %rax, %rdi
movq 0x16dca(%rip), %rsi     # 0x1dfb8
callq 0x4440
xorl %edi, %edi
callq 0x44c0
movq 0x16dcf(%rip), %rdi     # 0x1dfd0
leaq 0xe075(%rip), %rsi      # 0x1527d
callq 0x4400
movq %rax, %rdi
movq 0x16da1(%rip), %rsi     # 0x1dfb8
callq 0x4440
leaq 0x1744d(%rip), %rdi     # 0x1e670
callq 0x12010
xorl %edi, %edi
callq 0x44c0
movl $0x1, %edi
callq 0x7850
movl $0x1, %edi
callq 0x44c0
movq 0x16d86(%rip), %rdi     # 0x1dfd0
leaq 0xe05b(%rip), %rsi      # 0x152ac
callq 0x4400
movq %rax, %rdi
movq 0x16d58(%rip), %rsi     # 0x1dfb8
callq 0x4440
leaq 0x173b4(%rip), %rdi     # 0x1e620
callq 0x12010
jmp 0x7829
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe059(%rip), %rsi      # 0x152de
callq 0x4590
cmpl $0x0, %eax
jne 0x75db
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x4720
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x119(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x46e0
movq -0x268(%rbp), %rsi
movq -0x260(%rbp), %rdx
leaq -0x118(%rbp), %rdi
callq 0x4560
jmp 0x72e4
leaq 0x17385(%rip), %rdi     # 0x1e670
leaq -0x118(%rbp), %rsi
callq 0x9440
movq %rax, -0x270(%rbp)
jmp 0x7300
movq -0x270(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x47e8
leaq -0x119(%rbp), %rdi
callq 0x4460
leaq 0x17343(%rip), %rdi     # 0x1e670
callq 0x93e0
movq %rax, -0x128(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x9db0
testb $0x1, %al
jne 0x7352
jmp 0x73b5
movq 0x16c77(%rip), %rdi     # 0x1dfd0
leaq 0xdf87(%rip), %rsi      # 0x152e7
callq 0x4400
movq %rax, %rdi
movq 0x16c49(%rip), %rsi     # 0x1dfb8
callq 0x4440
movl $0x1, %edi
callq 0x44c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x73a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x47e8
leaq -0x119(%rbp), %rdi
callq 0x4460
jmp 0x783a
movq 0x170fc(%rip), %rdi     # 0x1e4b8
callq 0x11810
movq %rax, %rdi
callq 0x95e0
movq %rax, -0x138(%rbp)
movq 0x170e1(%rip), %rdi     # 0x1e4b8
callq 0x11810
movq %rax, %rdi
callq 0x9650
movq %rax, -0x140(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x161(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x46e0
movq -0x280(%rbp), %rsi
movq -0x278(%rbp), %rdx
leaq -0x160(%rbp), %rdi
callq 0x4560
jmp 0x7428
movq -0x138(%rbp), %rdi
movq -0x140(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0x9e40
movq %rax, -0x288(%rbp)
jmp 0x744b
movq -0x288(%rbp), %rax
movq %rax, -0x130(%rbp)
movq 0x17058(%rip), %rdi     # 0x1e4b8
callq 0x11810
movq %rax, -0x290(%rbp)
jmp 0x746e
movq -0x290(%rbp), %rdi
callq 0x9650
movq %rax, -0x170(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x9e00
movb %al, -0x291(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x47e8
leaq -0x161(%rbp), %rdi
callq 0x4460
movb -0x291(%rbp), %al
testb $0x1, %al
jne 0x74c1
jmp 0x7594
movq -0xf0(%rbp), %rax
movq %rax, -0x2a8(%rbp)
leaq -0x191(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x46e0
movq -0x2a8(%rbp), %rsi
movq -0x2a0(%rbp), %rdx
leaq -0x190(%rbp), %rdi
callq 0x4560
jmp 0x74fe
leaq -0x190(%rbp), %rdi
callq 0x5ee0
jmp 0x750c
leaq -0x190(%rbp), %rdi
callq 0x47e8
leaq -0x191(%rbp), %rdi
callq 0x4460
jmp 0x7594
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x754c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x47e8
leaq -0x161(%rbp), %rdi
callq 0x4460
jmp 0x783a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7583
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x47e8
leaq -0x191(%rbp), %rdi
callq 0x4460
jmp 0x783a
movq 0x16a35(%rip), %rdi     # 0x1dfd0
leaq 0xdd71(%rip), %rsi      # 0x15313
callq 0x4400
movq %rax, %rdi
movq -0xf0(%rbp), %rsi
callq 0x4400
movq %rax, %rdi
leaq 0xdd5c(%rip), %rsi      # 0x1531c
callq 0x4400
movq %rax, %rdi
movq 0x169e9(%rip), %rsi     # 0x1dfb8
callq 0x4440
xorl %edi, %edi
callq 0x44c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe285(%rip), %rsi      # 0x1586f
callq 0x4590
cmpl $0x0, %eax
jne 0x75fe
callq 0x11080
jmp 0x7825
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xdd39(%rip), %rsi      # 0x15346
callq 0x4590
cmpl $0x0, %eax
jne 0x77e8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x1c1(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x46e0
movq -0x2b8(%rbp), %rsi
movq -0x2b0(%rbp), %rdx
leaq -0x1c0(%rbp), %rdi
callq 0x4560
jmp 0x7659
leaq 0x17010(%rip), %rdi     # 0x1e670
leaq -0x1c0(%rbp), %rsi
callq 0x9440
movq %rax, -0x2c0(%rbp)
jmp 0x7675
movq -0x2c0(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x47e8
leaq -0x1c1(%rbp), %rdi
callq 0x4460
leaq 0x16fce(%rip), %rdi     # 0x1e670
callq 0x93e0
movq %rax, -0x1d0(%rbp)
leaq -0x1a0(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x9db0
testb $0x1, %al
jne 0x76c7
jmp 0x7727
movq 0x16902(%rip), %rdi     # 0x1dfd0
leaq 0xdc79(%rip), %rsi      # 0x1534e
callq 0x4400
movq %rax, %rdi
movq 0x168d4(%rip), %rsi     # 0x1dfb8
callq 0x4440
xorl %edi, %edi
callq 0x44c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7716
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x47e8
leaq -0x1c1(%rbp), %rdi
callq 0x4460
jmp 0x783a
callq 0x5fe0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2d0(%rbp)
leaq -0x1f1(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x46e0
movq -0x2d0(%rbp), %rsi
movq -0x2c8(%rbp), %rdx
leaq -0x1f0(%rbp), %rdi
callq 0x4560
jmp 0x776a
leaq -0x1f0(%rbp), %rdi
callq 0x6630
jmp 0x7778
leaq -0x1f0(%rbp), %rdi
callq 0x47e8
leaq -0x1f1(%rbp), %rdi
callq 0x4460
movq 0x16839(%rip), %rdi     # 0x1dfd0
leaq 0xdc01(%rip), %rsi      # 0x1539f
callq 0x4400
movq %rax, %rdi
movq 0x1680b(%rip), %rsi     # 0x1dfb8
callq 0x4440
jmp 0x7823
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x77da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x47e8
leaq -0x1f1(%rbp), %rdi
callq 0x4460
jmp 0x783a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xdc1e(%rip), %rsi      # 0x15415
callq 0x4590
cmpl $0x0, %eax
jne 0x780d
callq 0x5fe0
callq 0x6990
jmp 0x7821
movl $0x1, %edi
callq 0x7850
movl $0x1, %edi
callq 0x44c0
jmp 0x7823
jmp 0x7825
jmp 0x7827
jmp 0x7829
jmp 0x782b
jmp 0x782d
jmp 0x782f
xorl %eax, %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x46d0
nopw %cs:(%rax,%rax)