#!/bin/bash
Help()
{
   echo "NAME"
   echo "ConvertTo-Csv"
   echo
   echo "SYNOPSIS"
   echo "Converts nfdump files to csv files."
   echo
   echo "SYNTAX"
   echo "$0 [-r <String>] [-w <String>]"
   echo
   echo "DESCRIPTION"
   echo "Reads all NFDUMP source files in the source folder and generates corresponding CSV files in the destination path."
   echo "Destination files will have the same name as input files but with .csv extension. Exiting files will be overwritten. "
   echo
   echo "PARAMETERS"
   echo "-r"
   echo "    Specifies the source folder that directly contains NFDUMP files."
   echo
   echo "-w"
   echo "    Specifies the destination folder in which the generated CSV files will be created."
}

while getopts ":h:r:w:" option; do
   case $option in
      h) # display Help
         Help
         exit;;
      r)
        inputFolder=${OPTARG}
        ;;
      w)
        outputFolder=${OPTARG}
        ;;
     *) # incorrect option
         Help
         exit;;
   esac
done

if [ -z "$inputFolder" ]
  then
    echo "Error: No source folder specified."
    exit
fi
if [ -z "$outputFolder" ]
  then
    echo "Error: No destination folder specified."
    exit
fi

for inputFile in $inputFolder/*
do
    outputFile=`basename $inputFile`
    echo "Processing $inputFile ---> $outputFolder/$outputFile.csv"
    /usr/local/bin/nfdump -r $inputFile -o csv ipv4 | head -n -3 1> $outputFolder/$outputFile.csv
done
