#!/usr/bin/env bash

set -e

echo " ┌──────────────────────────────────────────────┐"
echo " │          Compiling CCIP contracts...         │"
echo " └──────────────────────────────────────────────┘"

# The offRamp uses a specific lower optimization runs value. All other contracts use the default value
# as specified in the foundry.toml.
OPTIMIZE_RUNS_OFFRAMP=800
OPTIMIZE_RUNS_FEE_QUOTER=3000
OPTIMIZE_RUNS_LARGE_TOKEN_POOL=5000
PROJECT="ccip"
FOUNDRY_PROJECT_SUFFIX="-compile"
export FOUNDRY_PROFILE="$PROJECT"$FOUNDRY_PROJECT_SUFFIX

CONTRACTS_DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; cd ../ && pwd -P )"
ABI_DIR="$CONTRACTS_DIR"/abi/
mkdir -p "$ABI_DIR"

compileContract() {
  local contract
  contract=$(basename "$1")
  echo "Compiling" "$contract"
  dir=$CONTRACTS_DIR/solc/$PROJECT/$contract
  
  local args # base args for forge
  args="build $CONTRACTS_DIR/contracts/"$1.sol" --root $CONTRACTS_DIR"
  
  if [ "$ZKSYNC" = "true" ]; then
    $(dirname "$0")/forge_zksync $args --zksync
  else
    local command
    command="forge $args \
    $(getOptimizations "$contract") \
    --extra-output-files bin abi metadata \
    --build-info \
    --build-info-path $dir/build \
    -o $dir"

    $command

    # Move the build info to an expected file name
    mv $(find $dir/build -type f -name '*.json' ! -name 'build.json') $dir/build/build.json

    # Copy the generated abi files to a single folder
    cp "$CONTRACTS_DIR"/solc/$PROJECT/"$contract"/"$contract".sol/"$contract".abi.json "$ABI_DIR""$contract".abi.json
  fi
}

# Define optimization overrides in this function. Anything that is not an override will use the default value
# as specified in the foundry.toml.
function getOptimizations() {
    local optimize_runs_override=""

    case $1 in
      "OffRamp" | "OffRampOverSuperchainInterop" | "OffRampWithMessageTransformer")
        optimize_runs_override="--optimizer-runs $OPTIMIZE_RUNS_OFFRAMP"
        ;;
      "FeeQuoter" | "FeeQuoterV2")
        optimize_runs_override="--optimizer-runs $OPTIMIZE_RUNS_FEE_QUOTER"
        ;;
      "BurnMintFastTransferTokenPool" | "HybridLockReleaseUSDCTokenPool")
        optimize_runs_override="--optimizer-runs $OPTIMIZE_RUNS_LARGE_TOKEN_POOL"
        ;;
    esac

    echo "$optimize_runs_override"
}


# Mod sec
compileContract ccvs/VerifierProxy
compileContract ccvs/CommitteeVerifier
compileContract onRamp/CCVProxy
compileContract onRamp/ExecutorOnRamp
compileContract offRamp/CCVAggregator

# 1.6
compileContract offRamp/OffRamp
compileContract FeeQuoter
compileContract FeeQuoterV2
compileContract onRamp/OnRamp
compileContract applications/PingPongDemo
compileContract applications/EtherSenderReceiver
compileContract Router
compileContract tokenAdminRegistry/TokenAdminRegistry
compileContract tokenAdminRegistry/RegistryModuleOwnerCustom
compileContract tokenAdminRegistry/TokenPoolFactory/TokenPoolFactory
compileContract tokenAdminRegistry/TokenPoolFactory/FactoryBurnMintERC20
compileContract tokenAdminRegistry/TokenPoolFactory/HyperLiquidCompatibleERC20
compileContract capability/CCIPHome
compileContract NonceManager
compileContract rmn/RMNRemote
compileContract rmn/RMNHome
compileContract rmn/RMNProxy
compileContract DonIDClaimer

# Pools
compileContract pools/LockReleaseTokenPool
compileContract pools/BurnMintTokenPool
compileContract pools/BurnFromMintTokenPool
compileContract pools/BurnWithFromMintTokenPool
compileContract pools/BurnToAddressMintTokenPool
compileContract pools/TokenPool
compileContract pools/USDC/USDCTokenPool
compileContract pools/USDC/CCTPMessageTransmitterProxy
compileContract pools/SiloedLockReleaseTokenPool
compileContract pools/BurnMintFastTransferTokenPool
compileContract pools/ERC20LockBox
compileContract pools/USDC/SiloedUSDCTokenPool
compileContract pools/USDC/USDCTokenPoolCCTPV2
compileContract pools/USDC/BurnMintWithLockReleaseFlagTokenPool

# Test helpers
compileContract test/helpers/MessageHasher
compileContract test/helpers/USDCReaderTester
compileContract test/helpers/ReportCodec
compileContract test/helpers/receivers/MaybeRevertMessageReceiver
compileContract test/helpers/receivers/LogMessageDataReceiver
compileContract test/helpers/MultiOCR3Helper
compileContract test/mocks/MockE2EUSDCTokenMessenger
compileContract test/mocks/MockE2EUSDCTransmitter
compileContract test/mocks/MockE2ELBTCTokenPool
compileContract test/mocks/MockReceiverV2
compileContract test/helpers/CCIPReaderTester
# Offchain test encoding utils
compileContract test/helpers/EncodingUtils

# Message Transformer On/OffRamps
compileContract offRamp/OffRampWithMessageTransformer
compileContract onRamp/OnRampWithMessageTransformer

# Superchain Interop On/OffRamps
compileContract onRamp/OnRampOverSuperchainInterop
compileContract offRamp/OffRampOverSuperchainInterop

