#!/usr/bin/env bash

INPUT_DATA="user name 123 and password is 321"
PASSWORD="SecretPassword123"
FILE_PATH="/home/kuku/lab/data.backup.tar.gz"

# --- 1. STRING MANIPULATION ---

# Replace the first space with an underscore
FIRST_UNDERSCORE="${INPUT_DATA/ /_}"

# Replace ALL spaces with underscores
ALL_UNDERSCORES="${INPUT_DATA// /_}"

# Get string length for masking
PASS_LEN="${#PASSWORD}"


# --- 2. CASE CONVERSION ---

# Capitalize only the first letter: user -> User
CAPITALIZED="${INPUT_DATA^}"

# Convert entire string to uppercase: user name 123 -> USER NAME 123
SHOUTING="${INPUT_DATA^^}"

# Convert password to lowercase
LOWER_PASS="${PASSWORD,,}"


# --- 3. PATH MANIPULATION ---

# Extract ONLY the filename
FILENAME="${FILE_PATH##*/}"

# Extract the directory (/home/kuku/lab)
DIR_ONLY="${FILE_PATH%/*}"

# Extracting "backup" 
# Step 1: Remove the first part (data.) -> backup.tar.gz
TEMP_BACKUP="${FILENAME#*.}"
# Step 2: Remove everything after the next dot -> backup
BACKUP_PART="${TEMP_BACKUP%%.*}"

# Extracting "tar"
# Step 1: Remove the last extensions (.gz) -> data.backup.tar
TEMP_TAR="${FILENAME%.*}"
# Step 2: Remove everything before the last dot of that result -> tar
TAR_PART="${TEMP_TAR##*.}"

# Extract ONLY the final extension (gz)
EXT_ONLY="${FILE_PATH##*.}"

# Remove ALL extensions
PURE_NAME="${FILENAME%%.*}"

# --- 4. PATTERN SUBSTITUTION ---

# Replace only the first occurrence of the letter "a" with "@"
FIRST_REPLACE="${INPUT_DATA/a/@}"

# Replace ALL occurrences of "a" with "@"
ALL_REPLACE="${INPUT_DATA//a/@}"

# Replace only if the START of the string matches
# If INPUT_DATA starts with "user", replace it with "ADMIN"
START_REPLACE="${INPUT_DATA/#user/ADMIN}"

# Replace only if the END of the string matches
# If INPUT_DATA ends with "123", replace it with "XYZ"
END_REPLACE="${INPUT_DATA/%123/XYZ}"


# --- 4. OUTPUT  ---

echo -e "-----------------------------------"
echo "Original:          $INPUT_DATA"
echo "First space to _:  $FIRST_UNDERSCORE"
echo "All spaces to _:   $ALL_UNDERSCORES"
echo "Capitalized:       $CAPITALIZED"
echo "Uppercase:         $SHOUTING"
echo "Password length:   $PASS_LEN"
echo "Lowered password:  $LOWER_PASS"

echo -e "-----------------------------------"
echo "Full Path:         $FILE_PATH"
echo "Directory:         $DIR_ONLY"
echo "Filename:          $FILENAME"
echo "Base Name:         $PURE_NAME"
echo "BACKUP:            $BACKUP_PART"
echo "TAR:               $TAR_PART"
echo "Extension:         $EXT_ONLY"

echo -e "-----------------------------------"
echo "First replace:     $FIRST_REPLACE"
echo "All replace:       $ALL_REPLACE"
echo "Start replace:     $START_REPLACE"
echo "End replace:       $END_REPLACE"