#!/usr/bin/env bash
#
# Generates a list of files with absolute path.
# Input can be composed of files and folders.
#
# Usage: t2caplist [OPTION...] files > pcap_list.txt

source "$(dirname "$0")/t2utils.sh"

usage() {
    echo "Usage:"
    echo "    $SNAME [OPTION...] <FILE|DIR>"
    echo
    echo "Optional arguments:"
    echo "    -d depth          List pcaps up to the given depth"
    echo "    -L                Follow symbolic links"
    echo "    -r                List pcaps recursively"
    echo "    -R                Sort the list in reverse order"
    echo "    -z                Sort the list by file size"
    echo "    -s                Do not sort the list"
    echo "    -v                Report invalid files to stderr"
    echo
    echo "Help and documentation arguments:"
    echo "    -h, -?, --help    Show this help, then exit"
}

cleanup() {
    [ -f "$TEMPFILE" ] && rm "$TEMPFILE"
    exit $ret
}

# Default values
DEPTH_LEVEL=1

while [ $# -gt 0 ]; do
    case "$1" in
        -d|--depth)
            validate_next_num "$1" "$2"
            DEPTH_LEVEL="$2"
            shift
            ;;
        -z|--size) SIZE=1;;
        -s|--no-sort) NOSORT=1;;
        -r|--recursive) RECURSIVE=1;;
        -R|--reverse) SORDER="-r";;
        -v|--verbose) VERBOSE=1;;
        -L|--symlinks) SYMLINKS="$1";;
        -h|-\?|--help) usage; exit 0;;
        *)
            if [ -d "$1" ]; then
                DIRS+=("$1")
            elif [ -f "$1" ]; then
                FILES+=("$1")
            else
                abort_option_unknown "$1"
            fi
            ;;
    esac
    shift
done

# No files or directory specified
if [ -z "$FILES" ] && [ -z "$DIRS" ]; then
    DIRS="."
fi

if [ -z "$RECURSIVE" ]; then
   DEPTH="-maxdepth $DEPTH_LEVEL"
fi

# Setup signal handler
trap "trap - SIGTERM && cleanup 1" HUP INT QUIT TERM
trap "cleanup \$?" EXIT

TEMPFILE=$(mktemp)

for i in "${DIRS[@]}"; do
    find $SYMLINKS "$($READLINK -f "$i")" $DEPTH -type f -exec file {} \; | \
        grep -wi 'capture file' | AWK -F': ' '{ print $1 }' >> "$TEMPFILE"
done

for i in "${FILES[@]}"; do
    if [ -f "$i" ] && [ ! -z "$(file "$i" | grep -wi 'capture file')" ]; then
        $READLINK -f "$i" >> "$TEMPFILE"
    elif [ -n "$VERBOSE" ]; then
        printwrn "'$i' is not a valid pcap file" >&2
    fi
done

if [ ! -s "$TEMPFILE" ]; then
    printerr "No valid PCAP files found"
    exit 1
fi

if [ $NOSORT ]; then
    cat "$TEMPFILE"
elif [ $SIZE ]; then
    while IFS= read -r line
    do
        du "$line"
    done < <(cat "$TEMPFILE") | sort -f $SORDER -n | AWK '{
        $1 = "" # discard size
        gsub("^" OFS, "")
        print
    }'
elif [ "$(uname)" = "Darwin" ]; then
    sort -f $SORDER "$TEMPFILE"
else
    sort -f -V $SORDER "$TEMPFILE"
fi
