#!/usr/bin/env bash
# GridAdmin Tools logger

set -e

. oio-ga-tools-utils

logfile=${OIO_GA_LOGS:="/tmp/oio-gridadmin-tools.log"}

format_to_file() {
  l=$1
  year=`date +"%Y"`
  if [ "${l:0:4}" = "$year" ]; then
    echo ${l/$'\n'/} >> $logfile
  else
    log "WARN" ${l/$'\n'/} >> $logfile
  fi
}

exec 8> >(
    IFS=''
    while read -r line; do
       year=`date +"%Y"`
       if [ "${line:0:4}" = "$year" ]; then
         echo ${line/$'\n'/} >> $logfile
       else
         log "WARN" ${line/$'\n'/} >> $logfile
       fi
    done
    unset IFS
)

eval "$@" 2>&1 | tee >(cat - >&8)
