#!/usr/bin/env bash

# Directory containing files
DIR="../target/svg_sprites"
# Output file
OUTPUT="../dist/plantuml-stdlib/stdlib/archimate/ArchimateSprites.puml"

## Duplicate identical SVGs for several types
duplicate_svg() {
  local source="$1"
  shift                 # Shift arguments so that $@ contains the array elements
  local targets=("$@")  # Capture remaining arguments as an array

  for element in "${targets[@]}"; do
    cp $DIR/"$source".svg $DIR/"$element".svg
  done
}

to_kebab_case() {
    local input="$1"
    # Convert to lowercase and replace underscores with dashes
    local output="${input,,}"          # lowercase
    output="${output//_/-}"            # replace underscores with dashes
    echo "$output"
}

# Motivation_Stakeholder serves as template for Business_Role
targets=("Business_Role")
duplicate_svg "Motivation_Stakeholder" "${targets[@]}"

# Other_Location serves as template for Business_Location
targets=("Business_Location")
duplicate_svg "Other_Location" "${targets[@]}"

# Business_Collaboration serves as template for Application_Collaboration and Technology_Collaboration
targets=("Application_Collaboration" "Technology_Collaboration")
duplicate_svg "Business_Collaboration" "${targets[@]}"

# Business_Interface serves as template for Application_Interface and Technology_Interface
targets=("Application_Interface" "Technology_Interface")
duplicate_svg "Business_Interface" "${targets[@]}"

# Business_Process serves as template for Application_Process and Technology_Process
targets=("Application_Process" "Technology_Process")
duplicate_svg "Business_Process" "${targets[@]}"

# Business_Function serves as template for Application_Function and Technology_Function
targets=("Application_Function" "Technology_Function")
duplicate_svg "Business_Function" "${targets[@]}"

# Business_Interaction serves as template for Application_Interaction and Technology_Interaction
targets=("Application_Interaction" "Technology_Interaction")
duplicate_svg "Business_Interaction" "${targets[@]}"

# Business_Event serves as template for Application_Event, Technology_Event, and Implementation_Event
targets=("Application_Event" "Technology_Event" "Implementation_Event")
duplicate_svg "Business_Event" "${targets[@]}"

# Business_Service serves as template for Application_Service and Technology_Service
targets=("Application_Service" "Technology_Service")
duplicate_svg "Business_Service" "${targets[@]}"

# Business_Object serves as template for Application_DataObject
targets=("Application_DataObject")
duplicate_svg "Business_Object" "${targets[@]}"

## Generate sprites for all Archimate types

echo "' Do not edit this file. It is generated by running ./generate_spriteinclude.sh" > "$OUTPUT"
echo "" >> "$OUTPUT"

# Loop through all files in the folder
for file in "$DIR"/*; do
    # Only process regular files
    if [ -f "$file" ]; then
        # Get filename without extension
        filename_without_ext="$(basename "$file" | sed 's/\.[^.]*$//')"
        sprite_name=$(to_kebab_case "$filename_without_ext")

        # Define the sprite based on the filename
        echo -n "sprite \$${sprite_name}-svg " >> "$OUTPUT"

        # Append the file content skipping the first 2 lines
        tail -n +3 "$file" >> "$OUTPUT"
    fi
    echo "" >> "$OUTPUT"
done
