#!/bin/bash
# SPDX-FileCopyrightText: 2025 artlog@l0g.eu
# SPDX-License-Identifier: AGPL-3.0-or-later
# header generated by lib/metascript.sh header
# metascript_version=v1.0.0

toolsdir=lib
pushd $toolsdir >/dev/null
toolsdir=$(pwd)
source metascript.sh
popd >/dev/null

usage () {
    cat <<EOF
output list of clones and current git commit references
for this project and projects dependencies ( clones.dep )
EOF
}

metarun=metarun
# change default to sudo if needed
# metasudo=sudo

while [[ $# > 0 ]]
do
    case "$1" in
        *)
           parsemetaarg "$1"
        ;;
    esac
    shift
done

get_git_info()
{
    local clone=$1
    echo "[$clone]"
    echo "path=$(realpath --relative-to=$absolute $(pwd))"
    echo "remote.origin.url=$(git config --get remote.origin.url)"
    echo "head=$(< .git/$(git symbolic-ref HEAD))"
    # echo "branch=$(git branch)"
}

absolute=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )

pushd $absolute >/dev/null

echo "#"
echo "# root.absolute=$absolute"
echo "# date=$(date)"

project_name=.

if [[ -f project_params ]]
then
    project_name=$(grep "project_name" project_params)
    project_name=${project_name/project_name=/}
fi

# expect . to be in cube
get_git_info $project_name

if [[ -f  clones.dep ]]
then
    clones=$(< clones.dep)
    for clone in $clones
    do
	directory=$clone
	if [[ -d $directory ]]
	then
	    pushd $directory >/dev/null
	    get_git_info $clone
	    popd >/dev/null
	else
	    log_error "clone $clone not found in $directory"
	fi
    done
else
    log_info "no clone.deps file"
fi

popd >/dev/null
