#!/usr/bin/env bash
#-----------------------------------------------------------------------
#        NAME:	convtbl_extract
#       USAGE:	convtbl_extract <convinfo.txt >convinfo.tbl
#		convtbl_extract  convinfo.txt >convinfo.tbl
#		convtbl_extract  convinfo.txt  convinfo.tbl
#		convtbl_extract  convinfo.txt  convinfo.tbl active.tbl
#
# DESCRIPTION:	extracts from a given <convinfo.txt> file to produce
#		a list in the form of convinfo.db/available.tbl, and
#		ready to be edited by a convinfo.db/ editor.
#-----------------------------------------------------------------------

c=$(basename $(which $0))	# find command name
d=$(dirname  $(which $0))	# locate the command

# Functions
pre_listing_(){
  awk '
/^[ 	]*!.*$/		{ next }
			{
			  #printf("%s %s %s %s\n",$2,$3,$1,$4)
			  printf("%s %s %s %s\n",$3,$2,$1,$4)
			}'
}

post_listing_(){
  awk '
BEGIN			{ NJ=20; m=0; i=-99; j=-99; list=""; last=""; lastlist="" }
			{ this=sprintf("%-3s %4s",$1,$2);
			  if (m==0) {		# there is nothing yet
			    m=1; nextj=j+NJ; i=$1; j=$2; list=$3;
			  } else {		# there is something saved
			    if (this==last) {	# and the new record is the same class
			      m++; nextj=j+NJ; i=$1; j=$2; list=sprintf("%-s %s",list,$3);	# append the new otype
			    } else {		# otherwise, dump the list
			      if((lastlist!=list) || (nextj<j)) { printf("\n#<subty>  <from>            <to>      <type> m    <otypes>\n") }
			      printf("%-3s  19000101 000000   29991231 240000  %4s%2i    %s",i,j,m,list);
			      printf("\n");
			      lastlist=list;
			      m=1; nextj=j+NJ; i=$1; j=$2; list=$3;	# then restart
			    }
			  }
			  last=this;		# save the new class for the next comparison
			}
END			{ if (m!=0) {		# there is nothing in the input, dump the saved.
			    if((lastlist!=list) || (nextj<j)) { printf("\n#<subty>  <from>            <to>      <type> m    <otypes>\n") }
			    printf("%-3s  19000101 000000   29991231 240000  %4s%2i    %s",i,j,m,list);
			    printf("\n");
			  }
			}'
}

active_listing_(){
  awk '
BEGIN			{ NJ=20; m=0; i=-99; j=-99; list=""; last=""; lastlist=""; n=0; luse="" }
			{ this=sprintf("%-3s %4s",$1,$2);
			  iuse=0; if ($4=="1") {iuse=1}
			  if (m==0) {		# there is nothing yet
			    m=1; nextj=j+NJ; i=$1; j=$2; list=$3; n=0; luse="";
			    if (iuse==1) { n=1; luse=$3 }
			  } else {		# there is something saved
			    if (this==last) {	# and the new record is the same class
			      m++; nextj=j+NJ; i=$1; j=$2; list=sprintf("%-s %s",list,$3);	# append the new otype
			      if (iuse==1) {if (n==0) {n=1; luse=$3} else {n++; luse=sprintf("%-s %s",luse,$3)}}
			    } else {		# otherwise, dump the list
			      if((lastlist!=list) || (nextj<j)) { printf("\n#<subty>  <from>            <to>      <type> m    <otypes>\n") }
			      printf("%-3s  19000101 000000   29991231 240000  %4s%2i    %s",i,j,n,luse);
			      if(m!=n) {printf("\t#  %2i    %s",m,list)}
			      printf("\n");
			      lastlist=list;
			      m=1; nextj=j+NJ; i=$1; j=$2; list=$3; n=0; luse="";
			      if (iuse==1) { n=1; luse=$3 }
			    }
			  }
			  last=this;		# save the new class for the next comparison
			}
END			{ if (m!=0) {		# there is nothing in the input, dump the saved.
			    if((lastlist!=list) || (nextj<j)) { printf("\n#<subty>  <from>            <to>      <type> m    <otypes>\n") }
			    printf("%-3s  19000101 000000   29991231 240000  %4s%2i    %s",i,j,n,luse);
			    if(m!=n) {printf("\t#  %2i    %s",m,list)}
			    printf("\n");
			  }
			}'
}

list_sorting_(){
 env LC_ALL=C sort --stable -k2,2n -k1,1n -k3,3f -u
}

#-- Check arguments
if [ $# -eq 0 ]; then
  pre_listing_ | list_sorting_ | post_listing_
  pre_listing_ | list_sorting_ | active_listing_

elif [ $# -eq 1 ]; then
  pre_listing_ <$1 | list_sorting_ | post_listing_
  pre_listing_ <$1 | list_sorting_ | active_listing_

elif [ $# -eq 2 ]; then
  pre_listing_ <$1 | list_sorting_ | post_listing_ >$2
  pre_listing_ <$1 | list_sorting_ | active_listing_ >>$2

elif [ $# -eq 3 ]; then
  pre_listing_ <$1 | list_sorting_ | post_listing_ >$2
  pre_listing_ <$1 | list_sorting_ | active_listing_ >$3

else
  echo "Usage: ${c} [<convinfo.txt> [<convinfo.tbl>]]" 1>&2
  exit 1
fi
