#!/bin/bash
# Copyright 2015 Lucid Networks sloeckle

source /etc/default/nexp-tools

export DISPLAY=:0
export EMAIL=$HOSTNAME'@lucidnetworks.net'
time=$(date +"%m-%d-%Y_%T")
rand=$(od -N 4 -t uL -An /dev/urandom | tr -d " ")
ipfulleth0=$(ip addr show eth0 | grep inet)
ipaddreth0=$(echo $ipfulleth0 | awk '{print $2}')
ipfulltun0=$(ip addr show tun0 | grep inet)
ipaddrtun0=$(echo $ipfulltun0 | awk '{print $2}')
networkinfo="N-EXP Network Information for $HOSTNAME:\n\teth0: $ipaddreth0\n\ttun0: $ipaddrtun0"
dir=/tmp/$rand
mkdir $dir &>/dev/null
scrot $dir/$time.png &>/dev/null
convert $dir/$time.png -colorspace Gray $dir/$time.tif
#convert $dir/$time.png -blur 1x65535 -level 30x100% -colorspace Gray -contrast-stretch 0 -lat 25x25+20 -sharpen 0x1 $dir/$time.tif
ocr=$(tesseract $dir/$time.tif stdout)
text="$ocr\n\n\n$networkinfo\n"
if test -z "$1"
then
	echo -e $text | mutt -s "$HOSTNAME Screenshot OCR @ $time" -a $dir/$time.png -- $MAILTO
else
	echo -e $text | mutt -s "$HOSTNAME Screenshot OCR @ $time" -a $dir/$time.png -- $MAILTO,$1
fi
rm -rf $dir &>/dev/null
