#include <iostream>
#include <cstdlib>
#include <csignal>
#include "Server.hpp"

volatile sig_atomic_t g_stop = 0;

void  handleSig(int)
{
      g_stop = 1;
}

int	main(int argc, char **argv)
{
	if (argc != 3)
	{
		std::cerr << "Usage: ./ircserv <port> <password>" << std::endl;
		return (1);
	}
	int port = std::atoi(argv[1]);
	if (port <= 0 || port > 65535)
	{
		std::cerr << "Error: invalid port" << std::endl;
		return (1);
	}
	signal(SIGINT, handleSig);
    signal(SIGQUIT, handleSig);
	try
	{
		Server server(port, argv[2]);
		server.start();
		server.run();
	}
	catch (std::exception &e)
	{
		std::cerr << "Error: " << e.what() << std::endl;
		return (1);
	}
	return (0);
}
