#!/usr/bin/bash

##
# That script extracts, from the log file generated by the TraDemGen binary,
# all the generated booking requests. The output (made of booking requests)
# can then be analysed with other tools.
#
# Note: the trademgen_drawBookingArrivals Python script extracts its data
#       directly from the same log file generated by the TraDemGen binary.
#       It therefore does not need this Shell script.
##

# Log file generated by the 'trademgen_generateDemand' binary
LOG_FILE=trademgen_generateDemand.log

#
LOG_FILE_OPTION=NO
for opt_elem in $@
do
	if [ "${opt_elem}" = "-h" -o "${opt_elem}" = "-H" -o "${opt_elem}" = "--h" -o "${opt_elem}" = "--help" ]
	then
		echo
		echo " This script extracts booking request data from the log file"
		echo " generated by the 'trademgen_generateDemand' binary."
		echo
		echo " It produces an output with the following format:"
		echo " Logfile line number, Booking date, Orign-Destination, Departure date"
		echo " For instance:"
		echo " 39, 2009-May-26, SIN-HND, 2010-Feb-09"
		echo
		echo " See also the trademgen_drawBookingArrivals script, which plots"
		echo " booking arrival curves from those very same booking requests."
		echo " Note that there is no dependency between those two scripts." 
    echo
    echo " Usage:"
    echo "  $0 [-i/--input <path-to-input-log-file>]"
	echo "    Default input file: '${LOG_FILE}'"
    echo
    exit 0
  fi

  if [ "${LOG_FILE_OPTION}" = "YES" -o "${opt_elem}" = "-i" -o "${opt_elem}" = "--input" ]
  then
      if [ "${LOG_FILE_OPTION}" = "YES" ]
      then
				LOG_FILE="${opt_elem}"
	  		LOG_FILE_OPTION="NO"
      else
	  		LOG_FILE_OPTION="YES"
      fi
  fi
done

#
grep -n "Poped" ${LOG_FILE} | \
 sed -e "s/^\([0-9]\+\).*\(20[0-9]\+-[A-Z][a-z]\+-[0-9]\+\).*\ \([A-Z]\+-[A-Z]\+\)\ .*\(20[0-9]\+-[A-Z][a-z]\+-[0-9]\+\).*$/\1,\t\2, \3, \4/" | sort -k3

