#!/bin/bash
# output su stdout
# input RangeMessage.xml from https://www.isbn-international.org/range_file_generation

IFS=$'\t\n'
fFileInp=${1:-RangeMessage.xml}
echo '#ifndef TELLICO_ISBNVALIDATOR_RANGE_H'
echo '#define TELLICO_ISBNVALIDATOR_RANGE_H'
echo
echo "//--------------------------------------:--------------------------------------:"
echo "// Source:        $(grep -m1 '<MessageSource>' "${fFileInp}"|sed -r 's/^[^>]+>([^<]+)<.+$/\1/')"
echo "// Serial Number: $(grep -m1 'MessageSerialNumber>' "${fFileInp}"|sed -r 's/^[^>]+>([^<]+)<.+$/\1/')"
echo "// Date:          $(grep -m1 '<MessageDate>' "${fFileInp}"|sed -r 's/^[^>]+>([^<]+)<.+$/\1/')"
echo "// Generated by:  Alex Oio"
echo "//--------------------------------------:--------------------------------------:"
echo
echo "ISBNValidator::isbn_band* ISBNValidator::bands;"
echo

# read -p 'continua ...' xxxx

sOldEan=''
sOldPrefix=''
for sRow in $(sed -n '/<RegistrationGroups>/,/<\/RegistrationGroups>/p' "${fFileInp}"|\
              grep -E '<(Prefix|Range|Length|Agency|/Rule)>'|\
              sed ':a;N;$!ba;s/\n//g'|sed -re 's/ *<\/Rule>/\n/g' -e 's/^ +//g'); do

   cmdSetData=$(echo "${sRow}"|sed -re 's/<([^\/][^>]+)>/s\1="/g' -e 's/<\/[^>]+>/"; /g')
   eval "${cmdSetData}"

   sRangeMin=${sRange%-*}
   sRangeMax=${sRange#*-}
   sPrefixEan=${sPrefix%-*}
   sPrefixLng=${sPrefix#*-}
   sIsbnMin=$(printf '%.8s' "${sPrefixLng}${sRangeMin}")
   sIsbnMax=$(printf '%.8s' "${sPrefixLng}${sRangeMax}")

   if [ "${sPrefixEan}" != "${sOldEan}" ]; then
      sNextMin='00000001'
      echo "Initialize ${sPrefixEan}" 1>&2
      test "${sOldEan}" != '' && echo "//--------------------------------------: end of last group"
      test "${sOldEan}" != '' && echo "};" && echo
      echo "//--------------------------------------:--------------------------------------:"
      echo "ISBNValidator::isbn_band ISBNValidator::bands${sPrefixEan}[] = {"
      sOldEan="${sPrefixEan}"
      sOldPrefix=''
   fi

   if [ "${sNextMin}" -lt "${sIsbnMin}" ]; then
      test "${sOldPrefix}" == '' && sOldPrefix="${sPrefixEan}-0"
      sPrevLng=${sOldPrefix#*-}
      sPrevMax=$(printf '%08i' $((10#${sIsbnMin}-1)))
      sPrefixFake=$(printf "%.${#sPrevLng}s" ${sPrevMax})
      sRangeFake=$(printf '%.7s' "${sPrevMax#${sPrefixFake}}99999")
echo "Dummy range:" 1>&2
echo "- sIsbnMin=${sIsbnMin}" 1>&2
echo "- sPrevMax=${sPrevMax}" 1>&2
echo "- #sPrefixLng=${#sPrefixLng} (${sPrefixLng})" 1>&2
echo "- sPrefixFake=${sPrefixFake}" 1>&2
echo "- sRangeFake=${sRangeFake}" 1>&2

      echo "//--------------------------------------: Dummy (${sPrefixEan}-${sPrefixFake})"
      echo "ISBNGRP_${#sPrefixFake}DUMMY(${sPrefixFake},$(printf "% $((6-${#sPrefixFake}))s" ' ') ISBNPUB_7DIGIT($(printf '% 7s' $((10#${sRangeFake})))), 0, 9),"
      read -p 'continua ...' xxxx
   fi
    if [ "${sOldPrefix}" != "${sPrefix}" ]; then
      echo "//--------------------------------------: ${sAgency} (${sPrefix})"
      sOldPrefix="${sPrefix}"
   fi
   nMidPos=0
   test ${sLength} -ne 0 && nMidPos=$((${#sPrefixLng} + ${sLength}))
   echo "ISBNGRP_${#sPrefixLng}DIGIT(${sPrefixLng},$(printf "% $((6-${#sPrefixLng}))s" ' ') ISBNPUB_7DIGIT(${sRangePrt}$(printf '% 7s' $((10#${sRangeMax})))), ${nMidPos}, 9),"
   sNextMin=$(printf '%08s' $((10#${sIsbnMax}+1)))

done
echo "//--------------------------------------: Dummy (${sPrefixEan}-9)"
echo "ISBNGRP_1DUMMY(9,      ISBNPUB_7DIGIT(9999999), 0, 9)"
echo "//--------------------------------------: end of last group"
echo "};"
echo "//--------------------------------------:--------------------------------------:"
echo '#endif'
