#!/bin/bash

BASE_PATH="/home/anaber/prj/trenton"
DB_NAME="compile_commands.json"

function do_update {
    DB_SRC="$1"
    ln -sf "${DB_SRC}" "${BASE_PATH}"/"${DB_NAME}"
}

# Get a list of valid compile command databases in the repo
mapfile -t DB_LIST < <( find "${BASE_PATH}" -mindepth 2 -type f -name "${DB_NAME}" )
idx=0
for DB_ELEM in ${DB_LIST[@]}; do
    echo "${idx}:  ${DB_ELEM}"
    idx=$((idx+1))
done

# Parse and validate the user selection
read -p "Select an option to update: " SELECTION
NUM_ELEM=${#DB_LIST[@]}
MAX_ELEM=$((NUM_ELEM-1))
case "${SELECTION}" in
    ''|*[!0-${MAX_ELEM}]*) echo "Operation cancelled" ;;
    *) do_update "${DB_LIST[${SELECTION}]}" ;;
esac
