#!/bin/bash

#
# OpenTravelData (OPTD) utility
# Git repository:
#   https://github.com/opentraveldata/opentraveldata/tree/master/tools
#

# Compare the best known airline details with reference data
# - optd_airline_best_known_so_far.csv
# - dump_from_ref_airline.csv
#
# => optd_airline_diff_w_ref.csv
#

##
# GNU tools, including on MacOS
source setGnuTools.sh || exit -1

##
# Directories
source setDirs.sh "$0" || exit -1

##
# OpenTravelData directory
OPTD_DIR="$(dirname ${EXEC_FULL_PATH})"
OPTD_DIR="${OPTD_DIR}/"

##
# OPTD sub-directories
DATA_DIR="${OPTD_DIR}opentraveldata/"
TOOLS_DIR="${OPTD_DIR}tools/"

##
# Log level
LOG_LEVEL=3

##
# File of best known airline details (future)
OPTD_AIR_FILENAME="optd_airline_best_known_so_far.csv"
#
OPTD_AIR_FILE="${DATA_DIR}${OPTD_AIR_FILENAME}"

##
# REF (to be found, as temporary files, within the ../tools directory)
REF_AIR_FILENAME="dump_from_ref_airline.csv"
#
REF_AIR_FILE="${TOOLS_DIR}${REF_AIR_FILENAME}"
# REF with primary key (generated by the
# ../tools/prepare_ref_dump_file.sh script)
REF_CAP_FILENAME="cap_${REF_AIR_FILENAME}"
#
REF_CAP_FILE="${TOOLS_DIR}${REF_CAP_FILENAME}"

##
# Target (generated files)
OPTD_AIR_REF_DIFF_FILENAME="optd_airline_diff_w_ref.csv"
#
OPTD_AIR_REF_DIFF_FILE="${DATA_DIR}${OPTD_AIR_REF_DIFF_FILENAME}"

##
# Temporary
OPTD_AIR_HEADER="${OPTD_AIR_REF_DIFF_FILE}.tmp.hdr"
OPTD_AIR_WITH_NOHD="${OPTD_AIR_REF_DIFF_FILE}.wohd"
OPTD_AIR_UNSORTED_NOHDR="${OPTD_AIR_REF_DIFF_FILE}.wohd.unsorted"
OPTD_AIR_PUBLIC_UNSORTED_FILE="${OPTD_AIR_REF_DIFF_FILE}.unsorted"


##
# Sanity check
if [ ! -d ${TOOLS_DIR} ]
then
	echo
	echo "[$0:$LINENO] The tools/ sub-directory ('${TOOLS_DIR}') does not exist or is not accessible."
	echo "Check that your Git clone of the OpenTravelData is complete."
	echo
	exit -1
fi
if [ ! -f ${TOOLS_DIR}prepare_ref_dump_file.sh ]
then
	echo
	echo "[$0:$LINENO] The REF file preparation script ('${TOOLS_DIR}prepare_ref_dump_file.sh') does not exist or is not accessible."
	echo "Check that your Git clone of the OpenTravelData is complete."
	echo
	exit -1
fi


##
# Usage helper
#
if [ "$1" = "-h" -o "$1" = "--help" ]
then
	echo
	echo "That script compares the OPTD-maintained best known airline details with reference data"
	echo
	echo "Usage: $0 [<log level (0: quiet; 5: verbose)>]"
	echo " - Default log level (from 0 to 5): ${LOG_LEVEL}"
	echo
	echo "* Input data files"
	echo "------------------"
	echo " - OPTD-maintained file of best known details: '${OPTD_AIR_FILE}'"
	echo " - REF data dump file: '${REF_AIR_FILE}'"
	echo
	echo "* Output data file"
	echo "------------------"
	echo " - List of airlines for which the REF-derived names are different: '${OPTD_AIR_REF_DIFF_FILE}'"
	echo
	exit
fi


##
# Cleaning
#
if [ "$1" = "--clean" ]
then
	\rm -f ${OPTD_AIR_WITH_NOHD} ${OPTD_AIR_UNSORTED_NOHDR} \
		${OPTD_AIR_PUBLIC_UNSORTED_FILE} ${REF_CAP_FILE}

	bash prepare_ref_dump_file.sh --clean || exit -1
	exit
fi


##
# Log level
if [ "$1" != "" ]
then
	LOG_LEVEL="$1"
fi


##
# Preparation
bash prepare_ref_dump_file.sh ${OPTD_DIR} ${TOOLS_DIR} ${LOG_LEVEL} || exit -1

##
#
if [ ! -f ${REF_CAP_FILE} ]
then
	echo
	echo "[$0:$LINENO] The '${REF_CAP_FILE}' file does not exist."
	echo
	exit -1
fi

##
# Re-format the aggregated entries. See ${REDUCER} for more details and samples.
REDUCER="compare_airline_files.awk"
awk -F'^' -f ${REDUCER} ${OPTD_AIR_FILE} ${REF_CAP_FILE} \
	> ${OPTD_AIR_PUBLIC_UNSORTED_FILE}

##
# Extract the header into temporary files
grep -E "^code(.+)" ${OPTD_AIR_PUBLIC_UNSORTED_FILE} > ${OPTD_AIR_HEADER}

##
# Remove the header
${SED_TOOL} -E "s/^code(.+)//g" ${OPTD_AIR_PUBLIC_UNSORTED_FILE} \
	> ${OPTD_AIR_WITH_NOHD}
${SED_TOOL} -i"" -E "/^$/d" ${OPTD_AIR_WITH_NOHD}

##
# Sort on the code
sort -t'^' -k1,1 ${OPTD_AIR_WITH_NOHD} > ${OPTD_AIR_UNSORTED_NOHDR}

##
# Re-add the header
cat ${OPTD_AIR_HEADER} ${OPTD_AIR_UNSORTED_NOHDR} > ${OPTD_AIR_REF_DIFF_FILE}

##
# Delete the header file
\rm -f ${OPTD_AIR_HEADER}

##
# Reporting
#
echo
echo "Reporting Step"
echo "--------------"
echo
echo "${WC_TOOL} -l ${OPTD_AIR_REF_DIFF_FILE}"
echo
