#!/bin/bash
# desc: permite cambiar el listado de hosts a bloquear
# lastact: arturo@2022-5-5

FICHFUNCS="/usr/bin/vx-funcs-bash"
[ -f "${FICHFUNCS}" ] && . "${FICHFUNCS}" "null"

# Mostramos la ayuda si se pasa como parámetro -h o --help:
AYUDA="El script cambia el listado de URLs a bloquear (se registrarán en el fichero /etc/hosts)."
AYUDA+="\nDebe ejecutarse como root, y puedo o no pasarse parámetros.  En caso de no pasarse parámetros se interactuará con el usuario."
AYUDA+="\nEn caso de pasar un parámetro debe ser un número entero (1-8):"
AYUDA+="\n1) Feaknews
    2) Porno
    3) Social (redes sociales)
    4) Gambling (juegos online)
    5) Fakenews, Gambling, Porno
    6) Fakenews, Porno, Social
    7) Gambling, Porno, Social
    8) Todo => Feaknews, Gambling, Porno y Social (opción por defecto)"
EJEMPLOS=("${0} # Se interactuará con el usuario para pedirle una opción")
EJEMPLOS=("${0} \"8\" # Se bloqueará Todo: Feaknews, Gambling, Porno y Social")
vx-check_need_help "${1}" &&
    vx-show_help "${0}" "${AYUDA}" "${EJEMPLOS[@]}" &&
    exit 0

# Comprobamos que el usuario root es que quiere ejecutar el script:
! vx-check_user_root && \
echo "=> Necesitas ser usuario root para poder gestionar el bloqueo de hosts" && \
exit 1

FICHEROS_BLOQUEO=( hosts.unified_hosts-fakenews
hosts.unified_hosts-porn
hosts.unified_hosts-social
hosts.unified_hosts-gambling
hosts.unified_hosts-fakenews_gambling_porn
hosts.unified_hosts-fakenews_porn_social
hosts.unified_hosts-gambling_porn_social 
hosts.unified_hosts-fakenews_gambling_porn_social )

function vx_conf_blockhosts_enlazar()
{
    if [[ "${1}" == 0 ]] ; then
        FICHORIG="/etc/hosts.orig"
        [[ -f "${FICHORIG}" ]] && \
        {
        ln -sf "${FICHORIG}" "/usr/share/divert/etc/hosts" && \
        echo "=> Creamos el enlace simbólico: ${FICHORIG} => /usr/share/divert/etc/hosts" ;
        } || \
        {
        echo "=> ¡¡Error!! Problemas para enlazar: ${FICHORIG} => /usr/share/divert/etc/hosts"
        exit 1 ;
        }
    else
        OPCION="${1}"
        ln -sf "/usr/share/vitalinux/hosts/${FICHEROS_BLOQUEO[$(( OPCION - 1 ))]}" "/usr/share/divert/etc/hosts" && \
        echo "=> Creamos el enlace simbólico: /usr/share/vitalinux/hosts/${FICHEROS_BLOQUEO[${1}]} => /usr/share/divert/etc/hosts"
    fi
}

function vx_conf_blockhosts()
{
    read -r -t 15 -n 1 -p $'Elige las lista de urls a bloquear [1-4] (tienes 15 segundos):
    0) Sin Filtrado, permitir todo
    1) Feaknews
    2) Porno
    3) Social (redes sociales)
    4) Gambling (juegos online)
    5) Fakenews, Gambling, Porno
    6) Fakenews, Porno, Social
    7) Gambling, Porno, Social
    8) Todo => Feaknews, Gambling, Porno y Social (opción por defecto)
Opción Escogida: ' OPCION
    OPCION="${OPCION:-"8"}"
    REGEX='[0-8]'
    ! [[ "${OPCION}" =~ ^${REGEX}$ ]] && \
    {
        vx-show_help "vx-blockhosts" "${AYUDA}" "${EJEMPLOS[@]}"
        exit 2 ;
    }
    # echo "=> La opción escogida es: ${OPCION}"
    # echo "=> Llamamos a la función vx_conf_blockhosts_enlazar con el parámetro $(( OPCION - 1 ))"
    vx_conf_blockhosts_enlazar "${OPCION}"
}

[[ -z "${1}" ]] && vx_conf_blockhosts && exit 0
REGEX='[0-8]'
[[ "${1}" =~ ^${REGEX}$ ]] && OPCION="${1}" || \
{
    vx-show_help "${0}" "${AYUDA}" "${EJEMPLOS[@]}"
    exit 0 ;
}
vx_conf_blockhosts_enlazar "${OPCION}"

resolvectl flush-caches

exit 0