#!/bin/bash

# Get the path of the script
script_name="cp-tool.py"
script_path=$(command -v "$script_name")

# Check if the script file exists
if [ ! -f "$script_path" ]; then
    echo "Error: $script_path does not exist."
    exit 1
fi

# Run the python script
output=$(python "$script_path" "$@")

# Read lines from output string
parsed_dir=""
while IFS= read -r line; do
    if [[ $line == dir=* ]]; then
        parsed_dir=${line#dir=}
        break;
    else
        echo "$line"
    fi
done <<< "$output"

# Change to the parsed directory if it was found
if [ -n "$parsed_dir" ]; then
    cd "$parsed_dir" || exit 1
fi
