#!/bin/bash

if [[ ! $# -eq 1 ]] ; then
    echo "Usage: ${0##*/} 38331-xyz.docx"
    echo "38331-xyz.docx is a version of TS 38.331"
    echo "Creates one .asn pure ASN.1 text file per ASN.1 module in TS 38.331"
    echo "Message, IE, field and presence condition descriptions are included as comments"
    echo "Produces 2 versions:"
    echo "    - one with descriptions of fields and presence conditions at the end of the IE definition"
    echo "    - one with those descriptions before the concerned field(s)"
    exit 1
fi

if [[ ! -w . ]] ; then
    echo "Cannot write to current directory"
    exit 1
fi

if [[ ! -f "$1" ]] || [[ ! -r "$1" ]] ; then
    echo "$1 is not a readable file"
    exit
fi    

if [[ -e "${1%.docx}.txt" ]] ; then
    echo "${1%.docx}.txt already exists: skipping conversion"
else
    platform=$(uname)
    case $platform in
	Linux) libreoffice="libreoffice" ;;
	CYGWIN*) libreoffice="soffice" ;;
	*) echo "Unkown platform $platform"
	   exit 1;;
    esac

    if [[ ! -x $(which "$libreoffice") ]] ; then
	echo "Cannot find libreoffice executable"
	echo "Make sure it is installed and the PATH variable includes its path"
	exit 1
    fi
    $libreoffice --convert-to txt "$1"
    
    if [[ ! -f "${1%.docx}.txt" ]] || [[ ! -r "${1%.docx}.txt" ]] ; then
	echo "libreoffice did not create ${1%.docx}.txt"
	exit
    fi

    case $platform in
	# Replace EN-DASH (UTF-8 encoding) at line start with -
	Linux) LC_ALL=C sed -i $'s/^\u2013/-/g' "${1%.docx}.txt" ;;
	
	# Replace EN-DASH (Windows-1252 encoding) at line start with -
	# Remove CR (0x0d) put by libreoffice in windows before each LF (0x0a)
	CYGWIN*) LC_ALL=C sed -i $'s/^\x96/-/g; s/\r$//' "${1%.docx}.txt" ;;
	*) echo "Unkown platform $platform"
	   exit 1;;
    esac
    
    # Changes things that should not be so in the specification
    # Change "-- Cond N3C MP" to "-- Cond N3C-MP" (to respect condition naming rules)
    LC_ALL=C sed -i 's/-- Cond N3C MP/-- Cond N3C-MP/' "${1%.docx}.txt"
    LC_ALL=C sed -i 's/^N3C MP/N3C-MP/' "${1%.docx}.txt"
    LC_ALL=C sed -i 's/-- Cond HOAndServCellAdd,$/-- Cond HOAndServCellAdd/' "${1%.docx}.txt"
fi       

cat "${1%.docx}.txt" | awk '/^-- ASN1START$/,/^-- ASN1STOP$/' | awk '!/^[[:space:]]*--/ && !/^[[:space:]]*$/'| sed -e 's/--.*$//g' > "${1%.docx}.asncheck"

if [[ ! -e "${1%.docx}.asncheck" ]] ; then
    echo "${1%.docx}.asn generation failed"
    exit
fi

mkdir -p by-IE
cd by-IE
txt2asn.awk "../${1%.docx}.txt" > "${1%.docx}.asn"
cat "${1%.docx}.asn" | awk '!/^[[:space:]]*--/ && !/^[[:space:]]*$/'| sed -e 's/--.*$//g' > "${1%.docx}.asncheck"
compare1=$(diff -b -B "../${1%.docx}.asncheck" "${1%.docx}.asncheck")
if [[ "$compare1" ]] ; then
    echo "The ASN.1 in by-IE/${1%.docx}.asn does not fully match with the ASN.1 in ${1%.docx}.asncheck"
    echo "Check with:"
    echo "  diff -b -B by-IE/${1%.docx}.asncheck ${1%.docx}.asncheck"
else
    rm "${1%.docx}.asncheck"
fi

cd ..
mkdir -p before-field
cd before-field
txt2asn-integrated.awk "../${1%.docx}.txt" > "${1%.docx}.asn"
cat "${1%.docx}.asn" | awk '!/^[[:space:]]*--/ && !/^[[:space:]]*$/'| sed -e 's/--.*$//g' > "${1%.docx}.asncheck"
compare2=$(diff -b -B "../${1%.docx}.asncheck" "${1%.docx}.asncheck")
if [[ "$compare2" ]] ; then
    echo "The ASN.1 in before-field/${1%.docx}.asn does not fully match with the ASN.1 in ${1%.docx}.asncheck"
    echo "Check with:"
    echo "  diff -b -B before-field/${1%.docx}.asncheck ${1%.docx}.asncheck"
else
    rm "${1%.docx}.asncheck"
fi

cd ..
if [[ "$compare1" ]] || [[ "$compare1" ]] ; then
    exit
else
    rm "${1%.docx}.asncheck"
fi
