#!/pkg/bin/ksh

parentDir="/misc/disk1/cc_dumps"
prefix=""
option=""
label=""
ipv4_list=""
ipv6_list=""
labels_list=""

while getopts "p:o:l:a:b:c:" opt; do
  case $opt in
    p)
      prefix="$OPTARG"
      ;;
    o)
      option="$OPTARG"
      ;;
    l)
      label="$OPTARG"
      ;;
    a)
      ipv4_list="${OPTARG// / }"
      ;;
    b)
      ipv6_list="${OPTARG// / }"
      ;;
    c)
      labels_list="${OPTARG// / }"
   ;;
  esac
done

shift $((OPTIND-1))

IFS=' ' read -ra ipv4_list <<< "${ipv4_list[0]}"
IFS=' ' read -ra ipv6_list <<< "${ipv6_list[0]}"
IFS=' ' read -ra labels_list <<< "${labels_list[0]}"

get_pci_stats() {
  # Check for and make folders if they don't exist
  command="if [ -d $parentDir ]; then : ; else mkdir $parentDir; fi"
  eval $command
  
  command="if [ -d \"$parentDir/fib_dumps\" ]; then rm -rf $parentDir/fib_dumps/*; else mkdir \"$parentDir/fib_dumps\"; fi"
  eval $command

  command="if [ -d \"$parentDir/pci_stats\" ]; then rm -rf $parentDir/pci_stats/*; else mkdir \"$parentDir/pci_stats\"; fi"
  eval $command

  command="/pkg/bin/./dump_resources.sh all all"
  eval $command

  cd "/misc/disk1" || exit
  files=$(ls route_summary_*_*.gz)
  for file in $files; do
    filename=$(basename "$file" .gz)
    output_file="$parentDir/pci_stats/$filename"
    gunzip -c "$file" > "$output_file"
  done
}

get_cef_data() {
  command='dpa_qosea_voq_show "-v" "x" "-i" "0x10" "-n" "A" "-t" "n" "-p" "0x0" > "$parentDir/fib_dumps/npufile.txt"'
  eval $command

  command='fib_show_command "-t" "-O" "0x0" "-f" "all" > "$parentDir/fib_dumps/sh_cef_vrf_all.txt"'
  eval $command

  command='show_platform_sysdb > "$parentDir/fib_dumps/show_platform.txt"'
  eval $command

  command='fib_show_command "-O" "0x0" "-3" | more "-f" > "$parentDir/fib_dumps/sh_cef_tables.txt"'
  eval $command
}

get_fib_data() {
  
  lc=$1
  
  command='fib_show_command  "-t" "-O" "0x0" "-f" "all" "-H" "egress" "-d" "-N" "'"$((lc*256))"'" > "$parentDir/fib_dumps/cef_ipv4_$lc.txt"'
  eval $command
  
  command='fib_show_command  "-t" "-O" "0x1" "-f" "all" "-H" "egress" "-d" "-N" "'"$((lc*256))"'" > "$parentDir/fib_dumps/cef_ipv6_$lc.txt"'
  eval $command

  command='fib_mpls_show_fwding "-h" "'"$((lc*256))"'" "-l" "0x0" "-k" "0xfffff" "-V" "-E" "-P" > "$parentDir/fib_dumps/mpls_labels_$lc.txt"'
  eval $command
}

get_fib_data_single_prefix() {
  lc=$1
  option=$2
  prefix=$3
  if [ "$option" == "0x2" ]; then
    command="fib_show_command  \"-t\" \"-O\" \"0x0\" \"-f\" \"all\" \"-H\" \"egress\" \"-P\" \"$prefix\" \"-T\" \"0x2\" \"-d\" \"-N\" \"$((lc*256))\" | more \"-f\" >> \"$parentDir/fib_dumps/cef_ipv4_$lc.txt\""
    
  elif [ "$option" == "0xa" ]; then
    command="fib_show_command  \"-t\" \"-O\" \"0x1\" \"-f\" \"all\" \"-H\" \"egress\" \"-P\" \"$prefix\" \"-T\" \"0xa\" \"-d\" \"-N\" \"$((lc*256))\" | more \"-f\" >> \"$parentDir/fib_dumps/cef_ipv6_$lc.txt\""
    
  fi

  eval $command
  
  if [ ! -e "$parentDir/fib_dumps/cef_ipv4_$lc.txt" ]; then
      touch "$parentDir/fib_dumps/cef_ipv4_$lc.txt"
  fi
  
  if [ ! -e "$parentDir/fib_dumps/cef_ipv6_$lc.txt" ]; then
      touch "$parentDir/fib_dumps/cef_ipv6_$lc.txt"
  fi
  
  if [ ! -e "$parentDir/fib_dumps/mpls_labels_$lc.txt" ]; then
    touch "$parentDir/fib_dumps/mpls_labels_$lc.txt"
  fi
  
}

