#include "client.h"

int	usage(void)
{
	ft_printf("usage: ./client [server pid] [message]\n");
	return (0);
}

void	send_char(char c, int mask, int server_pid)
{
	if ((c & mask) != 0)
		kill(server_pid, SIGUSR1);
	if ((c & mask) == 0)
		kill(server_pid, SIGUSR2);
}

void	send_message(int sig_num)
{
	(void)sig_num;
	client.mask >>= 1;
	if (client.mask == 0)
	{
		client.mask = 1 << 6;
		if (client.text[client.count_char] == '\0')
			client.done = 1;
		(client.count_char)++;
	}
	if (client.done == 0)
		send_char(client.text[client.count_char], client.mask, client.srv_pid);
}

void	init_client(int pid, char *msg)
{
	client.mask = 1 << 7;
	client.count_char = 0;
	client.srv_pid = pid;
	client.text = msg;
	client.done = 0;
}

int	main(int ac, char **av)
{
	signal(SIGUSR1, send_message);
	if (ac != 3)
		return (usage());
	init_client(ft_atoi(av[1]), av[2]);
	kill((int)getpid(), SIGUSR1);
	while (client.done == 0)
		;
	return (0);
}
