#!/usr/bin/env bash
# Exit immediately if a command exits with a non-zero status
set -e
# Target directory path and file
TARGET_DIR="src/main/java/com/citigroup/qffcometbridge/server"
TARGET_FILE="${TARGET_DIR}/QffBridgeServer.java"
INJECTION_STR="com.citigroup.qffcometbridge.server.ProfilerAgent.initProfilerAgent();"
# 1. Ensure the directory exists (sanity check)
if [ ! -d "$TARGET_DIR" ]; then
echo "Error: Directory '$TARGET_DIR' does not exist."
exit 1
fi
# 2. Check if the target file exists
if [ ! -f "$TARGET_FILE" ]; then
echo "Error: Target file '$TARGET_FILE' not found."
exit 1
fi
# 3. Check if the injection is already present anywhere in the file
if grep -q "initProfilerAgent" "$TARGET_FILE"; then
echo "[INFO] ProfilerAgent embedding is already present in $TARGET_FILE. Skipping injection."
exit 0
fi
# 4. Locate the line number of the main method signature
TARGET_LINE=$(grep -n "public[[:space:]]\+static[[:space:]]\+void[[:space:]]\+main" "$TARGET_FILE" | head -n 1 | cut -d: -f1)
if [ -z "$TARGET_LINE" ]; then
echo "Error: Could not locate the 'public static void main' method signature in $TARGET_FILE."
exit 1
fi
echo "[INFO] Found main() definition at line $TARGET_LINE"
# 5. Inject the call exactly 1 line below the main method signature using awk
# Preserves 8 spaces of indentation to align perfectly inside the method
PAYLOAD=" ${INJECTION_STR}"
echo "[INFO] Injecting ProfilerAgent call into $TARGET_FILE..."
awk -v line="${TARGET_LINE}" -v text="${PAYLOAD}" '1; NR==line {print text}' "$TARGET_FILE" > temp_file && mv temp_file "$TARGET_FILE"
echo "[SUCCESS] Successfully injected initProfilerAgent() right under main()."
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)