/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   init.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: lperrey <lperrey@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/10/08 09:22:12 by lperrey           #+#    #+#             */
/*   Updated: 2021/12/10 10:28:16 by hulamy           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

static char	*init_prompt_base(void);
static int	init_readline_hook(void);
static int	shlvl_init(void);

int	init(t_all *c)
{
	ft_bzero(c, sizeof (*c));
//	rl_instream = stdin;
	rl_outstream = stderr;
//	rl_outstream = stdout;
	rl_startup_hook = init_readline_hook;
	readline(NULL);
	rl_startup_hook = NULL;
	environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
	if (!environ)
		return (ft_reti_perror(0, "ft_dup_2d_arr(environ)"));
	c->path = retrieve_path();
	if (!shlvl_init())
		return (ft_reti_perror(0, "shlvl_init()"));
	c->prompt_base = init_prompt_base();
	if (!c->prompt_base)
		return (ft_reti_perror(0, "init_prompt_base()"));
	c->prompt = init_prompt(c->prompt_base);
	if (!c->prompt)
		return (ft_reti_perror(0, "init_prompt()"));
	set_signals_behaviour();
	return (1);
}

static int	shlvl_init(void)
{
	char	*tmp;
	int		ret;

	tmp = getenv("SHLVL");
	if (tmp && ft_isdigit_str(tmp))
	{
		tmp = ft_itoa(ft_atoi(tmp) + 1);
		if (!tmp)
			return (0);
		tmp = ft_strjoinfree_s2("SHLVL=", tmp);
		if (!tmp)
			return (0);
		ret = export_var(tmp);
		free(tmp);
	}
	else
		ret = export_var("SHLVL=1");
	if (ret == -1)
		return (0);
	return (1);
}

static char	*init_prompt_base(void)
{
	char	*prompt_base;
	char	*tmp;

	tmp = getenv("USER");
	if (!tmp)
		tmp = getenv("LOGNAME");
	if (!tmp)
		tmp = U_DEFAULT_USER;
	prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp);
	if (!prompt_base)
		return (NULL);
	prompt_base = ft_strjoinfree_s1(prompt_base, "@");
	if (!prompt_base)
		return (NULL);
	tmp = getenv("NAME");
	if (!tmp)
		tmp = U_DEFAULT_NAME;
	prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
	if (!prompt_base)
		return (NULL);
	prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
	if (!prompt_base)
		return (NULL);
	return (prompt_base);
}

char	*init_prompt(char *prompt_base)
{
	char	*prompt;

	prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
	if (!prompt)
		return (NULL);
	prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END);
	if (!prompt)
		return (NULL);
	return (prompt);
}

/*
** set rl_startup_hook with this, for init COLUMNS and LINES variables
** and prevent leak/double_free with **environ
*/
static int	init_readline_hook(void)
{
	rl_done = 1;
	return (0);
}
