#!/usr/bin/env bash
#
# lvpn.in
#
# 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/>.
#
#
# Versión autogenerada
# Wrapper para correr los comandos al estilo `git comando parametros`
# Hay que crearlos en libdir/lvpn-comando

# ENV
# Tomar las variables de entorno (por ejemplo de un archivo de configuración
# o de la shell) o usar directorios locales
TINC="${TINC:-"/etc/tinc/lvpn"}"
NETWORK="${NETWORK:-"lvpn"}"
LVPN="/usr/bin/lvpn"
# Guardar todo en el ~/.config del usuario local
LVPN_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/${NETWORK}"
LVPN_LIBDIR="${LVPN_LIBDIR:-"/usr/lib/lvpn"}"
LVPN_HOSTS="${LVPN_HOSTS:-"/usr/share/lvpn/hosts"}"
LVPN_BEADLE="${LVPN_BEADLE:-"/usr/share/lvpn/beadle"}"
KEYSIZE=${KEYSIZE:-4096}
# Flags para 
TINCD_FLAGS="${TINCD_FLAGS:-"--logfile -U nobody"}"
PORT=${PORT:-655}

# Subnets
LVPN_SUBNET="${LVPN_SUBNET:-"192.168.9.0/24"}"
LVPN_SUBNET6="${LVPN_SUBNET6:-"2001:1291:200:83ab::/64"}"

# Para gettext
TEXTDOMAINDIR=${TEXTDOMAINDIR:-"/usr/share/locale"}
TEXTDOMAIN="lvpn"

export TINC NETWORK LVPN \
       LVPN_DIR LVPN_LIBDIR LVPN_HOSTS \
       KEYSIZE TEXTDOMAINDIR TEXTDOMAIN \
       LVPN_SUBNET LVPN_SUBNET6 TINCD_FLAGS PORT

. "${LVPN_LIBDIR}/common"

list_commands() {
  pushd "${LVPN_LIBDIR}" &>/dev/null

  printf '%s\n' lvpn-* | sed "s/^lvpn-//"

  popd &>/dev/null
}

while getopts "hc" arg; do
    case $arg in
        h) help lvpn ; exit 0;;
        c) list_commands ; exit 0;;
    esac
done
let OPTIND--; shift ${OPTIND}

test -z "$1" && help lvpn && exit 0

# El comando
command=$1; shift

# Chequear si el comando existe
if [ ! -x "${LVPN_LIBDIR}/lvpn-${command}" ]; then
    fatal_error "%s no existe, tal vez te gustaría implementarlo :D" ${command}
fi

# Correr el comando
exec ${LVPN_LIBDIR}/lvpn-${command} "$@"

exit $?
