/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_unset.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <stdlib.h>

#include "f_fail.h"
#include "s_lvars.h"
#include "s_struct.h"
#include "u_utils.h"

static t_bool
	check_valid_identifier(const char arg[])
{
	char	*ptr;
	t_bool	r;

	ptr = (char*)arg;
	r = TRUE;
	if (ft_isalpha(ptr[0]) == FALSE)
	{
		r = FALSE;
	}
	if (ptr[0] == '_')
	{
		r = TRUE;
	}
	while (*ptr != '\0')
	{
		if (*ptr == '=')
		{
			r = FALSE;
		}
		ptr++;
	}
	return (r);
}

static void
	b_realloc_env(size_t skip,
				t_msh *msh)
{
	char	**nenvp;
	int8_t	skipped;
	size_t	i;

	i = 0;
	while (msh->envp[i] != NULL)
		i++;
	if (!(nenvp = (char**)malloc(i * sizeof(char*))))
		f_alloc_and_destroy_msh(msh);
	i = 0;
	skipped = 0;
	while (msh->envp[i] != NULL)
	{
		if (i == skip)
		{
			i += 1;
			if (msh->envp[i] == NULL)
				break ;
			skipped = 1;
		}
		if (!(nenvp[i - skipped] = ft_strdup(msh->envp[i])))
			f_alloc_and_destroy_msh(msh);
		i++;
	}
	nenvp[i - 1] = 0;
	ft_delwords(msh->envp);
	msh->envp = nenvp;
}

static t_bool
	b_removed_from_env(const char arg[],
					t_msh *msh)
{
	char	**env_dup;
	size_t	i;

	env_dup = u_get_env_var_names(msh);
	i = 0;
	while (env_dup[i] != NULL)
	{
		if (ft_strncmp(arg, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
		{
			b_realloc_env(i, msh);
			ft_delwords(env_dup);
			return (TRUE);
		}
		i++;
	}
	ft_delwords(env_dup);
	return (FALSE);
}

static void	b_remove_it(const char arg[], t_msh *msh)
{
	if (b_removed_from_env(arg, msh) == FALSE)
	{
		lvars_delone(&msh->vars, arg);
	}
}

uint8_t
	b_unset(char *args[],
			t_msh *msh)
{
	char	**ptr;
	t_bool	next;
	int8_t	r;

	if (args[0] == NULL)
		return (0);
	r = 0;
	ptr = args;
	while (*ptr != NULL)
	{
		next = FALSE;
		if (check_valid_identifier(*ptr) == FALSE)
		{
			f_fail_identifier("unset", *ptr);
			next = TRUE;
			r = 1;
		}
		if (next == FALSE)
		{
			b_remove_it(*ptr, msh);
		}
		ptr++;
	}
	return (r);
}