#!/bin/bash

help="
usage: reportrun [-h] -s SUBJECT -t RECIPIENT [-f SENDER] [-C CONFIG.PY]
                [FILE [FILE ...]] -- COMMAND [COMMAND_ARG ...]

reportrun wraps the execution of a command and sends an email
whenever the command fails. The mail sending is processed by
emili so that ansi codes and spacing are shown properly.

positional arguments:
  FILE                  File to attach
  COMMAND               Command to run
  COMMAND_ARG           Argument for COMMAND

optional arguments:
  -f SENDER, --from SENDER
                        Message sender ('From:' header)
  -s SUBJECT, --subject SUBJECT
                        Message subject ('Subject:' header)
  -t recipient, --to recipient
                        Message recipient ('To:' header) (multiple)
  -C CONFIG.PY, --config CONFIG.PY
                        Python Module with smtp configuration defined.
  -a, --always
                        Sends even if the command does not fail.
  --
                        Marks the start of the command to execute.
"

color() { echo -e '\033['$1'm'"${@:2}"'\033[0m' >&2; }
error() { color "31" "ERROR:" "$@"; }
step() { color "34;1" "::" "$@"; }
success() { color "32;1" ">>" "$@"; }
fail() { error "$@"; exit -1; }
function quote() { for i; do printf "%q " "$i"; done }

# Take from environment if they exist
EMILI_CONFIG=${EMILI_CONFIG:-~/emili.cfg}
EMILI_FROM=${EMILI_FROM:-sistemas@somenergia.coop}

configopt="-C $EMILI_CONFIG"
fromopt="-f $EMILI_FROM"

while [ $# -gt 0 ]
do
	arg="$1"
	case $arg in
		[-][-])
			# Separates the actual command line
			shift
			break
			;;
		-h|--help)
			echo "$help"
			exit 0
			;;
		-C|--config)
			configopt="-C $2"
			shift
			;;
		-f|--from)
			fromopt="-f $2" 
			shift
			;;
		-t|--to)
			recipients="$recipients -t $2" 
			shift
			;;
		-s|--subject)
			subject="$2"
			shift
			;;
		-a|--always)
			always='y'
			;;
		*)
			attachments="$attachments $1"
			;;
	esac
	shift
done
# Set the default values

[ $# -gt 0 ] || fail "No command provided"

subject=${subject:-"Tarea:" $*}
output=$(step "Running:" $(quote "$@") 2>&1)
output+=$(echo; "$@" 2>&1)
result=$?
output+=$(
	echo;
	[ $result == 0 ] &&
		success "Command successful" 2>&1 ||
		error "Command failed with code $result" 2>&1
	)

cat <<<"$output"

[ "$result" != '0' ] || [ "$always" == 'y' ] && (
	[ $result == 0 ] || subject="FAILED: $subject"
	step "Sending email..."
	emili.py $configopt --format ansi $fromopt $recipients -s "${subject}" $attachments <<<"$output"
)

