#!/usr/bin/env bash
#
# lib/exec/publish
#
# Copyright (c) 2020-     RAP <contacto@partidopirata.com.ar>
# Copyright (c) 2011-2016 LibreVPN <vpn@hackcoop.com.ar>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General
# Public License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

. "${RAP_LIBDIR}"/common

requires systemctl grep ps wc ip

while getopts "dh" arg; do
  case $arg in
    h) help ${self} ; exit 0;;
    d) set -x;;
  esac
done
let OPTIND--; shift ${OPTIND}

# Se usa mucho el "|| :" (OR nada) para evitar que se corte 
# el script cuando devuelven mal los comandos

# Proceso de tinc
tincps="$(ps --no-headers -C tincd,tincd_pip -o pid,cmd || :)"
if [ -z "${tincps}" ]; then
  notincps=true
  msg_orange "No se encontró el proceso de tinc"
elif [ "$(echo "${tincps}" | wc -l)" -ne 1 ]; then
  manytincsps=true
  msg_orange "Se encontró más de un proceso de tinc"
else
  msg "Se encontró el proceso de tinc"
fi

# Servicio principal de tinc
tincsvstatus="$(systemctl status tinc 2>/dev/null || :)"
if [ -z "${tincsvstatus}" ]; then
  notincsv=true
  msg_orange "No se encontró el servicio principal de tinc"
else
  tincsv="$(systemctl show tinc)"
  echo "${tincsv}" | grep -q UnitFileState=enabled && enabled=si || enabled=no
  echo "${tincsv}" | grep -q ActiveState=active && active=si || active=no
  msgtxt="Se encontró el servicio principal de tinc: inicio en arranque=${enabled}, activo ahora=${active}"
  if [ "${active}" = si ]; then msg "${msgtxt}"
  else msg_orange "${msgtxt}"; fi
fi

# Servicio de tinc@rap (si existe el principal)
if [ "${notincsv}" != true ]; then
  tincrapsvstatus="$(systemctl status tinc@${NETWORK} || :)"
  if [ -z "${tincrapsvstatus}" ]; then
    msg_orange "No se encontró el servicio de tinc@${NETWORK}"
  else
    tincsv="$(systemctl show tinc@${NETWORK})"
    echo "${tincsv}" | grep -q UnitFileState=enabled && enabled=si || enabled=no
    echo "${tincsv}" | grep -q ActiveState=active && active=si || active=no
    msgtxt="Se encontró el servicio de tinc@${NETWORK}: inicio en arranque=${enabled}, activo ahora=${active}"
    if [ "${active}" = si ]; then msg "${msgtxt}"
    else msg_orange "${msgtxt}"; fi
  fi
fi

# Interface de red
iplink="$(ip link show "${NETWORK}" 2>/dev/null || :)"
if [ -z "${iplink}" ]; then
  msg_orange "No se encontró la interface de red de la ${NETWORK}"
else
  echo "${iplink}" | grep -q 'state UNKNOWN' && interface=true || interface=false
  if [ "${interface}" != true ]; then
    msg_orange "Interface de red de la ${NETWORK} encontrada, pero caída"
  else
    msg "Interface de red de la ${NETWORK} encontrada y andando"
    # IPv6 local
    ipaddress="$(ip address show "${NETWORK}" | grep inet6 | tr -s ' ' | cut -d' ' -f3)"
    if [ -z "${ipaddress}" ]; then
      msg_orange "No se encontró ninguna IP de ${NETWORK}"
    elif [ "$(echo "${ipaddress}" | wc -l)" -eq 1 ]; then
      msg_orange "Se encontró una sola IP: ${ipaddress}"
    else
      iprap="$(echo "${ipaddress}" | grep "${ULA_PREFIX}" || :)"
      if [ -z "${iprap}" ]; then
        msg_orange "Se encontraron varias IPs de ${NETWORK}, pero ninguna válida"
      else
        msg "Tu IP de ${NETWORK} es: ${iprap}"
      fi
    fi
  fi
fi

# Archivo de hosts
hostslines="$(grep "${HOSTS_COMMENT}" /etc/hosts || :)"
if [ -z "${hostslines}" ]; then
  msg_orange "No se encontraron nodos en el archivo de hosts"
else
  msg "Nodos en el archivo de hosts:"
  echo "${hostslines}" | cut -d' ' -f1,2 | column -t
fi


exit 0
