#!/usr/bin/bash

# Configuration - Edit these variables as needed
FILES_FOR_C_DIR=(
    "./C/Makefile"
    "./C/libstp3.c"
    "./C/libstp3.h"
    "./C/stp_internal.h"
    "./C/stp_ansible.c"
    "./C/stp_config.c"
    "./C/stp_core.c"
    "./C/stp_detection.c"
    "./C/stp_log.c"
    "./C/stp_package.c"
    "./C/stp_repo.c"
    "./C/stp_script.c"
    "./C/stp_security.c"
    "./C/stp_special.c"
    "./C/stp_theme.c"
    "./C/stp_tui.c"
)
FILES_FOR_CPP_DIR=("./C++/libstp3.hpp" "./C++/libstp3_cpp.cpp")
FILES_FOR_ROOT=("./LICENSE" "./README.md" "./special_dates.yaml")
FILES_FOR_FOLDER2=("./LICENSE" "./extra-package/libstp3.1" "./extra-package/libstp3.c" "./extra-package/.libstp3-sh")
FOLDER1="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/libstp3"
FOLDER2="/home/burningpho3nix/Documents/Development/Setup-Tool/normal/libstp3-extras"

# Default values
VERSION=""

# Function to display usage
usage() {
    echo "Usage: $0 -v VERSION"
    echo "Options:"
    echo "  -v VERSION    Version number for libstp3 and libstp3-extras (required)"
    echo "  -h            Show this help message"
    echo ""
    echo "Example: $0 -v 1.0.0"
    exit 1
}

# Parse command line arguments manually
while [[ $# -gt 0 ]]; do
    case $1 in
        -v)
            VERSION="$2"
            shift 2
            ;;
        -h)
            usage
            ;;
        *)
            echo "Invalid option: $1" >&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}"

# Create directories if they don't exist
mkdir -p "$VERSIONED_FOLDER1/C"
mkdir -p "$VERSIONED_FOLDER1/C++"
mkdir -p "$VERSIONED_FOLDER2"

echo "Copying files..."

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

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

# Copy files to root
echo "Copying to $VERSIONED_FOLDER1 (root):"
for file in "${FILES_FOR_ROOT[@]}"; 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

echo "Copy operation completed!"
echo "Version: $VERSION"
echo "Files copied to:"
echo "  - $VERSIONED_FOLDER1/"
echo "    - C/ (${#FILES_FOR_C_DIR[@]} files)"
echo "    - C++/ (${#FILES_FOR_CPP_DIR[@]} files)"
echo "    - root (${#FILES_FOR_ROOT[@]} files)"
echo "  - $VERSIONED_FOLDER2 (${#FILES_FOR_FOLDER2[@]} files)"
