#!/bin/bash # Configuration Switcher for moto_rd project # Usage: ./switch-config.sh [nand|nor] # Description: Switches between NAND and NOR flash configurations set -e # Color output for better readability RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Print colored output print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$SCRIPT_DIR" print_info "Project directory: $PROJECT_DIR" # Check command line arguments if [ $# -ne 1 ]; then print_error "Usage: $0 [nand|nor]" print_info "Example: $0 nand" print_info "Example: $0 nor" exit 1 fi CONFIG_TYPE="$1" # Validate config type if [[ ! "$CONFIG_TYPE" =~ ^(nand|nor)$ ]]; then print_error "Invalid configuration type: $CONFIG_TYPE" print_error "Must be either 'nand' or 'nor'" exit 1 fi print_info "Switching to $CONFIG_TYPE configuration..." # Define source and target files based on config type if [ "$CONFIG_TYPE" = "nand" ]; then DEFCONFIG_SRC="project_moto_rd_nand_defconfig" PARTITION_INFO_SRC="partition-info-nand.txt" PARTITION_CONFIG_SRC="partition-config-nand.json" elif [ "$CONFIG_TYPE" = "nor" ]; then DEFCONFIG_SRC="project_moto_rd_nor_defconfig" PARTITION_INFO_SRC="partition-info-nor.txt" PARTITION_CONFIG_SRC="partition-config-nor.json" fi # Define target files DEFCONFIG_TGT="project_moto_rd_defconfig" PARTITION_INFO_TGT="partition-info.txt" PARTITION_CONFIG_TGT="partition-config.json" # Verify source files exist print_info "Verifying source files..." if [ ! -f "$PROJECT_DIR/$DEFCONFIG_SRC" ]; then print_error "Source file not found: $DEFCONFIG_SRC" exit 1 fi if [ ! -f "$PROJECT_DIR/$PARTITION_INFO_SRC" ]; then print_error "Source file not found: $PARTITION_INFO_SRC" exit 1 fi if [ ! -f "$PROJECT_DIR/$PARTITION_CONFIG_SRC" ]; then print_error "Source file not found: $PARTITION_CONFIG_SRC" exit 1 fi print_info "All source files verified" # Create backups if target files exist backup_if_exists() { local file="$1" if [ -f "$PROJECT_DIR/$file" ]; then local backup_file="${PROJECT_DIR}/${file}.backup" cp "$PROJECT_DIR/$file" "$backup_file" print_info "Created backup: $file.backup" fi } print_info "Creating backups of existing files..." backup_if_exists "$DEFCONFIG_TGT" backup_if_exists "$PARTITION_INFO_TGT" backup_if_exists "$PARTITION_CONFIG_TGT" # Copy files print_info "Copying configuration files..." cp "$PROJECT_DIR/$DEFCONFIG_SRC" "$PROJECT_DIR/$DEFCONFIG_TGT" print_info "Copied: $DEFCONFIG_SRC -> $DEFCONFIG_TGT" cp "$PROJECT_DIR/$PARTITION_INFO_SRC" "$PROJECT_DIR/$PARTITION_INFO_TGT" print_info "Copied: $PARTITION_INFO_SRC -> $PARTITION_INFO_TGT" cp "$PROJECT_DIR/$PARTITION_CONFIG_SRC" "$PROJECT_DIR/$PARTITION_CONFIG_TGT" print_info "Copied: $PARTITION_CONFIG_SRC -> $PARTITION_CONFIG_TGT" # Verify copy operation print_info "Verifying copied files..." if [ ! -f "$PROJECT_DIR/$DEFCONFIG_TGT" ]; then print_error "Failed to copy: $DEFCONFIG_TGT" exit 1 fi if [ ! -f "$PROJECT_DIR/$PARTITION_INFO_TGT" ]; then print_error "Failed to copy: $PARTITION_INFO_TGT" exit 1 fi if [ ! -f "$PROJECT_DIR/$PARTITION_CONFIG_TGT" ]; then print_error "Failed to copy: $PARTITION_CONFIG_TGT" exit 1 fi print_info "All files verified successfully" # Display summary print_info "" print_info "=== Configuration Switch Summary ===" print_info "Configuration type: $CONFIG_TYPE" print_info "Files updated:" print_info " - $DEFCONFIG_TGT" print_info " - $PARTITION_INFO_TGT" print_info " - $PARTITION_CONFIG_TGT" print_info "" print_info "Backup files created (if originals existed):" print_info " - ${DEFCONFIG_TGT}.backup" print_info " - ${PARTITION_INFO_TGT}.backup" print_info " - ${PARTITION_CONFIG_TGT}.backup" print_info "" print_info "Configuration switch completed successfully!" print_info "" print_info "Next steps:" print_info " 1. Review the updated configuration files" print_info " 2. Run your build command to apply the new configuration" exit 0