get_fib_data_single_label() {
  lc=$1
  label=$2
  #command="fib_mpls_show_fwding \"-h\" \"0\" \"-l\" \"$label\" \"-V\" \"-E\" | more \"-f\" > \"$parentDir/fib_dumps/mpls_labels_$lc.txt\""
  command="fib_mpls_show_fwding \"-h\" \"$((lc*256))\" \"-l\" \"$label\" \"-V\" \"-E\" | more \"-f\" >> \"$parentDir/fib_dumps/mpls_labels_$lc.txt\""
  eval $command
  
  if [ ! -e "$parentDir/fib_dumps/cef_ipv4_$lc.txt" ]; then
      touch "$parentDir/fib_dumps/cef_ipv4_$lc.txt"
  fi
  
  if [ ! -e "$parentDir/fib_dumps/cef_ipv6_$lc.txt" ]; then
      touch "$parentDir/fib_dumps/cef_ipv6_$lc.txt"
  fi
  
  if [ ! -e "$parentDir/fib_dumps/mpls_labels_$lc.txt" ]; then
    touch "$parentDir/fib_dumps/mpls_labels_$lc.txt"
  fi
}



get_lc_list() {
  lc_list=()
  while IFS= read -r line; do
    if [[ $line =~ ([0-9]+)/([0-9]+)/CPU ]]; then
      lc_list+=("${BASH_REMATCH[1]}")  # Extract and add the first number (X) to lc_list
      lc_list+=("${BASH_REMATCH[2]}")  # Extract and add the second number (Y) to lc_list
    fi
  done < "$parentDir/fib_dumps/show_platform.txt"

  # Remove duplicates from lc_list and preserve order (requires Bash 4+)
  lc_list=($(printf "%s\n" "${lc_list[@]}" | sort -u))

  echo "${lc_list[*]}"
}

function make_tarfile() {
  local output_filename="$1"
  local source_dir="$2"
  tar -czf "$output_filename" -C "$(dirname "$source_dir")" "$(basename "$source_dir")"
}

echo "Getting PCI logs"
get_pci_stats
echo "Getting FIB logs"
get_cef_data
lc_list=$(get_lc_list)

if [ "${#ipv4_list[@]}" -ne 0 ]; then
    for ipv4_entry in "${ipv4_list[@]}"; do
        for lc in ${lc_list[@]}; do
            get_fib_data_single_prefix "$lc" "0x2" "$ipv4_entry"
        done
    done
fi
if [ "${#ipv6_list[@]}" -ne 0 ]; then
    for ipv6_entry in "${ipv6_list[@]}"; do
        for lc in ${lc_list[@]}; do
            get_fib_data_single_prefix "$lc" "0xa" "$ipv6_entry"
        done
    done
fi
if [ "${#labels_list[@]}" -ne 0 ]; then
    for mpls_label in "${labels_list[@]}"; do
        for lc in ${lc_list[@]}; do
            get_fib_data_single_label "$lc" "$mpls_label"
        done
    done
elif [ -z "$prefix" ] && [ -z "$option" ] && [ -z "$label" ]; then
    for lc in ${lc_list[@]}; do
        get_fib_data "$lc"
    done
elif [ -n "$prefix" ] && [ -n "$option" ] && [ -z "$label" ]; then
    for lc in ${lc_list[@]}; do
        get_fib_data_single_prefix "$lc" "$option" "$prefix"
    done
elif [ -n "$label" ] && [ -z "$prefix" ]; then
    for lc in ${lc_list[@]}; do
        get_fib_data_single_label "$lc" "$label"
    done
else
    # Handle the case when all three variables are present
    for lc in ${lc_list[@]}; do
        get_fib_data_single_prefix "$lc" "$option" "$prefix"
        get_fib_data_single_label "$lc" "$label"
    done
fi

make_tarfile "cc_dumps.tar" "$parentDir"
rm -rf "$parentDir"

