run.sh

1#!/bin/bash
2
3# Function to normalize a word (converts hyphens to spaces and capitalizes each word)
4normalize_word() {
5    echo "$1" | sed -e 's/-/ /g' -e 's/\b\(.\)/\u\1/g'
6}
7
8# Function to denormalize a word (converts spaces to hyphens and lowercases each word)
9denormalize_word() {
10    echo "$1" | tr '[:upper:]' '[:lower:]' | awk '{print tolower($0)}' | sed -e 's/ /-/g'
11}
12
13# Function to get normalized directory names under ./src/
14get_available_simulations() {
15    for dir in ./src/*; do
16        if [ -d "$dir" ]; then
17            echo "$(normalize_word "$(basename "$dir")")"
18        fi
19    done
20}
21
22# Function to parse a Makefile and extract the TARGET
23get_target_file() {
24    make_file="$1"
25    target=$(grep -E '^TARGET\s*=' "$make_file" | sed -e 's/TARGET\s*=\s*//g')
26    echo "$target"
27}
28
29echo "Available simulations:"
30echo "$(get_available_simulations)"
31
32read -p "Enter simulation name: " sim_name
33
34denormalize_name=$(denormalize_word "$sim_name")
35sim_dir="./src/$denormalize_name"
36
37if [ -d "$sim_dir" ]; then
38    # Compile the simulation
39    make_file="$sim_dir/Makefile"
40    echo "Compiling simulation $sim_name"
41    echo "=============================="
42    
43    if make -C "$sim_dir"; then
44        # Move ./src/$denormalize_name/$target to ./build/$target
45        target=$(get_target_file "$make_file")
46        
47        if [ -f "$sim_dir/$target" ]; then
48            mkdir -p ./build
49            mv "$sim_dir/$target" "./build/$target"
50        else
51            echo "Error: Target $target not found"
52        fi
53        
54        echo "=============================="
55        
56        # Run the simulation
57        read -p "Run simulation $sim_name? [y/n]: " run_sim
58        if [ "$run_sim" == "y" ]; then
59            echo "Running simulation $sim_name"
60            echo "=============================="
61            "./build/$target"
62            echo "Simulation $sim_name ran successfully"
63        else
64            echo "Simulation $sim_name not run"
65        fi
66    else
67        echo "Error: Compilation failed for simulation $sim_name"
68    fi
69else
70    echo "Error: Simulation $sim_name not found"
71fi
72