#!/bin/bash
#
# Process all directories in the same way
function processDirectory {
    local DIR="$1"
    local EXT="$2"
    mv "$DIR" "$DIR-$EXT"
    mkdir "$DIR"
    mv "$DIR-$EXT"/* "$DIR"
    rm -r "$DIR-$EXT"
    local NEWEST=$(find "$DIR" ! -type d -printf "%T@ %p\n" | sort -n | tail -n1)
    local FDATE=${NEWEST:0:10}
    local FNAME=${NEWEST:22}
    echo "$DIR - $EXT: $(date -d @$FDATE +%F) of $FNAME"
    touch -r "$FNAME" "$DIR"
    return 0
}
#
BASEDIR="/home/frs/project/coolprop/CoolProp"
# Start with the nightly builds, they are to remain at the bottom
DIRLIST=( "$BASEDIR/nightly/" )
for DIR in $BASEDIR/[0-9]*/; do 
    DIRLIST+=( "$DIR" )
done
#
EXT=$(date +%F)
#
for DIR in "${DIRLIST[@]}"; do
    sleep 1
    DIR=${DIR:0:${#DIR}-1}
    processDirectory "$DIR" "$EXT"
done
#
exit 0
