#!/opt/bin/bash

[ "$#" -lt 3 ] && exit

if [[ "$3" =~ ^[[:digit:]]+$ ]] ; then
  away="$3"
else
  away=0
fi

days=$(($away / (60*24)))
hours=$((($away % (24*60)) / 60))
mins=$(($away % 60))

declare -a texttime=()
if [ $days -gt 0 ] ; then
  texttime[1]="$days days"
fi
if [ $hours -gt 0 ] ; then
  texttime[2]="$hours hours"
fi
if [ $mins -gt 0 -a $days -eq 0 ] ; then
  texttime[3]="$mins minutes"
fi
if [ -z "${texttime[*]}" ] ; then
  texttime[1]="unknown time"
fi

echo "Welcome $1! You've been away ${texttime[*]}." | obexnote $2 2> /dev/null
