# Script Consolidation and Modularization Plan

## Overview

This document outlines the consolidation and modularization strategy for scripts in this repository. The goal is to reduce duplication, improve maintainability, and create reusable components.

## Library Structure (Created)

✅ **scripts/lib/** - Common libraries
- `lib/common/colors.sh` - Color definitions
- `lib/common/logging.sh` - Logging functions
- `lib/common/paths.sh` - Path definitions
- `lib/common/utils.sh` - Utility functions
- `lib/config/env.sh` - Environment loading
- `lib/config/regions.sh` - Region code mapping (single source of truth)
- `lib/azure/cli.sh` - Azure CLI wrappers
- `lib/init.sh` - Initialize all libraries

## Script Groups Identified for Consolidation

### 1. Deployment Scripts - High Duplication

#### Monitor Scripts (Can be consolidated into 1-2 scripts)
- `monitor-deployment.sh`
- `monitor-and-complete.sh`
- `monitor-and-fix.sh`
- `monitor-continuous.sh`
- `monitor-deployment-live.sh`
- `live-monitor.sh`
- `continuous-monitor.sh`
- `monitor-36-region-deployment.sh`
- `deployment-dashboard.sh`

**Action**: Create `monitor-deployment.sh` with modes: `--continuous`, `--live`, `--complete`, `--fix`

#### Parallel Deployment Scripts (Can be consolidated)
- `deploy-parallel.sh`
- `deploy-all-parallel.sh`
- `deploy-besu-parallel.sh`
- `deploy-max-parallel.sh`
- `deploy-ultra-parallel.sh`
- `deploy-besu-max-parallel.sh`
- `deploy-monitoring-parallel.sh`
- `configure-kubernetes-parallel.sh`
- `configure-kubernetes-max-parallel.sh`
- `deploy-contracts-parallel.sh`
- `verify-all-clusters-parallel.sh`
- `verify-all-max-parallel.sh`

**Action**: Create generic `deploy-parallel.sh` with resource type parameter

#### Phase Deployment Scripts
- `deploy-infrastructure-phase1.sh`
- `deploy-infrastructure-phase2.sh`
- `deploy-infrastructure-phase3.sh`
- `deploy-infrastructure-phase4.sh`
- `deploy-infrastructure-all-phases.sh`
- `deploy-all-phases.sh`
- `complete-all-phases-parallel.sh`
- `execute-all-phases.sh`
- `wait-and-complete-all.sh`

**Action**: Create `deploy-phase.sh` with phase number parameter

#### Complete/All Scripts
- `complete-all-deployment.sh`
- `complete-all-next-steps.sh`
- `complete-all-tasks.sh`
- `complete-infrastructure-deployment.sh`
- `wait-and-run-all-next-steps.sh`
- `run-all-next-steps.sh`
- `wait-and-run-next-steps.sh`
- `run-next-steps-with-available.sh`

**Action**: Consolidate into `complete-deployment.sh` with step options

### 2. Cost Calculation Scripts - High Duplication

- `calculate-accurate-costs.sh`
- `calculate-accurate-deployment-costs.sh`
- `calculate-conservative-costs.sh`
- `calculate-contract-deployment-costs.sh`
- `calculate-gas-fees.sh`
- `update-all-cost-estimates.sh`
- `update-cost-estimates.sh`
- `finalize-cost-estimates.sh`
- `get-accurate-gas-price.sh`
- `get-conservative-gas-price.sh`
- `get-mainnet-gas-prices.sh`
- `get-real-gas-prices.sh`
- `test-etherscan-gas-api.sh`

**Action**: Create `lib/deployment/costs.sh` library + single `calculate-costs.sh` script

### 3. Verification Scripts

- `verify-chain138-complete.sh`
- `verify-chain138-full-deployment.sh`
- `verify-chain138-services.sh`
- `verify-deployment.sh`
- `verify-mainnet-deployments.sh`
- `verify-on-chain-deployments.sh`
- `verify-all-clusters-parallel.sh`
- `verify-36-region-clusters.sh`
- `verify-all-max-parallel.sh`
- `verify-contract-etherscan.sh`

**Action**: Create `verify-deployment.sh` with resource type and mode options

### 4. Contract Deployment Scripts

- `deploy-weth.sh`
- `deploy-weth10.sh`
- `deploy-weth-with-ccip.sh`
- `deploy-ccip-weth9-bridge.sh`
- `deploy-ccip-weth10-bridge.sh`
- `deploy-multicall.sh`
- `deploy-multisig.sh`
- `deploy-ccip-router.sh`

**Action**: Create generic `deploy-contract.sh` with contract name parameter

### 5. Check/Status Scripts

- `check-deployment-status.sh`
- `check-infrastructure-status.sh`
- `check-mainnet-deployment-status.sh`
- `check-terraform-status.sh`
- `check-all-deployment-sources.sh`
- `check-existing-deployments.sh`
- `check-mainnet-balances.sh`
- `check-wallet-balances.sh`
- `check-and-proceed.sh`
- `check-rpc-status.sh`

**Action**: Create `check-status.sh` with resource type parameter

### 6. Import Scripts

- `import-existing-clusters.sh`
- `import-all-resources.sh`

**Action**: Create `import-resources.sh` with resource type parameter (these are already similar)

## Migration Strategy

### Phase 1: Library Adoption ✅ (In Progress)
1. ✅ Create common library structure
2. ✅ Create shared utilities
3. 🔄 Update key scripts to use libraries (example created)

### Phase 2: High-Impact Consolidations
1. Consolidate monitor scripts
2. Consolidate parallel deployment scripts
3. Consolidate cost calculation scripts

### Phase 3: Medium-Impact Consolidations
1. Consolidate verification scripts
2. Consolidate check/status scripts
3. Consolidate contract deployment scripts

### Phase 4: Cleanup
1. Remove deprecated scripts
2. Update documentation
3. Update Makefile targets

## Script Template

All new scripts should follow this template:

```bash
#!/bin/bash

# Script description
# Usage: script-name.sh [OPTIONS]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --option)
            OPTION="$2"
            shift 2
            ;;
        *)
            log_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Main script logic
ensure_azure_cli || exit 1
log_section "Script Title"
```

## Benefits

1. **Reduced Duplication**: ~50% reduction in script count
2. **Single Source of Truth**: Region codes, colors, utilities defined once
3. **Consistency**: All scripts use same logging, error handling
4. **Maintainability**: Update once, applies everywhere
5. **Easier Testing**: Reusable functions can be unit tested
6. **Better Documentation**: Clear library structure

## Progress Tracking

- [x] Create library structure
- [x] Create common utilities
- [x] Create example refactored script
- [ ] Migrate high-priority scripts
- [ ] Consolidate monitor scripts
- [ ] Consolidate deployment scripts
- [ ] Consolidate cost scripts
- [ ] Update documentation
- [ ] Remove deprecated scripts

