# LOGIN
az login --identity

BASEDIR=$(dirname "$0")
SUBS="62ebff8f-c40b-41be-9239-252d6c0c8ad9"
CONTAINER="exports"
STORAGEACCOUNT="qmiexports"
STORAGEACCOUNTKEY="6PI/qNoJ7UnX83lVcffN7p7X1TwYXv+jHKLIEcMKZWpWA01Vj75AAyjq4dXJdmUgCeSEIRefHnJzQeuPZRHInQ=="

# GET FILES
az storage blob download-batch --destination . --source $CONTAINER --account-name $STORAGEACCOUNT --account-key $STORAGEACCOUNTKEY --pattern *.csv --subscription $SUBS


# MOVE LATEST FILE PER MONTH TO FOLDER latest
echo "BASEDIR=$BASEDIR"

for d in "$BASEDIR/qmicloud/qmicloud/"*
do
    folderBase=$(basename "$d")
    ymn=$(echo $folderBase | cut -c1-6)
    lastFile=$(find $d -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
    echo $lastFile
    cp $lastFile $BASEDIR/$ymn.csv
done