💾 Archived View for tilde.club › ~filip › tech › script › versify › versify.awk.txt captured on 2022-07-16 at 15:39:44.

View Raw

More Information

-=-=-=-=-=-=-

#                    _  __                       _
#__   _____ _ __ ___(_)/ _|_   _   __ ___      _| | __
#\ \ / / _ \ '__/ __| | |_| | | | / _` \ \ /\ / / |/ /
# \ V /  __/ |  \__ \ |  _| |_| || (_| |\ V  V /|   <
#  \_/ \___|_|  |___/_|_|  \__, (_)__,_| \_/\_/ |_|\_\
#                          |___/
# FORWARDED VARIABLES: fullfilelocation, cmd
BEGIN{
    # GET FULL FILE PATH, NAME, AND EXTENSION
    if(fullfilelocation==""){
        getloc="readlink -f "ARGV[1] | getline t
        close(getloc)
        fullfilelocation=t
    }
    split(fullfilelocation,p,"/")
    for(i=1;i<length(p);i++){
        filedir=filedir p[i] "/"
    }
    split(p[length(p)],q,".")
    filename=q[1]
    fileextension=q[2]

    # SET FIELD SEPARATOR FOR THE INPUT FILE IN TSV FORMAT
    FS="\t"

    # VARIABLES.
    LIBNUM=0;LIBTIT="";                      # BOOK TITLE AND NUMBER.
    SERTIT="";SERNUM=0;SERLIBNUM=0;          # SERIES TITLE, NUMBER, AND NUMBER WITHIN A BOOK.
    VERNUM=0;VERLIBNUM=0;VERSERNUM=0;VER=""; # VERSE, VERSE NUMBER, VERSE NUMBER WITHIN A BOOK, AND VERSE NUMBER WITHIN A SERIES.

    # SPLIT REFERENCES IF cmd IS A LIST OF REFERENCES
    if(cmd~/^[^-].*/){
        split(cmd,l,"/")
        REF=l[1]
        QUE=l[2]
        split(REF,q,":")
        REFTYPE=length(q)
        REFLIB=q[1]
        REFSER=q[2]
        REFVER=q[3]
    }
}

# CHECK IF NUM IS IN RANGE. EXAMPLE: inrange("1,3,5-8,11",6)=1
function inrange(list,num){
    ok=0
    split(list,l,",")
    for(i=1;i<=length(l);i++){
        split(l[i],q,"-")
        if(length(q)==1){
            if((q[1]==num) || (q[1]=="*")){ok=1}
        }else{
            if(q[1]<=num && num<=q[2]){ok=1}
        }
    }
    return ok
}
# PRINT VERSE
function printverse(){
    printf "%s\t%s\t%s\t%s\n",LIBTIT,SERTIT,VERSERNUM,VER
}

# GET VERSE AND ADJUST VARIABLES
#{
#    if($1 != LIBTIT){
#        LIBTIT=$1;LIBNUM++;SERTIT=$2;SERNUM++;SERLIBNUM=1;VERNUM++;VERLIBNUM=1;VERSERNUM=1;
#    }else{
#        if($2 != SERTIT){
#            SERTIT=$2;SERNUM++;SERLIBNUM++;VERNUM++;VERLIBNUM++;VERSERNUM=1;
#        }else{
#            VERNUM++;VERSERNUM++;
#        }
#    }
#    VER=$3
#}
{
	LIBNUM=$1;LIBTIT=$2;                       # BOOK TITLE AND NUMBER.
    SERNUM=$3;SERLIBNUM=$4;SERTIT=$5;          # SERIES TITLE, NUMBER, AND NUMBER WITHIN A BOOK.
    VERNUM=$6;VERSERNUM=$7;VER=$8;             # VERSE, VERSE NUMBER, VERSE NUMBER WITHIN A BOOK, AND VERSE NUMBER WITHIN A SERIES.
}

# PRINT VERSES IN THE REFERENCES. EXAMPLE OF REFERENCES: "gen:1/heaven /aaron john:1:1"
(cmd~/^[^-].*/){
    if((REFTYPE==0) && tolower(VER)~tolower(QUE)){
        printverse()
    }

    if(REFTYPE==1){
        if(tolower(LIBTIT)~tolower(REFLIB) && !(REFLIB ~ /^[0-9]+$/) && tolower(VER)~tolower(QUE)){
            printverse()
        }else{
            if(inrange(REFLIB,LIBNUM) && tolower(VER)~tolower(QUE)){
                printverse()
            }
        }
    }

    if(REFTYPE==2){
        if(tolower(LIBTIT)~tolower(REFLIB) && !(REFLIB ~ /^[0-9]+$/) && tolower(VER)~tolower(QUE)){
            if(inrange(REFSER,SERLIBNUM)){
                printverse()
            }
        }else{
            if(inrange(REFLIB,LIBNUM) && inrange(REFSER,SERLIBNUM) && tolower(VER)~tolower(QUE)){
                printverse()
            }
        }
    }

    if(REFTYPE==3){
        if(tolower(LIBTIT)~tolower(REFLIB) && !(REFLIB ~ /^[0-9]+$/) && tolower(VER)~tolower(QUE)){
            if(inrange(REFSER,SERLIBNUM) && inrange(REFVER,VERSERNUM)){
                printverse()
            }
        }else{
            if(inrange(REFLIB,LIBNUM) && inrange(REFSER,SERLIBNUM) && inrange(REFVER,VERSERNUM) && tolower(VER)~tolower(QUE)){
                printverse()
            }
        }
    }
}