#! /bin/bash
# Zachary Lund
# $Id$

if [ $# -lt 1 -o $# -gt 2 ]; then
    echo "Usage: $( basename $0 ) <used minutes> [rollover minutes]"
    exit
fi

MIN_TOTAL=450
MIN_USED=$1
[ -n "$2" ] && let "MIN_TOTAL=$MIN_TOTAL+$2"
let "MIN_LEFT=$MIN_TOTAL-$MIN_USED"
END_DAY=17
let "START_DAY=$END_DAY+1"

WD_USED=0
WD_LEFT=0

DAYS=0
while [ 1 ]; do

    WEEK_DAY=$( date +%u -d "$DAYS day ago" ) # 1 = Monday

    if [ $WEEK_DAY -le 5 ]; then
        let "WD_USED=$WD_USED+1"
    fi

    MONTH_DAY=$( date +%d -d "$DAYS day ago" )

    if [ $MONTH_DAY -eq $START_DAY ]; then
        break
    fi

    let "DAYS=$DAYS+1"

done

DAYS=0
while [ 1 ]; do

    WEEK_DAY=$( date +%u -d "$DAYS day" ) # 1 = Monday

    if [ $WEEK_DAY -le 5 ]; then
        let "WD_LEFT=$WD_LEFT+1"
    fi

    MONTH_DAY=$( date +%d -d "$DAYS days" )

    if [ $MONTH_DAY -eq $END_DAY ]; then
        break
    fi

    let "DAYS=$DAYS+1"

done

echo "There are $WD_LEFT weekdays left in this billing cycle."
echo "You have $MIN_LEFT minutes left in this billing cycle."
QUOTA=$( echo "scale=2; $MIN_LEFT / $WD_LEFT" | bc )
echo "Your quota is $QUOTA minutes per day."

QUOTA=$( echo "scale=2; $MIN_USED / $WD_USED" | bc )
echo "(You are averaging $QUOTA minutes per day.)"
