#!/bin/sh -e
# ----------------------------------------------------
# This script uses the Open Source asn1c to check all used ASN.1 modules
# are compile clean (no guarantee the generated code is valid).
#
# The used compile binary was built from here:
#   https://github.com/brchiu/asn1c/commits/velichkov_s1ap_plus_option_group_plus_adding_trailing_ull
#
# The referenced ISO ASN.1 modules are pulled from the ISO Standards Maintenance Portal
# Yet not all are compile clean, thus minor fixed need to be done.
# All these changes are irrelevant for the resulting encoding.
#  - Fixing duplicate DE tags
#  - Fixing OID typo
#  - Removing unused types (which bloat the IMPORTS)
# ----------------------------------------------------

# set -x
ASN_CACHE=".external"
ASN_OUT=".build"

mkdir -p ${ASN_CACHE} ${ASN_OUT}

if [ ! -f iso-patched/ISO19321IVIv2-patched.asn ]; then
  wget -P ${ASN_CACHE} 'https://standards.iso.org/iso/ts/19321/ed-2/en/ISO19321IVIv2.asn'
  sed -e 's/^IVI {.*/IVI {iso (1) standard (0) ivi (19321) major-version-3 (3) minor-version-1 (1)}/g' \
      -e 's/^FROM DSRC.*/FROM ETSI-ITS-DSRC { itu-t (0) identified-organization (4) etsi (0) itsDomain (5) wg1 (1) ts103301 (103301) dsrc (6) major-version-2 (2) minor-version-1 (1) }/g' \
      -e 's/^FROM ITS-Container.*/FROM ETSI-ITS-CDD { itu-t (0) identified-organization (4) etsi (0) itsDomain (5) wg1 (1) 102894 cdd (2) major-version-4 (4) minor-version-1 (1) }/g' \
      ${ASN_CACHE}/ISO19321IVIv2.asn > iso-patched/ISO19321IVIv2-patched.asn
fi

ASN1_SRC="\
 DSRC-addgrp-C.asn \
 DSRC-region.asn \
 DSRC.asn \
 cdd/ETSI-ITS-CDD.asn"

set +e
asnimports.py --verbose -o ${ASN_CACHE} ${ASN1_SRC} > ${ASN_CACHE}/dsrc-ext-modules
set -e

ASN1C_OPTIONS="-E -F -fcompound-names -fknown-extern-type=DATE"

asn1c ${ASN1C_OPTIONS} -pdu=SPATEM SPATEM-PDU-Descriptions.asn ${ASN1_SRC} $(cat ${ASN_CACHE}/dsrc-ext-modules) > ${ASN_OUT}/SPATEM.asn

asn1c ${ASN1C_OPTIONS} -pdu=MAPEM MAPEM-PDU-Descriptions.asn ${ASN1_SRC} $(cat ${ASN_CACHE}/dsrc-ext-modules) > ${ASN_OUT}/MAPEM.asn

asn1c ${ASN1C_OPTIONS} -pdu=SREM SREM-PDU-Descriptions.asn ${ASN1_SRC} $(cat ${ASN_CACHE}/dsrc-ext-modules) > ${ASN_OUT}/SREM.asn

asn1c ${ASN1C_OPTIONS} -pdu=SSEM SSEM-PDU-Descriptions.asn  ${ASN1_SRC} $(cat ${ASN_CACHE}/dsrc-ext-modules) > ${ASN_OUT}/SSEM.asn

asn1c ${ASN1C_OPTIONS} -pdu=RTCMEM RTCMEM-PDU-Descriptions.asn  ${ASN1_SRC} $(cat ${ASN_CACHE}/dsrc-ext-modules) > ${ASN_OUT}/RTCMEM.asn

asn1c ${ASN1C_OPTIONS} -pdu=IVIM IVIM-PDU-Descriptions.asn ${ASN1_SRC} $(cat ${ASN_CACHE}/dsrc-ext-modules) \
  iso-patched/ISO19321IVIv2-patched.asn \
	iso-patched/ISO14823-missing.asn \
  iso-patched/AVIAEINumberingAndDataStructures-1-0-14816-1-1.asn \
	'iso-patched/ISO14906(2018)EfcDsrcApplicationv6-patched.asn' \
	'iso-patched/ISO14906(2018)EfcDsrcGenericv7-patched.asn' \
   > ${ASN_OUT}/IVIM.asn

python3 asn2raw.py DSRC.asn > ${ASN_OUT}/DSRC.raw.asn
python3 asn2raw.py DSRC-region.asn > ${ASN_OUT}/DSRC-region.raw.asn
python3 asn2raw.py DSRC-addgrp-C.asn > ${ASN_OUT}/DSRC-addgrp-C.raw.asn

echo "++++++++++++++++++++++++"
echo "+++ SUCCESS"
echo "+++ All modules compiled"
echo "++++++++++++++++++++++++"
