#!/usr/bin/env bash

N_ROTATE_LOGFILE=5
rotate_log_file() {
    local logfile="$1"
    local err=0
    # Rotate old logs only if current is non-empty
    if [[ -s $logfile ]]; then
        for ((i=N_ROTATE_LOGFILE;i>0;i--)); do
            local rfile="$logfile"
            (( i == 1 )) || rfile="${rfile}.$((i-1))"
            if [[ -e "$rfile" ]]; then
                if ! mv -f "$rfile" "$logfile.$i"; then
                    err=1
                    break
                fi
            fi
        done
    fi
    return $err
}
