#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage: $0 input.pcap"
    exit 1
fi

input_pcap="$1"
output_dir="split_output/"
rm -rf $output_dir && mkdir -p $output_dir

num=$(tshark -r "$input_pcap" -T fields -e frame.number | tail -n 1)
echo -e "\e[32m input pcap $input_pcap has $num packets\e[0m"

for ((i = 1; i <= num; i++)); do
    tshark -r "$input_pcap" -Y "frame.number==$i" -w "$output_dir/packet_$i.pcap"
done

echo -e "\e[32m split pcap is saved in $output_dir\e[0m"
