#!/bin/bash

# Overwrite the previous backup with the current version

./qfixpt -v > info

gzip -k qfixpt

cp qfixpt.gz "backups"
cp conf.toml "backups"
cp info "backups"

# Keep a copy with version info

ver_all=$(./qfixpt -v | grep Build)
arr=("$ver_all")
ver_aux=${arr[1]}
ver=${ver_aux::-1}
echo "$ver"

mkdir -p "backups/$ver"

mv qfixpt.gz "backups/$ver"
mv info "backups/$ver"
cp conf.toml "backups/$ver"

# Only keep the indicated number of backups. Delete the oldests ones.

i=0
keep=10

for d in $(ls -dt backups/*/); do
  if [[ "$i" -gt "$keep" ]]; then
    echo "Deleting: ${d}"
  else
    echo "Keep: ${d}"
  fi
  ((i++))
done

