#!/usr/bin/bash

# Configuration - Edit these variables as needed
FILES_FOR_FOLDER1=("./LICENSE" "./README.md" "./starter/Setup-Tool.desktop" "./starter/setup-tool-128.png" "./starter/setup-tool-512.png" "./starter/setup-tool.1" "./starter/setup-tool.c" "./starter/setup-tool.conf" "./starter/special_dates.yaml")
FILES_FOR_FOLDER2=("./LICENSE" "./cli/setup-tool-cli.c" "./cli/pmsn")
FILES_FOR_FOLDER3=("./LICENSE" "./tui/setup-tool-tui.c")
DIRS_FOR_FOLDER3=("./tui/themes")
FILES_FOR_FOLDER4=("./LICENSE" "./ansible/ansible.cfg" "./ansible/Makefile" "./ansible/README.md" "./ansible/cli/setup-tool-ansible.c")
DIRS_FOR_FOLDER4=("./ansible/playbooks" "./ansible/roles" "./ansible/group_vars" "./ansible/inventory")
FILES_FOR_FOLDER5=("./LICENSE" "./ansible/cli/st-node.c")
FILES_FOR_FOLDER6=("./LICENSE" "./gui/CMakeLists.txt" "./gui/resources/setup-tool-gui-beta.desktop")
DIRS_FOR_FOLDER6=("./gui/src" "./gui/resources")
FOLDER1="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/setup-tool"
FOLDER2="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/setup-tool-cli"
FOLDER3="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/setup-tool-tui"
FOLDER4="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/setup-tool-ansible"
FOLDER5="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/st-node"
FOLDER6="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/setup-tool-gui"

# Default values
VERSION=""

# Function to display usage
usage() {
    echo "Usage: $0 -v VERSION"
    echo "Options:"
    echo "  -v VERSION    Version number to append to folders (required)"
    echo "  -h            Show this help message"
    echo ""
#    echo "Files for ${FOLDER1}: ${FILES_FOR_FOLDER1[*]}"
#    echo "Files for ${FOLDER2}: ${FILES_FOR_FOLDER2[*]}"
#    echo "Files for ${FOLDER3}: ${FILES_FOR_FOLDER3[*]}"
#    echo ""
    echo "Example: $0 -v 1.2.3"
    exit 1
}

# Parse command line arguments
while getopts "v:h" opt; do
    case $opt in
        v)
            VERSION="$OPTARG"
            ;;
        h)
            usage
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            usage
            ;;
    esac
done

# Check if version is provided
if [ -z "$VERSION" ]; then
    echo "Error: Version number is required"
    usage
fi

# Create versioned folder names
VERSIONED_FOLDER1="${FOLDER1}-${VERSION}"
VERSIONED_FOLDER2="${FOLDER2}-${VERSION}"
VERSIONED_FOLDER3="${FOLDER3}-${VERSION}"
VERSIONED_FOLDER4="${FOLDER4}-${VERSION}"
VERSIONED_FOLDER5="${FOLDER5}-${VERSION}"
VERSIONED_FOLDER6="${FOLDER6}-${VERSION}"

# Create directories if they don't exist
mkdir -p "$VERSIONED_FOLDER1"
mkdir -p "$VERSIONED_FOLDER2"
mkdir -p "$VERSIONED_FOLDER3"
mkdir -p "$VERSIONED_FOLDER4"
mkdir -p "$VERSIONED_FOLDER5"
mkdir -p "$VERSIONED_FOLDER6"

echo "Copying files..."

# Copy files to first folder
echo "Copying to $VERSIONED_FOLDER1:"
for file in "${FILES_FOR_FOLDER1[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$VERSIONED_FOLDER1/"
        echo "  Copied: $file"
    else
        echo "  Warning: $file not found or is not a regular file"
    fi
done

# Copy files to second folder
echo "Copying to $VERSIONED_FOLDER2:"
for file in "${FILES_FOR_FOLDER2[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$VERSIONED_FOLDER2/"
        echo "  Copied: $file"
    else
        echo "  Warning: $file not found or is not a regular file"
    fi
done

# Copy files to third folder
echo "Copying to $VERSIONED_FOLDER3:"
for file in "${FILES_FOR_FOLDER3[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$VERSIONED_FOLDER3/"
        echo "  Copied: $file"
    else
        echo "  Warning: $file not found or is not a regular file"
    fi
done

# Copy directories to third folder (TUI themes)
for dir in "${DIRS_FOR_FOLDER3[@]}"; do
    if [ -d "$dir" ]; then
        cp -r "$dir" "$VERSIONED_FOLDER3/"
        echo "  Copied directory: $dir"
    else
        echo "  Warning: $dir not found or is not a directory"
    fi
done

# Copy files to fourth folder (ansible)
echo "Copying to $VERSIONED_FOLDER4:"
for file in "${FILES_FOR_FOLDER4[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$VERSIONED_FOLDER4/"
        echo "  Copied: $file"
    else
        echo "  Warning: $file not found or is not a regular file"
    fi
done

# Copy directories to fourth folder (ansible)
for dir in "${DIRS_FOR_FOLDER4[@]}"; do
    if [ -d "$dir" ]; then
        cp -r "$dir" "$VERSIONED_FOLDER4/"
        echo "  Copied directory: $dir"
    else
        echo "  Warning: $dir not found or is not a directory"
    fi
done

# Copy files to fifth folder (st-node)
echo "Copying to $VERSIONED_FOLDER5:"
for file in "${FILES_FOR_FOLDER5[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$VERSIONED_FOLDER5/"
        echo "  Copied: $file"
    else
        echo "  Warning: $file not found or is not a regular file"
    fi
done

# Copy files to sixth folder (gui)
echo "Copying to $VERSIONED_FOLDER6:"
for file in "${FILES_FOR_FOLDER6[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$VERSIONED_FOLDER6/"
        echo "  Copied: $file"
    else
        echo "  Warning: $file not found or is not a regular file"
    fi
done

# Copy directories to sixth folder (gui)
for dir in "${DIRS_FOR_FOLDER6[@]}"; do
    if [ -d "$dir" ]; then
        cp -r "$dir" "$VERSIONED_FOLDER6/"
        echo "  Copied directory: $dir"
    else
        echo "  Warning: $dir not found or is not a directory"
    fi
done

echo "Copy operation completed!"
echo "Files copied to:"
echo "  - $VERSIONED_FOLDER1 (${#FILES_FOR_FOLDER1[@]} files)"
echo "  - $VERSIONED_FOLDER2 (${#FILES_FOR_FOLDER2[@]} files)"
echo "  - $VERSIONED_FOLDER3 (${#FILES_FOR_FOLDER3[@]} files + ${#DIRS_FOR_FOLDER3[@]} directories)"
echo "  - $VERSIONED_FOLDER4 (${#FILES_FOR_FOLDER4[@]} files + ${#DIRS_FOR_FOLDER4[@]} directories)"
echo "  - $VERSIONED_FOLDER5 (${#FILES_FOR_FOLDER5[@]} files)"
echo "  - $VERSIONED_FOLDER6 (${#FILES_FOR_FOLDER6[@]} files + ${#DIRS_FOR_FOLDER6[@]} directories)"
