#!/bin/bash

version="0.1.0"
hbar="----------------------------------------------------"

# Information
echo $hbar
echo "MBARI Radar PCAP processing script version $version"
echo $hbar

# Input arguments 
if [ "$#" -ne 2 ]; then
    echo "Error: incorrect number of arguments send to program."
    echo "usage: $0 [SRCDIR] [DESTDIR]"
fi

SRCDIR=$1
DESTDIR=$2

echo "source dir: $SRCDIR"
echo "destination dir: $DESTDIR"

#echo $( ls -a $SRCDIR )

#make sure target subdirs exist 
for d in $(ls -p $SRCDIR | grep /)
do
    echo $hbar
    read -r -p "Process $d? (Y/n) " input
    case "$input" in ([yY][eE][sS]|[yY])
        echo "creating dir: $DESTDIR/$d"
        mkdir -p $DESTDIR/$d
        basename -s .pcapng $(ls $SRCDIR/$d//*.pcapng ) | xargs -P 8 -I [] pcaptoradar -o $DESTDIR/$d/[].h5 $SRCDIR/$d[].pcapng
        echo $hbar
        echo "compressing files in destination"
        find $DESTDIR/$d//*.h5 | xargs -P8 -I [] gzip -v []
        ;;
    esac
done

