#!/bin/bash

shopt -s dotglob
shopt -s nullglob

## try to find the repcode from current directory
IFS='/' read -r -a array <<< `pwd`
repcode=''
for dir in "${array[@]}";
do
    if [[ $dir == ${IMPNA}-* ]]; then
        if [ ! $dir == $IMPNA ]; then
            repcode=$dir
        fi
    fi
done

## if this fails, require the repcode as an argument
if [ -z $repcode ]; then
    if [ $# -eq 0 ]; then
        echo "I need a repcode."
        exit
    else
        repcode=$1
    fi
fi

report_fudi=$HOME/reports/$repcode

if [ ! -d $report_fudi ]
then
    echo "I don't see $report_fudi."
    exit
fi

sent_fudi=$report_fudi/sent
if [ ! -d $sent_fudi ]
then
    echo "I don't see $sent_fudi."
    exit
fi

## exit if no issues
#if [ -n "$(ls -A $sent_fudi)" ]; then
#    echo "$1 has no issues."
#    exit
#fi

fufis=( $sent_fudi/* )

from_fufi=${fufis[0]}
from_bana="${fufis[0]##*/}"
from_date="${from_bana:0:10}"

until_fufi=${fufis[-1]}
until_bana="${fufis[-1]##*/}"
until_date="${until_bana:0:10}"

echo "from=\"${from_date}\" until=\"${until_date}\""
