#!/bin/bash
#
# lib/lvpn-send-email
#
# 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/>.
#
#
# Envia el archivo de host del nodo a los nodos en connectto

. "${LVPN_LIBDIR}"/common

# export SENDMAIL=msmtp
SENDMAIL=${SENDMAIL:-sendmail}

requires ${SENDMAIL}

while getopts "dhs:m:f:t:"; do
    case $arg in 
        h) help ${self} ; exit 0;;
        s) subject="${OPTARG}" ;;
        m) message="${OPTARG}" ;;
        f) from="${OPTARG}" ;;
        t) to="${OPTARG}" ;;
    esac
done
let OPTIND--; shift $OPTIND

node="$(get_node_name "$1")"
nodefile="$(get_node_file ${node})"

subject="${subject:-"[LibreVPN] Adjuntando nodo ${node}"}"
message="${message:-"Hola! Te adjunto mi nodo ${node}"}"
from="${from:-"$(get_id | tr -d "#")"}"
to="${to:-"vpn@hackcoop.com.ar"}"

# Crea el mail y lo envía a sendmail
cat <(echo "Subject: ${subject}") \
    <(echo "From: ${from}") \
    <(echo -e "To: ${to}\n") \
    <(echo -e "${message}\n\n") \
    ${nodefile} | \
    ${SENDMAIL} -f ${from} -t

exit $?
