#!/bin/sh

usage ()
{
    cat <<EOF
Usage: $0 [--print-doc] path-to-gdb
EOF
    exit 1
}

print_doc=
level=regression
case "$1" in
    --print-doc ) print_doc=true ; shift ;;
esac

if test ! "${print_doc}"
then
    if test $# -ne 1
    then
	usage
    fi
    gdb=$1 ; shift
    configure_target=${gdb}/configure.tgt
    configure_host=${gdb}/configure.host
    config=${gdb}/config
fi


# Check that there is a directory for all host/target CPUs

obsolete_cpu ()
{
    # ari.*.bug: <FILE>:<LINE>: <CATEGORY>: <BUG>: <DOC>
    cat <<EOF
$1: $2: regression: obsolete cpu: No config directory for cpu$3
EOF
}

if test "${print_doc}"
then
    obsolete_cpu "" "" ""
else
    awk '
/gdb_(target|host)_cpu=[a-z]/ {
    cpu = gensub(/^.*gdb_(target|host)_cpu=([-a-z0-9]*).*$/, "\\2", 1)
    print cpu, FILENAME, FNR
}
' ${configure_target} ${configure_host} | sort -u | while read cpu filename fnr
    do
	if test ! -d ${config}/${cpu}
	then
	    obsolete_cpu ${filename} ${fnr} " (${cpu})"
	fi
    done
fi





obsolete_system ()
{
    # ari.*.bug: <FILE>:<LINE>: <CATEGORY>: <BUG>: <DOC>
    cat <<EOF
$1: $2: ${level}: obsolete system: No config file for system$3
EOF
}
if test "${print_doc}"
then
    obsolete_system "" "" ""
fi

# Check that there is a .mt file for all targets

if test "${print_doc}"
then
    :
else
    awk '
/gdb_target=/ {
    print gensub(/^.*gdb_target=([-a-z0-9]*).*$/, "\\1", 1), FNR
}
' < ${configure_target} | sort -u | while read target fnr
    do
	if find ${config} -name ${target}.mt -print | awk '
BEGIN { status = 0 }
{ status = 1}
END { exit status}'
	then
	    obsolete_system ${configure_target} ${fnr} " (target ${target})"
	fi
    done
fi



# Check that there is a .mh file for all hosts

if test "${print_doc}"
then
    :
else
    awk '
/gdb_host=/ {
    print gensub(/^.*gdb_host=([-a-z0-9]*).*$/, "\\1", 1), FNR
}
' ${configure_host} | sort -u | while read host fnr
    do
	if find ${config} -name ${host}.mh -print | awk '
BEGIN { status = 0 }
{ status = 1}
END { exit status}'
	then
	    obsolete_system ${configure_host} ${fnr} " (host ${host})"
	fi
    done
fi


obsolete_file ()
{
	# ari.*.bug: <FILE>:<LINE>: <CATEGORY>: <BUG>: <DOC>
	cat <<EOF
$1: $2: ${level}: obsolete file: The file is not used$3
EOF
}
if test "${print_doc}"
then
    obsolete_file "" "" ""
fi

# Check that every .mh file has a gdb_host=

if test "${print_doc}"
then
    :
else
    find ${config} -name '*.mh' -print | while read mh
    do
	host=`basename $mh .mh`
	if awk '
BEGIN { status = 0 }
/(^|[^_[:alnum:]])gdb_host='"${host}"'($|[^_[:alnum:]])/ { status = 1 }
END { exit status }
' ${configure_host}
	then
	    obsolete_file ${mh} 0 " (${host}.mh)"
	fi
    done
fi



# Check that every .mt file has a gdb_target=

if test "${print_doc}"
then
    :
else
    find ${config} -name '*.mt' -print | while read mt
    do
	target=`basename $mt .mt`
	if awk '
BEGIN { status = 0 }
/(^|[^_[:alnum:]])gdb_target='"${target}"'($|[^_[:alnum:]])/ { status = 1 }
END { exit status }
' ${configure_target}
	then
	    obsolete_file ${mt} 0 " (${target}.mt)"
	fi
    done
fi



# Check that every config .h file is used

if test "${print_doc}"
then
    :
else
    find ${config} -name '*.h' -print | while read h
    do
	f=`basename $h .h`
	if find ${config} -name CVS -prune -o -type f -print \
		| xargs grep "[^-a-z0-9]$f\.h" \
		| awk '
BEGIN { status = 0 }
{ status = 1}
END { exit status}'
	then
	    obsolete_file "${h}" 0 " (${f}.h)"
	fi
    done
fi

