#!/usr/bin/env bash
# prints all files in SOURCE that have incomplete variable descriptions to console.
# in each file, prints corresponding subroutine, function or module with its line.

# returns the line-number of the last line matching $3 in file $1 before line $2
get_prev_regex_line_number () {
   local search_end=$(expr ${2} - 1)
   line_number_prev=$(sed -n 1,${search_end}p ${1} | grep -n ${3} | grep -vP ': *!' | cut -f1 -d: | tail -n1)
#    line_number_prev=$(sed -n "/${3}/{1,${search_end}=}" ${1} | tail -n1)
   echo $line_number_prev
}

# searches file $1 for the closest expression SUBROUTINE, FUNCTION or MODULE to each uncommented declaration and returns that line number
foo () {
    # all lines that have doxygen comment (!<) ready but end after that and are not a comment (don' start with !)
    for uncommented_linenumber in $(grep -nP '!< *$' ${1} | grep -vP ': *!' | grep -oP '^\d{1,5}:' | grep -oP '^\d{1,5}'); do
        rout=-999
        func=-999
        mod=-999
        rout=$(get_prev_regex_line_number ${1} ${uncommented_linenumber} 'SUBROUTINE')
        func=$(get_prev_regex_line_number ${1} ${uncommented_linenumber} 'FUNCTION')
        mod=$(get_prev_regex_line_number ${1} ${uncommented_linenumber} 'MODULE')
        # return only the largest of the found line numbers as this is the closest to the uncommented declaration
        if [[ ${rout} -gt ${func} ]]; then
            if [[ ${rout} -gt ${mod} ]]; then
                echo ${rout}
            else
                echo ${mod}
            fi
        else
            if [[ ${func} -gt ${mod} ]]; then
                echo ${func}
            else
                echo ${mod}
            fi
        fi
    done
    # all lines that contain declarations (::) but no comment at the end and are no comments themselves
    for uncommented_linenumber in $(grep -nP '::' ${1} | grep -vP '!<' | grep -vP 'INTRINSIC' | grep -vP ': *!' | grep -oP '^\d{1,5}:' | grep -oP '^\d{1,5}'); do
        rout=-999
        func=-999
        mod=-999
        rout=$(get_prev_regex_line_number ${1} ${uncommented_linenumber} 'SUBROUTINE')
        func=$(get_prev_regex_line_number ${1} ${uncommented_linenumber} 'FUNCTION')
        mod=$(get_prev_regex_line_number ${1} ${uncommented_linenumber} 'MODULE')
        # return only the largest of the found line numbers as this is the closest to the uncommented declaration
        if [[ ${rout} -gt ${func} ]]; then
            if [[ ${rout} -gt ${mod} ]]; then
                echo ${rout}
            else
                echo ${mod}
            fi
        else
            if [[ ${func} -gt ${mod} ]]; then
                echo ${func}
            else
                echo ${mod}
            fi
        fi
    done
}

wd=~/palm/current_version/trunk/SOURCE
for filename in ${wd}/*.f90; do
    #give each line only once and none if they still have initial values
    routine_lines=$(foo ${filename} | sort -n | uniq | sed "s/-999//" )
    #print filename if there are uncommented declarations
    if [[ ${routine_lines} != "" ]]; then
        echo $(basename ${filename} )
    fi
    #print each SUBROUTINE, FUNCTION or MODULE with line and name that contains uncommented declarations
    for linenumber in ${routine_lines}; do
        sed -n "${linenumber}{p;q}" ${filename} | sed "s/^ */   ${linenumber}   /" | sed "s/([^)]*) *$//" | sed "s/([^&]*& *$//" | sed "s/(.* result//" | sed "s/(.* RESULT//"
    done
done
