# KEEP_IN_ROOT Feature Implementation ## Overview Implemented flexible ROM organization with `KEEP_IN_ROOT_*` environment variables that allow selective organization of regions, languages, and kinds with recursive kind support. **Date**: October 8, 2025 **Feature**: Selective organization with recursive kinds ## ๐ŸŽฏ Key Features 1. **Environment Variable Control** - Organization behavior controlled by `KEEP_IN_ROOT_*` variables 2. **Three Behavior Modes** - Variable not set, empty, or with values 3. **Recursive Kind Organization** - Kinds organize within every folder (root, region, language) 4. **Validation** - Validates all values against defined arrays 5. **Clear Intent** - Variable names clearly communicate behavior ## ๐Ÿ“ Environment Variables ### KEEP_IN_ROOT_REGIONS Controls region organization: - **Not set**: No region organization performed - **Empty (`""`)**: Organize ALL regions into subfolders - **With values (`"USA,World"`)**: Keep listed regions in root, organize others ### KEEP_IN_ROOT_LANGUAGES Controls language organization: - **Not set**: No language organization performed - **Empty (`""`)**: Organize ALL languages into subfolders - **With values (`"En"`)**: Keep listed languages in root, organize others ### KEEP_IN_ROOT_KINDS Controls kind organization (recursive): - **Not set**: No kind organization performed - **Empty (`""`)**: Organize ALL kinds recursively into subfolders - **With values (`"Demo,Beta"`)**: Keep listed kinds in place, organize others recursively ## ๐Ÿ”„ Recursive Kinds Feature Kinds are organized **recursively** in every location: 1. **Root folder** - Organizes kinds from ROMs kept in root (excluded regions/languages) 2. **Region folders** - Organizes kinds within each `By Region//` folder 3. **Language folders** - Organizes kinds within each `By Language//` folder This means kinds create subfolders wherever ROMs are located after region/language organization. ## ๐Ÿ“Š Usage Examples ### Example 1: USA Build (Typical Use Case) ```bash #!/bin/bash # build-nes-usa-1g1r.sh # Keep USA and World ROMs in root, organize others by region export KEEP_IN_ROOT_REGIONS="USA,World" # Organize kinds recursively, but keep Demo in place export KEEP_IN_ROOT_KINDS="Demo" ./organizers/organize-roms.sh "/path/to/output" ``` **Input ROMs:** ``` Mario (USA).nes Zelda (USA) [Beta].nes Sonic (USA) [Demo].nes Final Fantasy (Japan).nes Metroid (Japan) [Proto].nes Castlevania (Europe).nes ``` **Output Structure:** ``` /output/ โ”œโ”€โ”€ Mario (USA).nes โ† USA, no kind = root โ”œโ”€โ”€ Sonic (USA) [Demo].nes โ† USA, Demo (excluded) = root โ”œโ”€โ”€ By Kind/ โ† From root (USA/World ROMs) โ”‚ โ””โ”€โ”€ Beta/ โ”‚ โ””โ”€โ”€ Zelda (USA) [Beta].nes โ† USA, Beta (not excluded) โ””โ”€โ”€ By Region/ โ”œโ”€โ”€ Europe/ โ”‚ โ””โ”€โ”€ Castlevania (Europe).nes โ† Europe, no kind โ””โ”€โ”€ Japan/ โ”œโ”€โ”€ Final Fantasy (Japan).nes โ† Japan, no kind โ””โ”€โ”€ By Kind/ โ† Within Japan folder! โ””โ”€โ”€ Proto/ โ””โ”€โ”€ Metroid (Japan) [Proto].nes โ† Japan, Proto ``` ### Example 2: English Build ```bash #!/bin/bash # build-snes-eng-1g1r.sh # Organize all regions export KEEP_IN_ROOT_REGIONS="" # Keep English in root export KEEP_IN_ROOT_LANGUAGES="En" # Organize all kinds recursively export KEEP_IN_ROOT_KINDS="" ./organizers/organize-roms.sh "/path/to/output" ``` **Result:** - All regions organized into `By Region//` - English ROMs stay in root - Other languages go to `By Language//` - Kinds organized recursively everywhere ### Example 3: All Regions Build (Future) ```bash #!/bin/bash # build-gba-all-1g1r.sh # Organize ALL regions (nothing in root) export KEEP_IN_ROOT_REGIONS="" # Don't organize by language # (KEEP_IN_ROOT_LANGUAGES not set) # Organize all kinds export KEEP_IN_ROOT_KINDS="" ./organizers/organize-roms.sh "/path/to/output" ``` **Result:** - Every region gets its own folder - No language organization - Kinds organized within each region folder ### Example 4: No Organization (Variable Not Set) ```bash #!/bin/bash # Don't set any variables ./organizers/organize-roms.sh "/path/to/output" ``` **Result:** - No organization performed - All ROMs stay exactly where they are ## ๐Ÿ”ง Technical Implementation ### Phase 1: Region Organization ```bash if [[ -v KEEP_IN_ROOT_REGIONS ]]; then for region in "${REGION_ARRAY[@]}"; do if [[ ",${KEEP_IN_ROOT_REGIONS}," == *",${region},"* ]]; then # Skip - keep in root continue fi # Organize into By Region// organize_region "$inFolder" "$region" done fi ``` ### Phase 2: Language Organization ```bash if [[ -v KEEP_IN_ROOT_LANGUAGES ]]; then for language in "${LANGUAGE_ARRAY[@]}"; do if [[ ",${KEEP_IN_ROOT_LANGUAGES}," == *",${language},"* ]]; then # Skip - keep in root continue fi # Organize into By Language// organize_language "$inFolder" "$language" done fi ``` ### Phase 3: Recursive Kind Organization ```bash if [[ -v KEEP_IN_ROOT_KINDS ]]; then # Organize kinds in root organize_kinds_in_folder "$inFolder" "root" # Organize kinds in each region folder for region_dir in "$inFolder/By Region"/*; do organize_kinds_in_folder "$region_dir" "region:$(basename "$region_dir")" done # Organize kinds in each language folder for lang_dir in "$inFolder/By Language"/*; do organize_kinds_in_folder "$lang_dir" "language:$(basename "$lang_dir")" done fi ``` ## โœ… Validation The script validates all KEEP_IN_ROOT values: ```bash # Validates USA and World exist in REGION_ARRAY export KEEP_IN_ROOT_REGIONS="USA,World" # Validates En exists in LANGUAGE_ARRAY export KEEP_IN_ROOT_LANGUAGES="En" # Validates Demo exists in GLOBAL_KINDS export KEEP_IN_ROOT_KINDS="Demo" # Invalid values will trigger warnings export KEEP_IN_ROOT_REGIONS="USA,InvalidRegion" # Output: [WARN] Unknown region in KEEP_IN_ROOT_REGIONS: 'InvalidRegion' ``` ## ๐Ÿงช Testing Test with dry-run mode: ```bash export KEEP_IN_ROOT_REGIONS="USA,World" export KEEP_IN_ROOT_KINDS="Demo" ./organizers/organize-roms.sh /path/to/roms --dry-run ``` ## ๐Ÿ“– Help Output Run with `--help` to see full documentation: ```bash ./organizers/organize-roms.sh --help ``` ## ๐ŸŽฏ Benefits 1. **Flexible** - Three modes per variable (not set, empty, with values) 2. **Clear Intent** - Variable names communicate behavior 3. **Recursive** - Kinds organize everywhere, not just root 4. **Validated** - Catches typos and invalid values 5. **Scriptable** - Perfect for build scripts 6. **Backward Compatible** - Old behavior available (don't set variables) ## ๐Ÿ”ฎ Future Enhancements 1. **Auto-detection** - Detect from calling script name (e.g., `build-nes-usa-1g1r.sh` auto-sets USA) 2. **Config files** - Support `.romgroomerrc` config files 3. **Interactive mode** - Prompt for settings if variables not set 4. **Preset modes** - `--mode=usa`, `--mode=all`, `--mode=english` ## ๐Ÿ“ฆ Files Modified - `/data/emu/rom-groomer/organizers/organize-roms.sh` - Complete rewrite of organization logic - `/data/emu/rom-groomer/docs/KEEP-IN-ROOT-IMPLEMENTATION.md` - This document ## ๐ŸŽ‰ Status โœ… **Fully implemented and tested** โœ… **Recursive kind organization working** โœ… **Validation working** โœ… **Ready for use in build scripts** --- **Next Steps**: Update your build scripts to use the new `KEEP_IN_ROOT_*` variables!