#!/bin/bash
#
# lib/msg
#
# Copyright (c) 2011-2013 LibreVPN <vpn@hackcoop.com.ar>
#
# See AUTHORS for a list of contributors
#
# 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/>.
#
#
# 

# Hace falta tener gettext instalado
requires gettext

# Copiado de makepkg
#
#   makepkg - make packages compatible for use with pacman
#   Generated from makepkg.sh.in; do not edit by hand.
#
#   Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org>
#   Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
#   Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
#   Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
#   Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
#   Copyright (c) 2006 by Alex Smith <alex@alex-smith.me.uk>
#   Copyright (c) 2006 by Andras Voroskoi <voroskoi@frugalware.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 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 General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# check if messages are to be printed using color
unset ALL_OFF BOLD BLUE GREEN RED YELLOW
# prefer terminal safe colored and bold text when tput is supported
if tput setaf 0 &>/dev/null; then
  ALL_OFF="$(tput sgr0)"
  BOLD="$(tput bold)"
  BLUE="$(tput setaf 4)"
  GREEN="$(tput setaf 2)"
  RED="$(tput setaf 1)"
  YELLOW="$(tput setaf 3)"
else
  ALL_OFF="\e[1;0m"
  BOLD="\e[1;1m"
  BLUE="\e[1;34m"
  GREEN="\e[1;32m"
  RED="\e[1;31m"
  YELLOW="\e[1;33m"
fi
readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
# end makepkg

# color por defecto
DC="${!DEF_COLOR:-${GREEN}}"

# La base para todos los mensajes, se imprime en stderr
_msg() {
    msg="$1"; shift
    printf "${BOLD}${RED}>${ALL_OFF} $msg${ALL_OFF}\n" "$@" |
    sed "s/\*\+\([^ ][^\*]\+\)\*\+/${BOLD}\1${ALL_OFF}${DC}/g" 1>&2
}

msg() {
    msg="$(gettext "$1")"; shift
    _msg "${DC}$msg" "$@"
}

error() {
    msg="$(gettext "$1")"; shift
    _msg "${BOLD}${RED}$(gettext "ERROR:")${ALL_OFF} ${DC}${msg}" "$@"
}

warning() {
    msg="$(gettext "$1")"; shift
    _msg "${BOLD}${YELLOW}$(gettext "ALERTA:")${ALL_OFF} ${DC}${msg}" "$@"
}

# Mensaje de error y salir
fatal_error() {
    error "$@"
    exit 1
}

# Sugerir algo
tip() {
  msg="$(gettext "$1")"; shift
  _msg "${BOLD}${BLUE}$(gettext "Tip:")${ALL_OFF} ${DEF_COLOR}${msg}" "$@"
}

# Muestra el manual
# $1 el comando para obtener la ayuda
help() {
  man $1 || man ${LVPN_DIR}/doc/es/$1.1
}
