automate/utils/generate-conf_print.sh

71 lines
1.8 KiB
Bash

#!/bin/bash
set -x
# Function to check if a directory exists and create it if not
ensure_directory_exists() {
local dir_path="$1"
local output_file="$2"
echo "*** adding mkdir -p $dir_path to the conf_print file"
echo -e "\nmkdir -p debian/$(basename $dir_path)\n" >>"$output_file"
echo "calling generate_functions() on $dir_path"
generate_functions "$dir_path" "$output_file"
}
# Function to generate functions for text files in a directory
generate_functions() {
local directory=$1
local output_file=$2
for item in "$directory"/*; do
# Use the file command to determine the type of the item
item_type=$(file --mime-type -b "$item")
if [[ "$item_type" =~ directory ]]; then
# If the item is a directory, recursively call generate_functions on it
ensure_directory_exists "$item" "$output_file"
elif [[ "$item_type" =~ ^text/.* ]]; then
# If the item is a text file, proceed with generating functions
local filename=$(basename "$item")
local function_name="conf_print_$(echo "$filename" | tr -c '[:alnum:]' '_')"
function_name=${function_name%_}
local heredoc=$(
cat <<-EOF
$function_name() {
cat <<-'FOE'
$(cat "$item")
FOE
}
EOF
)
echo "$heredoc" >>"$output_file"
echo "# $function_name | tee $directory/$filename >/dev/null" >>"$output_file"
fi
done
}
# Main script execution
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <directory> <output_file>"
exit 1
fi
directory=$1
output_file=$2
if [[ -f ${output_file} ]]; then
echo "Appending to ${output_file}"
else
echo "Creating ${output_file}"
echo -e "#!/usr/bin/env bash\n" >${output_file}
tree -L 6 ${directory} | while read -r line; do echo "# $line"; done >>${output_file}
fi
# Generate functions for the directory
generate_functions "$directory" "$output_file"
echo "Functions generated and appended to $output_file"