#!/bin/bash

PATH=/Users/tblancher/homebrew/opt/coreutils/libexec/gnubin:/Users/tblancher/homebrew/opt/gnu-tar/libexec/gnubin:/Users/tblancher/bin:/Users/tblancher/homebrew/bin:/Users/tblancher/homebrew/sbin:/Users/tblancher/gem/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/sbin:/usr/sbin:/Users/tblancher/bin

if [[ -z "$1" ]]; then
  MONTH=$(( $(date +%-m) - 1 ))
else
	MONTH=$1
    shift
fi

if [[ -z "$1" ]]; then
    YEAR=$(date +%Y)
	CURR=1
else
    YEAR=$1
    shift
	CURR=0
fi


if [[ "$MONTH" -eq 0 ]];
then
	MONTH=12
fi

if [[ "${CURR}" -eq 1 ]] && [[ $(( $(date +%-m) - MONTH )) -le 0 ]]; then
  YEAR=$(( YEAR - 1))
fi

if [ "$MONTH" -lt 10 ];
then
	MONTH=0$MONTH
fi
tar -cvJf "$YEAR-$MONTH.tar.xz"  $YEAR-$MONTH-*.log --remove-files

