// File: /workspace/github_cpp_makefile_star2_O2/gualtieromalde[P]owmread/owmread.C
// Function: main
int main(int nargs, char ** args)
{
	boost::program_options::options_description desc("owmread reads OpenWeatherMap current weather and feed it to FlightGear. Options:");
	desc.add_options()
    	("help", "produce help message")
    	("key", boost::program_options::value<std::string>(), "set OpenWeatherMap api key (if not specified search from file key, or from standard input)")
    	("port", boost::program_options::value<int>(), "set httpd port to communicate with FlightGear (fgfs must run with --httpd=port)")
	("interval", boost::program_options::value<int>()->default_value(10), "number of seconds between each API call. Default is 10. Note that, for the free OpenWeatherMap account the limit is set to 60 calls/minute and 1M calls/month, so a safe limit is interval>=3 s.")
    	("quiet", "does not display output")
;

	boost::program_options::variables_map vm;
	boost::program_options::store(boost::program_options::parse_command_line(nargs, args, desc), vm);
	boost::program_options::notify(vm);   

	bool ovr_key;
	bool isQuiet;

	int sleep_time =  vm["interval"].as<int>(); 
	if(sleep_time<=1)
	{
		std::clog << "Sleep time <= 1 s. This could generate a number of API calls per minute not allowed in OpenWeatherMap free plan. Continue? y/n: ";
		char yn; std::cin >> yn;
		if(yn!='y' || yn!='Y') return 2;	
	}
	else if(sleep_time<3)
	{
		std::clog << "Sleep time < 3 s. This could generate a number of API calls per month not allowed in OpenWeatherMap free plan. Continue? y/n: ";
		char yn; std::cin >> yn;
		if(yn!='y' || yn!='Y') return 2;	
	}
		


	std::string argKey;

	if(vm.count("help"))
	{
		std::clog << desc <<"\n";
		return 1;
	}

	if(vm.count("key")) 
	{
		ovr_key=true;
	      	argKey = vm["key"].as<std::string>();
	}
	else 
		ovr_key=false;

	int port;
	if(vm.count("port"))
	{
		port=vm["port"].as<int>();
	}
	else
	{
		std::cout << "insert port: ";
		std::cin >> port;
	}

	isQuiet = vm.count("quiet");

	std::string myKey;
	std::ifstream keyFile("key");
	if(!ovr_key && keyFile.good() )
	{
		keyFile >> myKey;
		keyFile.close();
	}
	else if(!ovr_key)
	{
		std::clog << "insert key: ";
		std::cin >> myKey;
		std::ofstream kF("key");
		kF << myKey;
		kF.close();
	}
	else
	{
		myKey =	argKey;
	}
	std::chrono::seconds interval( sleep_time ) ;


	while(true)
	{
		auto now = std::chrono::system_clock::now();
		std::time_t now_time = std::chrono::system_clock::to_time_t(now);
		Coord c =readCoord(port);
		Weather w = getWeather(c.lat, c.lon, myKey);
		setMetar(port, w.makeMetar());
		if(!isQuiet) std::cout << std::ctime(&now_time) <<c.lat << " " << c.lon << " " << " " << w.makeMetar();

		std::this_thread::sleep_for( interval ) ;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x618, %rsp            # imm = 0x618
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x2621d(%rip), %rsi     # 0x30629
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
movq 0x37bb8(%rip), %rax     # 0x41fe0
movl (%rax), %edx
movl %edx, %ecx
shrl %ecx
leaq 0x398(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x84f0
leaq 0x90(%rsp), %rdi
callq 0x8988
leaq 0x398(%rsp), %rdi
callq 0x8750
leaq 0x90(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x2620b(%rip), %rsi     # 0x3067a
leaq 0x26209(%rip), %rdx     # 0x3067f
callq 0x85b0
movq %rax, %r14
callq 0xae68
leaq 0x2620a(%rip), %rsi     # 0x30694
leaq 0x26207(%rip), %rcx     # 0x30698
movq %r14, %rdi
movq %rax, %rdx
callq 0x8610
movq %rax, %r14
callq 0xae6f
leaq 0x26248(%rip), %rsi     # 0x306f3
leaq 0x26246(%rip), %rcx     # 0x306f8
movq %r14, %rdi
movq %rax, %rdx
callq 0x8610
movq %rax, %r14
callq 0xae6f
leaq 0x418(%rsp), %rsi
movl $0xa, (%rsi)
movq %rax, %rdi
callq 0xae76
leaq 0x26266(%rip), %rsi     # 0x30748
leaq 0x26268(%rip), %rcx     # 0x30751
movq %r14, %rdi
movq %rax, %rdx
callq 0x8610
leaq 0x26317(%rip), %rsi     # 0x30812
leaq 0x26316(%rip), %rdx     # 0x30818
movq %rax, %rdi
callq 0x85b0
leaq 0x2f8(%rsp), %rdi
callq 0x80e0
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r9
movaps %xmm0, (%r9)
movaps %xmm0, 0x10(%r9)
leaq 0x90(%rsp), %rdi
leaq 0x398(%rsp), %rcx
movl %ebp, %esi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0xaee8
leaq 0x90(%rsp), %rdi
leaq 0x2f8(%rsp), %rsi
xorl %edx, %edx
callq 0x8680
leaq 0x90(%rsp), %rdi
callq 0xb8e4
leaq 0x70(%rsp), %rdi
callq 0xaf98
leaq 0x2f8(%rsp), %rdi
callq 0x8640
leaq 0x261c1(%rip), %rsi     # 0x30748
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x2f8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8050
movq %rax, %rdi
callq 0x11f90
movslq (%rax), %r12
leaq 0x90(%rsp), %rdi
callq 0x8988
cmpq $0x1, %r12
jg 0xa5f8
movq 0x37952(%rip), %rdi     # 0x41f28
leaq 0x26253(%rip), %rsi     # 0x30830
callq 0x8420
movq 0x379cf(%rip), %rdi     # 0x41fb8
leaq 0x90(%rsp), %rsi
callq 0x8030
jmp 0xa625
cmpl $0x2, %r12d
jne 0xa62c
movq 0x37923(%rip), %rdi     # 0x41f28
leaq 0x262a5(%rip), %rsi     # 0x308b1
callq 0x8420
movq 0x379a0(%rip), %rdi     # 0x41fb8
leaq 0x90(%rsp), %rsi
callq 0x8030
pushq $0x2
jmp 0xa6b5
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x26036(%rip), %rsi     # 0x3067a
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x308(%rsp), %rbx
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0xafa8
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x8988
testq %r14, %r14
je 0xa6e4
movq 0x3789b(%rip), %rdi     # 0x41f28
leaq 0x398(%rsp), %rsi
callq 0x83c0
leaq 0x27ae2(%rip), %rsi     # 0x32183
movq %rax, %rdi
callq 0x8420
leaq 0x50(%rsp), %rdi
callq 0x8988
pushq $0x1
popq %rbx
leaq 0x2f8(%rsp), %rdi
callq 0xaffa
leaq 0x398(%rsp), %rdi
callq 0xb02c
movl %ebx, %eax
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25fa9(%rip), %rsi     # 0x30694
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0xafa8
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x8988
testq %r14, %r14
jne 0xa793
leaq 0x25fc7(%rip), %rsi     # 0x306f3
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0xafa8
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x8988
testq %r15, %r15
jne 0xa7eb
movq 0x37807(%rip), %rdi     # 0x41f78
leaq 0x261b8(%rip), %rsi     # 0x30930
callq 0x8420
movq 0x37834(%rip), %rdi     # 0x41fb8
leaq 0xc(%rsp), %rsi
callq 0x8930
jmp 0xa837
leaq 0x25efa(%rip), %rsi     # 0x30694
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x2f8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8050
movq %rax, %rdi
callq 0x1225e
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x88e0
leaq 0x90(%rsp), %rdi
callq 0x8988
jmp 0xa725
leaq 0x25f01(%rip), %rsi     # 0x306f3
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x2f8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8050
movq %rax, %rdi
callq 0x11f90
movl (%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x8988
leaq 0x25fd4(%rip), %rsi     # 0x30812
leaq 0x90(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x87d0
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0xafa8
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8988
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x25e09(%rip), %rsi     # 0x30694
leaq 0x90(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x8070
testq %r14, %r14
jne 0xa8db
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xb0(%rsp,%rax)
jne 0xaa25
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x82e0
leaq 0x90(%rsp), %rdi
callq 0x8480
jmp 0xa8ea
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x88e0
leaq 0x28(%rsp), %rax
movq %r12, (%rax)
leaq 0x2d8(%rsp), %r15
leaq 0x418(%rsp), %r13
leaq 0x2b8(%rsp), %rbp
leaq 0x298(%rsp), %r12
callq 0x8540
cqto
movl $0x3b9aca00, %ecx       # imm = 0x3B9ACA00
idivq %rcx
movq %rax, 0x20(%rsp)
movl 0xc(%rsp), %edi
callq 0x9e5b
movsd %xmm0, 0x18(%rsp)
movsd %xmm1, 0x10(%rsp)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0x83d0
movq %r13, %rdi
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r15, %rsi
callq 0x8ea8
movq %r15, %rdi
callq 0x8988
movl 0xc(%rsp), %r14d
movq %rbp, %rdi
movq %r13, %rsi
callq 0x938c
movl %r14d, %edi
movq %rbp, %rsi
callq 0xa296
movq %rbp, %rdi
callq 0x8988
testq %rbx, %rbx
jne 0xaa16
leaq 0x20(%rsp), %rdi
callq 0x8810
movq 0x375d4(%rip), %rdi     # 0x41f78
movq %rax, %rsi
callq 0x8420
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x85a0
movq %rax, %rdi
leaq 0x277ac(%rip), %rsi     # 0x32170
callq 0x8420
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x85a0
movq %rax, %rdi
leaq 0x2778f(%rip), %rsi     # 0x32170
callq 0x8420
movq %rax, %rdi
leaq 0x27780(%rip), %rsi     # 0x32170
callq 0x8420
movq %rax, %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x938c
movq %r14, %rdi
movq %r12, %rsi
callq 0x81e0
movq %r12, %rdi
callq 0x8988
leaq 0x28(%rsp), %rdi
callq 0xafc2
jmp 0xa912
movq 0x374fc(%rip), %rdi     # 0x41f28
leaq 0x25f0b(%rip), %rsi     # 0x3093e
callq 0x8420
movq 0x37579(%rip), %rdi     # 0x41fb8
leaq 0x30(%rsp), %rsi
callq 0x82e0
leaq 0x25c44(%rip), %rsi     # 0x30694
leaq 0x418(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x8210
leaq 0x418(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x81e0
leaq 0x418(%rsp), %rdi
callq 0x8730
leaq 0x418(%rsp), %rdi
callq 0x85d0
jmp 0xa8ea
movq %rax, %rbx
leaq 0x298(%rsp), %rdi
jmp 0xaacc
jmp 0xaad7
movq %rax, %rbx
leaq 0x418(%rsp), %rdi
callq 0x85d0
jmp 0xaada
jmp 0xaad7
movq %rax, %rbx
leaq 0x2b8(%rsp), %rdi
jmp 0xaacc
movq %rax, %rbx
leaq 0x2d8(%rsp), %rdi
callq 0x8988
jmp 0xaada
jmp 0xaad7
jmp 0xaad7
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8060
jmp 0xaaec
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0xab14
jmp 0xab09
jmp 0xab1f
jmp 0xab09
jmp 0xab1f
jmp 0xab1f
jmp 0xab09
jmp 0xab09
jmp 0xab1f
jmp 0xab1f
jmp 0xab09
jmp 0xab1f
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8988
jmp 0xab22
jmp 0xab2f
jmp 0xab1f
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0xab79
jmp 0xab2f
jmp 0xab2f
jmp 0xab2f
movq %rax, %rbx
jmp 0xab7e
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xb8e4
jmp 0xab49
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0xaf98
jmp 0xab7e
jmp 0xab8f
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8988
jmp 0xab9f
movq %rax, %rbx
jmp 0xab9f
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8988
leaq 0x2f8(%rsp), %rdi
callq 0xaffa
jmp 0xab92
jmp 0xab8f
movq %rax, %rbx
leaq 0x398(%rsp), %rdi
callq 0xb02c
movq %rbx, %rdi
callq 0x84b0