#!/bin/bash

# Exit immediately if a command exits with a non-zero status
set -e

# Define base and target branches
BASE_BRANCH="origin/deprecated_android-3.18-2020-08-05_3.18"
TARGET_BRANCH="origin/lineage-18.1-ASB-2020-07-05_2020-08-05_3.18"

# Fetch the latest branches from the remote repository
git fetch origin

# Check if the branches exist
if ! git show-ref --verify --quiet "refs/remotes/$BASE_BRANCH"; then
    echo "Error: Base branch '$BASE_BRANCH' does not exist."
    exit 1
fi

if ! git show-ref --verify --quiet "refs/remotes/$TARGET_BRANCH"; then
    echo "Error: Target branch '$TARGET_BRANCH' does not exist."
    exit 1
fi

# Create patches directory if it doesn't exist
mkdir -p patches

# Get the list of modified files, excluding .git and patches directories
modified_files=$(git diff --name-only "$BASE_BRANCH" "$TARGET_BRANCH" | grep -v '^.git/' | grep -v '^patches/')

# Generate individual patches for each modified file
for file in $modified_files; do
    # Create the directory structure in the patches folder
    patch_dir="patches/$(dirname "$file")"
    
    # Create the directory if it doesn't exist
    mkdir -p "$patch_dir"
    
    # Create patch for the file, using the correct path for the patch file
    git diff "$BASE_BRANCH" "$TARGET_BRANCH" -- "$file" > "$patch_dir/$(basename "$file").patch"
done

echo "Patches generated in the 'patches' directory."

# Identify new directories

# Get list of directories in base and target branches
dirs_base=$(git ls-tree -r -d --name-only "$BASE_BRANCH" | sort)
dirs_target=$(git ls-tree -r -d --name-only "$TARGET_BRANCH" | sort)

# Find directories that are in target but not in base
new_directories=$(comm -13 <(echo "$dirs_base") <(echo "$dirs_target"))

# Save new directories to patches/new_folders.txt
echo "$new_directories" > patches/new_folders.txt

# Output the new directories
if [ -s patches/new_folders.txt ]; then
    echo "New directories introduced (saved in 'patches/new_folders.txt'):"
    cat patches/new_folders.txt
else
    echo "No new directories were introduced."
fi

#-------------------------------------------------------------------------------

# Input and output file paths
input_file="patches/new_folders.txt"
output_file="patches/new_folders_sorted.txt"

# Check if 'new_folders.txt' exists
if [ ! -f "$input_file" ]; then
    echo "Error: '$input_file' not found."
    exit 1
fi

# Read and sort the paths
sorted_paths=$(sort "$input_file")

# Initialize variables
prev_path=""
minimal_paths=()

# Process each path
while IFS= read -r path; do
    # Skip empty lines
    if [ -z "$path" ]; then
        continue
    fi

    # If prev_path is empty, this is the first path
    if [ -z "$prev_path" ]; then
        minimal_paths+=("$path")
        prev_path="$path"
        continue
    fi

    # Check if the current path is a subdirectory of prev_path
    case "$path" in
        "$prev_path"/*)
            # Current path is a subdirectory; skip it
            continue
            ;;
        "$prev_path")
            # Current path is the same as prev_path; skip it
            continue
            ;;
        *)
            # Current path is a new directory; add it to minimal_paths
            minimal_paths+=("$path")
            prev_path="$path"
            ;;
    esac
done <<< "$sorted_paths"

can we simplyfy generation of new_folders_sorted.txt ?