#! /bin/bash -ue
# Last edited on 2026-01-16 18:49:16 by stolfi

sec="$1"; shift
table_file="$1"; shift

USAGE="extract-section_from_ivt.sh {SEC} {TABLE_FILE} < INPUT.ivt > OUTPUT.ivt"

# Reads an EVT file from standard input and writes to standard output
# only those lines that belong to the specified section SEC (like
# "hea.1", "unk.8", etc). The argument {TABLE_FILE} must be the name of
# a file that contains a mapping of page number (like "f10r", "f86v3")
# to the corresponding section code.

if [[ ! ( -s ${table_file} ) ]]; then echo "** missing file ${table_file}" 1>&2; exit 1; fi

# Code is a bit complicated because of the lines with "{$I=..." specs that 
# have a variable number of fields beyond the locus ID "<f...>".

cat \
  | gawk \
      ' //{ 
          loc = $1; 
          lin = $0; gsub(/^[^>]*[>] */, "", lin);
          fnum = loc; gsub(/[.>].*$/,"",fnum); gsub(/^[<]/,"",fnum);
          printf "%s %s @@%s\n", fnum, loc, lin;
        }
      ' \
  | map_field.gawk \
      -v inField=1 \
      -v outField=1 \
      -v table=${table_file} \
  | egrep -e "^${sec} " \
  | gawk \
    ' //{ 
      loc = $3;
      lin = $0; gsub(/^.*@@/, "", lin);
      printf "%-19s %s\n", loc, lin }
    '
