#!/bin/bash

function choose() {
    local F="$1"

    shift
    grep "$@" < "$F"
}

function format() {
    fgrep ')$' tags		\
	| tr '\t' ' '		\
	| sed 's=/^=='		\
	| cut -d '$' -f 1	\
	| sort -f
}

function header() {
cat <<.EOF
This file contains a list of all functions used in the ELKS kernel source tree as of version 0.1.1 thereof. It is split into separate sections for static and
global functions in each of the following categories:

 1. Scripts used to control the ELKS kernel compilation process.

 2. Tools used as part of the ELKS kernel compilation process.

 3. The ELKS kernel itself.
.EOF
}

function layout() {
    local A B C D

    while read A B D ; do
	if echo "$D" | grep -q '^static ' ; then
	    C=static
	    D=`echo "$D" | cut -d ' ' -f 2-`
	else
	    C=''
	fi
	printf '%-31s %-37s %-7s %s\n' "$A" "$B" "$C" "$D"
    done
}

function newline() {
    sed 's/$//' | tr '' '\r'
}

function process() {
    format | layout > tags.txt
    header
    ruler
    choose tags.txt ' scripts/'
    ruler
    choose tags.txt /tools/
    ruler
    choose tags.txt -v ' scripts/' | fgrep -v /tools/
    ruler
    trailer
}

function ruler() {
cat <<.EOF

================================================================================================================================================================

.EOF
}

function trailer() {
    echo End of listing.
}

if [ $# -eq 0 ]; then
    ctags *.c
else
    ctags "$@"
fi
process | newline > tags.lst
