#!/bin/bash

# Check if the required arguments are provided
if [ "$#" -lt 4 ] || [ "$#" -gt 5 ]; then
    echo "Usage: $0 {Folder} name1 {number1-number2 | letter1-letter2} name2 {symbol_between (optional)}"
    exit 1
fi

# Set the target directory
if [ "$#" -eq 5 ]; then
    TARGET_DIR="$1"
    NAME1="$2"
    RANGE="$3"
    NAME2="$4"
    SYMBOL="$5"
elif [ "$#" -eq 4 ]; then
    # If 4 arguments are provided, the folder is assumed to be the current directory
    TARGET_DIR="."
    NAME1="$1"
    RANGE="$2"
    NAME2="$3"
    SYMBOL="$4"
else
    # If 3 arguments are provided, the folder is assumed to be the current directory
    TARGET_DIR="."
    NAME1="$1"
    RANGE="$2"
    NAME2="$3"
    SYMBOL="_"  # Default separator is "_"
fi

/home/honney/.bin/tracker.py add create_folder_counting

# Ensure the target directory exists
mkdir -p "$TARGET_DIR"

# Check if RANGE is numeric or alphabetic
if [[ "$RANGE" =~ ^[0-9]+-[0-9]+$ ]]; then
    # Numeric range
    START_NUM=$(echo "$RANGE" | cut -d'-' -f1)
    END_NUM=$(echo "$RANGE" | cut -d'-' -f2)

    # Create folders for each number in the range
    for ((i=START_NUM; i<=END_NUM; i++)); do
        FOLDER_NAME="${NAME1}${SYMBOL}${i}${SYMBOL}${NAME2}"
        mkdir -p "$TARGET_DIR/$FOLDER_NAME"
    done

elif [[ "$RANGE" =~ ^[a-zA-Z]-[a-zA-Z]$ ]]; then
    # Alphabetic range
    START_LETTER=$(echo "$RANGE" | cut -d'-' -f1)
    END_LETTER=$(echo "$RANGE" | cut -d'-' -f2)

    # Convert letters to ASCII values for iteration
    START_ASCII=$(printf "%d" "'$START_LETTER")
    END_ASCII=$(printf "%d" "'$END_LETTER")

    # Ensure alphabetical order
    if [ "$START_ASCII" -gt "$END_ASCII" ]; then
        echo "Error: The alphabetical range should start with a letter that precedes the end letter."
        exit 1
    fi

    # Create folders for each letter in the range
    for ((i=START_ASCII; i<=END_ASCII; i++)); do
        LETTER=$(printf "\x$(printf %x $i)")
        FOLDER_NAME="${NAME1}${SYMBOL}${LETTER}${SYMBOL}${NAME2}"
        mkdir -p "$TARGET_DIR/$FOLDER_NAME"
    done

else
    echo "Error: RANGE should be in the format number1-number2 or letter1-letter2."
    exit 1
fi

echo "Folders created in $TARGET_DIR."
